mechanical-orchard / playwright-elixir

An Elixir library for Playwright web automation
MIT License
119 stars 23 forks source link

adds support for BrowserType.connect_over_cdp() (for chromium browser only) #37

Open oliverswitzer opened 2 years ago

oliverswitzer commented 2 years ago

This PR adds support for playwright's BrowserType.connectOverCdp() function, which attaches Playwright to an existing browser instance using the Chrome DevTools Protocol.

TODOs

coreyti commented 2 years ago

Hey @oliverswitzer, I'm getting test suite timeouts when running mix test test/integration and with the ConnectCDPTest. Those failures go away if I run that test alone, or if I remove that test and run the whole suite. I haven't spent a lot of time trying to figure out why, but perhaps we have some test cleanup issue.

Regarding the Playwright.launch with other than Chromium, I'll take a look into that. It's not too surprising that things are not working as expected there because I've not spent any time yet on browsers other than Chromium. BrowserType could also use some improvement to come more in-line with official implementations... it might be about time to do that work :)

oliverswitzer commented 2 years ago

Seems that @coreyti and I narrowed the test issue down to setup_browser getting called directly from test/integration/browser_test.exs:8

It is the only test that calls setup_browser, and we are able to reproduce the failing test suite when running test/integration/browser_type/connect_cdp_test.exs before test/integration/browser_test.exs

oliverswitzer commented 2 years ago

After pairing a bit more through this with @coreyti, it became apparent that browser_test was not the culprit and that more tests started failing when run right after connect_over_cdp_test.exs if we were to comment out that specific test.

From poking around in the playwright-python and playwright-js client code, we discovered that each of the other libraries seem to have instances of BrowserType that are isolated from each other and have their own channels/state.

The playwright server sends __create__ messages for a new browser, and all the pages and contexts that already exists on the playwright browser you are connecting to over CDP. As implemented in in playwright-elixir right now, connect_over_cdp/3 adds these objects to the same session catalog as the browser you passed into the function.

This duplication in the same catalog may be leading to some of the order-dependent test failures that we're seeing. It seems like our implementation of connectOverCDP should probably spin up a separate playwright session/catalog/supervision tree instead of sharing a session / catalog with the browser that gets passed into connect_over_cdp/3.