diff --git a/DRIVER_VERSION b/DRIVER_VERSION index e18f92e87..ac6792c0f 100644 --- a/DRIVER_VERSION +++ b/DRIVER_VERSION @@ -1 +1 @@ -a018f24a4fc76e2120d3b35d2415f1e5bd4b073e +049226728d2da8054db18b04782a9848b755855e diff --git a/playwright/_impl/frame.py b/playwright/_impl/frame.py index d335258d3..6ff9e917c 100644 --- a/playwright/_impl/frame.py +++ b/playwright/_impl/frame.py @@ -386,18 +386,6 @@ async def evaluate(self, expression: str, arg: Serializable) -> Any: ) return parse_result(result["value"]) - async def _evaluate_expose_utility_script( - self, expression: str, arg: Serializable - ) -> Any: - result = await self._channel.evaluateExpression( - expression=expression, - # Python: undefined; see the docstring of js_handle.py. - isFunction=None, - arg=serialize_argument(arg), - timeout=NO_TIMEOUT, - ) - return parse_result(result["value"]) - async def query_selector( self, selector: str, strict: Optional[bool] ) -> Optional[ElementHandle]: diff --git a/playwright/async_api/_generated.py b/playwright/async_api/_generated.py index fdc48ed1d..13108ea4a 100644 --- a/playwright/async_api/_generated.py +++ b/playwright/async_api/_generated.py @@ -4017,13 +4017,41 @@ async def connect_over_cdp( A CDP websocket endpoint or http url to connect to. For example `http://localhost:9222/` or `ws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4`. - Alternatively, a browser channel name such as `chrome` or `msedge` to connect to a browser with remote debugging - enabled in its default user data directory. On Linux, Playwright uses `CHROME_USER_DATA_DIR` when set. Otherwise, - it uses `CHROME_CONFIG_HOME`, then `XDG_CONFIG_HOME`, then `~/.config` to locate that directory. + Alternatively, a URL that tells which running browser to connect to: + - `remote-debugging://` - a browser with remote debugging enabled, for example + `remote-debugging://msedge`. The optional `userDataDir` parameter is the user data directory of the browser, + when it is not the default one: `remote-debugging://chrome?userDataDir=/path/to/data`. A bare channel name such + as `chrome` is the same as `remote-debugging://chrome`. + - `extension:///` - the tabs of a browser profile, through the + [Playwright Extension](https://chromewebstore.google.com/detail/playwright-extension/mmlmfjhmonkocbjadbfplnigmagldckm), + for example `extension://chrome/Profile 1`. Playwright opens the connect page of the extension in that profile, + and the user selects the tabs to share. Without the profile directory, Playwright uses the `Default` profile. + The optional parameters are `userDataDir`, the user data directory of the browser when it is not the default + one, and `executablePath`, the path to the browser executable when it is not at the standard location: + `extension://chromium?executablePath=/path/to/chrome&userDataDir=/path/to/data`. Pass the other options of this + connection in `headers`. + + The values of `userDataDir` and `executablePath` must be URL-encoded, for example with `encodeURIComponent` or + `URLSearchParams`. The channel is a browser channel name such as `chrome`, `chrome-beta` or `msedge`. The default + user data directory of the channel is where the browser keeps its data. On Linux, Playwright uses + `CHROME_USER_DATA_DIR` when set. Otherwise, it uses `CHROME_CONFIG_HOME`, then `XDG_CONFIG_HOME`, then `~/.config` + to locate that directory. artifacts_dir : Union[pathlib.Path, str, None] If specified, browser artifacts (such as traces and downloads) are saved into this directory. headers : Union[Dict[str, str], None] Additional HTTP headers to be sent with connect request. Optional. + + When `endpoint_url` is an extension URL such as `extension://chrome`, the headers are not sent. They are the + options of the connection: + - `x-playwright-extension-token` - the token of the extension. With the token, the user does not have to approve + the connection. Without the token, the user must approve the connection before `timeout` expires, so set it to + a value that gives the user enough time. + - `x-playwright-extension-client-name` - the name of the client that the extension shows to the user. + - `x-playwright-extension-tab-group` - pass `false` to keep the tabs out of a tab group. + - `x-playwright-extension-tab-group-title` - the title of the tab group. + - `x-playwright-extension-tab-group-color` - the color of the tab group: `green`, `blue`, `purple`, `orange`, + `pink`, `cyan`, `yellow` or `red`. + - `x-playwright-extension-background` - pass `true` to keep the tabs from becoming active. is_local : Union[bool, None] Tells Playwright that it runs on the same host as the CDP server. It will enable certain optimizations that rely upon the file system being the same between Playwright and the Browser. diff --git a/playwright/sync_api/_generated.py b/playwright/sync_api/_generated.py index bf4d4df2f..0831744b3 100644 --- a/playwright/sync_api/_generated.py +++ b/playwright/sync_api/_generated.py @@ -3943,13 +3943,41 @@ def connect_over_cdp( A CDP websocket endpoint or http url to connect to. For example `http://localhost:9222/` or `ws://127.0.0.1:9222/devtools/browser/387adf4c-243f-4051-a181-46798f4a46f4`. - Alternatively, a browser channel name such as `chrome` or `msedge` to connect to a browser with remote debugging - enabled in its default user data directory. On Linux, Playwright uses `CHROME_USER_DATA_DIR` when set. Otherwise, - it uses `CHROME_CONFIG_HOME`, then `XDG_CONFIG_HOME`, then `~/.config` to locate that directory. + Alternatively, a URL that tells which running browser to connect to: + - `remote-debugging://` - a browser with remote debugging enabled, for example + `remote-debugging://msedge`. The optional `userDataDir` parameter is the user data directory of the browser, + when it is not the default one: `remote-debugging://chrome?userDataDir=/path/to/data`. A bare channel name such + as `chrome` is the same as `remote-debugging://chrome`. + - `extension:///` - the tabs of a browser profile, through the + [Playwright Extension](https://chromewebstore.google.com/detail/playwright-extension/mmlmfjhmonkocbjadbfplnigmagldckm), + for example `extension://chrome/Profile 1`. Playwright opens the connect page of the extension in that profile, + and the user selects the tabs to share. Without the profile directory, Playwright uses the `Default` profile. + The optional parameters are `userDataDir`, the user data directory of the browser when it is not the default + one, and `executablePath`, the path to the browser executable when it is not at the standard location: + `extension://chromium?executablePath=/path/to/chrome&userDataDir=/path/to/data`. Pass the other options of this + connection in `headers`. + + The values of `userDataDir` and `executablePath` must be URL-encoded, for example with `encodeURIComponent` or + `URLSearchParams`. The channel is a browser channel name such as `chrome`, `chrome-beta` or `msedge`. The default + user data directory of the channel is where the browser keeps its data. On Linux, Playwright uses + `CHROME_USER_DATA_DIR` when set. Otherwise, it uses `CHROME_CONFIG_HOME`, then `XDG_CONFIG_HOME`, then `~/.config` + to locate that directory. artifacts_dir : Union[pathlib.Path, str, None] If specified, browser artifacts (such as traces and downloads) are saved into this directory. headers : Union[Dict[str, str], None] Additional HTTP headers to be sent with connect request. Optional. + + When `endpoint_url` is an extension URL such as `extension://chrome`, the headers are not sent. They are the + options of the connection: + - `x-playwright-extension-token` - the token of the extension. With the token, the user does not have to approve + the connection. Without the token, the user must approve the connection before `timeout` expires, so set it to + a value that gives the user enough time. + - `x-playwright-extension-client-name` - the name of the client that the extension shows to the user. + - `x-playwright-extension-tab-group` - pass `false` to keep the tabs out of a tab group. + - `x-playwright-extension-tab-group-title` - the title of the tab group. + - `x-playwright-extension-tab-group-color` - the color of the tab group: `green`, `blue`, `purple`, `orange`, + `pink`, `cyan`, `yellow` or `red`. + - `x-playwright-extension-background` - pass `true` to keep the tabs from becoming active. is_local : Union[bool, None] Tells Playwright that it runs on the same host as the CDP server. It will enable certain optimizations that rely upon the file system being the same between Playwright and the Browser. diff --git a/tests/async/library/chromium/test_connect_over_cdp.py b/tests/async/library/chromium/test_connect_over_cdp.py index 86df23e9c..316995599 100644 --- a/tests/async/library/chromium/test_connect_over_cdp.py +++ b/tests/async/library/chromium/test_connect_over_cdp.py @@ -19,6 +19,7 @@ import os import signal import sys +import urllib.parse import urllib.request from contextlib import suppress from pathlib import Path @@ -105,6 +106,13 @@ async def test_CD_001_should_connect_to_an_existing_cdp_session( assert len(contexts) == 1 await cdp_browser.close() + url_browser = await browser_type.connect_over_cdp( + "remote-debugging://chromium?" + + urllib.parse.urlencode({"userDataDir": str(user_data_dir)}) + ) + assert len(url_browser.contexts) == 1 + await url_browser.close() + if is_linux: environments: List[Dict[str, Optional[str]]] = [ { diff --git a/tests/async/library/test_browsertype_connect.py b/tests/async/library/test_browsertype_connect.py index 3d1214159..c551cbc5f 100644 --- a/tests/async/library/test_browsertype_connect.py +++ b/tests/async/library/test_browsertype_connect.py @@ -52,6 +52,7 @@ from tests.helpers.remote_server import ( KIND_LAUNCH_SERVER, KIND_RUN_SERVER, + ChildProcess, RemoteServerStarter, RunServer, redirect_port_forwarding_for_test, @@ -65,6 +66,21 @@ KINDS = [KIND_LAUNCH_SERVER, KIND_RUN_SERVER] +@pytest.fixture +def child_process() -> Generator[Callable[..., ChildProcess], None, None]: + """Port of the childProcess fixture in tests/config/commonFixtures.ts.""" + processes: List[ChildProcess] = [] + + def start(command: List[str]) -> ChildProcess: + process = ChildProcess(command) + processes.append(process) + return process + + yield start + for process in processes: + process.kill() + + async def connect_browser( browser_type: BrowserType, launch_arguments: Dict, @@ -1823,6 +1839,18 @@ async def test_BT_093_should_refuse_connecting_when_versions_do_not_match( assert "client version: v" + client_version() in exc_info.value.message +async def test_BT_105_run_server_should_exit_when_stdin_closes( + child_process: Callable[..., ChildProcess], +) -> None: + node, cli = compute_driver_executable() + server = child_process([node, cli, "run-server", "--port=0"]) + await asyncio.to_thread(server.wait_for_output, "Listening on ") + assert server.process.stdin + server.process.stdin.close() + # Python: a zero return code means exitCode: 0 and signal: null. + assert await asyncio.to_thread(server.exit_code) == 0 + + async def test_BT_094_should_filter_local_paths_from_unsafe_launch_options( connect: Callable[..., Awaitable[Browser]], start_remote_server: RemoteServerStarter, diff --git a/tests/async/library/test_har.py b/tests/async/library/test_har.py index 6196d9455..c41b6372c 100644 --- a/tests/async/library/test_har.py +++ b/tests/async/library/test_har.py @@ -149,8 +149,15 @@ async def test_HA_003_should_have_pages(page_with_har: PageWithHar) -> None: await har.page.goto("data:text/html,Hello") # For data: load comes before domcontentloaded... await har.page.wait_for_load_state("domcontentloaded") + + page_with_dialog = await har.page.context.new_page() + async with page_with_dialog.expect_event("dialog"): + await page_with_dialog.goto( + 'data:text/html,', wait_until="commit" + ) + log = await har.get_log() - assert len(log["pages"]) == 1 + assert len(log["pages"]) == 2 page_entry = log["pages"][0] assert page_entry["id"] assert page_entry["title"] == "Hello" diff --git a/tests/sync/library/chromium/test_connect_over_cdp.py b/tests/sync/library/chromium/test_connect_over_cdp.py index d4fe3d2a6..8c9208000 100644 --- a/tests/sync/library/chromium/test_connect_over_cdp.py +++ b/tests/sync/library/chromium/test_connect_over_cdp.py @@ -19,6 +19,7 @@ import signal import sys import time +import urllib.parse import urllib.request from concurrent.futures import ThreadPoolExecutor from contextlib import suppress @@ -117,6 +118,13 @@ def test_CD_001_should_connect_to_an_existing_cdp_session( assert len(contexts) == 1 cdp_browser.close() + url_browser = browser_type.connect_over_cdp( + "remote-debugging://chromium?" + + urllib.parse.urlencode({"userDataDir": str(user_data_dir)}) + ) + assert len(url_browser.contexts) == 1 + url_browser.close() + if is_linux: environments: List[Dict[str, Optional[str]]] = [ { diff --git a/tests/sync/library/test_browsertype_connect.py b/tests/sync/library/test_browsertype_connect.py index f3e20e0cf..637e20ae4 100644 --- a/tests/sync/library/test_browsertype_connect.py +++ b/tests/sync/library/test_browsertype_connect.py @@ -45,6 +45,7 @@ from tests.helpers.remote_server import ( KIND_LAUNCH_SERVER, KIND_RUN_SERVER, + ChildProcess, RemoteServerStarter, RunServer, redirect_port_forwarding_for_test, @@ -58,6 +59,21 @@ KINDS = [KIND_LAUNCH_SERVER, KIND_RUN_SERVER] +@pytest.fixture +def child_process() -> Generator[Callable[..., ChildProcess], None, None]: + """Port of the childProcess fixture in tests/config/commonFixtures.ts.""" + processes: List[ChildProcess] = [] + + def start(command: List[str]) -> ChildProcess: + process = ChildProcess(command) + processes.append(process) + return process + + yield start + for process in processes: + process.kill() + + def connect_browser( browser_type: BrowserType, launch_arguments: Dict, @@ -1848,6 +1864,18 @@ def test_BT_093_should_refuse_connecting_when_versions_do_not_match( assert "client version: v" + client_version() in exc_info.value.message +def test_BT_105_run_server_should_exit_when_stdin_closes( + child_process: Callable[..., ChildProcess], +) -> None: + node, cli = compute_driver_executable() + server = child_process([node, cli, "run-server", "--port=0"]) + server.wait_for_output("Listening on ") + assert server.process.stdin + server.process.stdin.close() + # Python: a zero return code means exitCode: 0 and signal: null. + assert server.exit_code() == 0 + + def test_BT_094_should_filter_local_paths_from_unsafe_launch_options( connect: Callable[..., Browser], start_remote_server: RemoteServerStarter, diff --git a/tests/sync/library/test_har.py b/tests/sync/library/test_har.py index 19d352804..e82ab6476 100644 --- a/tests/sync/library/test_har.py +++ b/tests/sync/library/test_har.py @@ -148,8 +148,15 @@ def test_HA_003_should_have_pages(page_with_har: PageWithHar) -> None: har.page.goto("data:text/html,Hello") # For data: load comes before domcontentloaded... har.page.wait_for_load_state("domcontentloaded") + + page_with_dialog = har.page.context.new_page() + with page_with_dialog.expect_event("dialog"): + page_with_dialog.goto( + 'data:text/html,', wait_until="commit" + ) + log = har.get_log() - assert len(log["pages"]) == 1 + assert len(log["pages"]) == 2 page_entry = log["pages"][0] assert page_entry["id"] assert page_entry["title"] == "Hello" diff --git a/tests/upstream_registry.md b/tests/upstream_registry.md index 8f1893320..ffc0fa9b7 100644 --- a/tests/upstream_registry.md +++ b/tests/upstream_registry.md @@ -607,6 +607,7 @@ subsystem, add a row to the Subsystems table. Then update this file: | `BT-102` | `library/browsertype-launch-selenium.spec.ts` | selenium grid 3.141.59 standalone non-chromium | | `BT-103` | `library/browsertype-launch-selenium.spec.ts` | selenium grid 3.141.59 standalone chromium through run-driver | | `BT-104` | `library/browsertype-launch-server.spec.ts` | launch server › should handle timeout | +| `BT-105` | `library/browsertype-connect.spec.ts` | run-server should exit when stdin closes | ### BU — Before unload