-
Notifications
You must be signed in to change notification settings - Fork 30
adds support for BrowserType.connect_over_cdp() (for chromium browser only) #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
oliverswitzer
wants to merge
2
commits into
main
Choose a base branch
from
add-connect-cdp-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,121 @@ | ||
defmodule Playwright.BrowserType.ConnectCDPTest do | ||
use Playwright.TestCase, async: true | ||
@remote_debug_port "9222" | ||
|
||
# test_connect_to_an_existing_cdp_session | ||
# test_connect_to_an_existing_cdp_session_twice | ||
# test_conect_over_a_ws_endpoint | ||
use Playwright.TestCase, async: true, args: ["--remote-debugging-port=#{@remote_debug_port}"] | ||
|
||
alias Playwright.Browser | ||
alias Playwright.BrowserContext | ||
alias Playwright.BrowserType | ||
alias Playwright.Page | ||
|
||
@tag :skip | ||
test "can only connect to CDP session if using chromium client" do | ||
# For some reason this still launches a chromium browser... I could not | ||
# figure out why. | ||
|
||
browser = Playwright.launch(:firefox) | ||
|
||
expected_message = "Attempted to use Playwright.BrowserType.connect_over_cdp/3 with incompatible browser client" | ||
|
||
assert_raise RuntimeError, expected_message, fn -> | ||
BrowserType.connect_over_cdp( | ||
browser, | ||
"http://localhost:#{@remote_debug_port}/" | ||
) | ||
end | ||
end | ||
|
||
test "can connect to an existing CDP session via http endpoint", %{browser: browser} do | ||
cdp_browser = | ||
BrowserType.connect_over_cdp( | ||
browser, | ||
"http://localhost:#{@remote_debug_port}/" | ||
) | ||
|
||
assert length(Browser.contexts(cdp_browser)) == 1 | ||
|
||
Browser.close(cdp_browser) | ||
end | ||
|
||
@tag exclude: [:page] | ||
test "can connect to an existing CDP session twice", %{browser: browser, assets: assets} do | ||
cdp_browser1 = | ||
BrowserType.connect_over_cdp( | ||
browser, | ||
"http://localhost:#{@remote_debug_port}/" | ||
) | ||
|
||
cdp_browser2 = | ||
BrowserType.connect_over_cdp( | ||
browser, | ||
"http://localhost:#{@remote_debug_port}/" | ||
) | ||
|
||
cdp_browser3 = | ||
BrowserType.connect_over_cdp( | ||
browser, | ||
"http://localhost:#{@remote_debug_port}/" | ||
) | ||
|
||
assert length(contexts(cdp_browser1)) == 1 | ||
|
||
page1 = | ||
contexts(cdp_browser1) | ||
|> List.first() | ||
|> BrowserContext.new_page() | ||
|
||
Page.goto(page1, assets.empty) | ||
|
||
assert length(contexts(cdp_browser2)) == 1 | ||
|
||
page2 = | ||
contexts(cdp_browser2) | ||
|> List.first() | ||
|> BrowserContext.new_page() | ||
|
||
Page.goto(page2, assets.empty) | ||
|
||
assert contexts(cdp_browser1) | ||
|> List.first() | ||
|> pages() | ||
|> length() == 2 | ||
|
||
assert contexts(cdp_browser2) | ||
|> List.first() | ||
|> pages() | ||
|> length() == 2 | ||
|
||
# NOTE: no `Page` was explicitly created off of `cdp_browser3`, but its context includes | ||
# those created by the other sessions. See docs in `BrowserType.connect_over_cdp/3` | ||
assert contexts(cdp_browser3) | ||
|> List.first() | ||
|> pages() | ||
|> length() == 2 | ||
|
||
Browser.close(cdp_browser1) | ||
Browser.close(cdp_browser2) | ||
Browser.close(cdp_browser3) | ||
end | ||
|
||
test "can connect over a websocket endpoint", %{browser: browser} do | ||
ws_endpoint = ws_endpoint_for_url("http://localhost:#{@remote_debug_port}/json/version") | ||
|
||
cdp_browser1 = BrowserType.connect_over_cdp(browser, ws_endpoint) | ||
assert contexts(cdp_browser1) |> length() == 1 | ||
Browser.close(cdp_browser1) | ||
|
||
cdp_browser2 = BrowserType.connect_over_cdp(browser, ws_endpoint) | ||
assert contexts(cdp_browser2) |> length() == 1 | ||
Browser.close(cdp_browser2) | ||
end | ||
|
||
defp ws_endpoint_for_url(url) do | ||
{:ok, %{body: body}} = HTTPoison.get(url) | ||
|
||
Jason.decode!(body) | ||
|> Map.get("webSocketDebuggerUrl") | ||
end | ||
|
||
defp contexts(browser), do: Browser.contexts(browser) | ||
defp pages(context), do: BrowserContext.pages(context) | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coreyti any thoughts about how to better test this error case? I couldn't quite figure out why but it appears that no matter whether I call
Playwright.launch()
with (ie:webkit
or:firefox
) I still get a chromium%Playwright.Browser{}
back.Perhaps this is just something to note and move on from since technically this library only supports chromium at the moment.