|
18 | 18 | import json |
19 | 19 | import os |
20 | 20 | import signal |
| 21 | +import sys |
21 | 22 | import urllib.request |
22 | 23 | from contextlib import suppress |
23 | 24 | from pathlib import Path |
24 | | -from typing import Any, Dict, Generator, List |
| 25 | +from typing import Any, Dict, Generator, List, Optional |
25 | 26 |
|
26 | 27 | import pytest |
27 | 28 |
|
|
35 | 36 | expect, |
36 | 37 | ) |
37 | 38 | from tests.helpers.proxy_server import ProxyServer |
38 | | -from tests.helpers.remote_server import RemoteServer |
| 39 | +from tests.helpers.remote_server import ChildProcess, RemoteServer |
39 | 40 | from tests.server import Server, TestServerRequest, WebSocketProtocol, find_free_port |
40 | 41 |
|
41 | 42 | pytestmark = pytest.mark.only_browser("chromium") |
@@ -83,16 +84,66 @@ async def start_playwright_with_env( |
83 | 84 |
|
84 | 85 |
|
85 | 86 | async def test_CD_001_should_connect_to_an_existing_cdp_session( |
86 | | - browser_type: BrowserType, launch_arguments: Dict, port: int |
| 87 | + browser_type: BrowserType, |
| 88 | + launch_arguments: Dict, |
| 89 | + tmp_path: Path, |
| 90 | + is_linux: bool, |
87 | 91 | ) -> None: |
88 | | - browser_server = await launch(browser_type, launch_arguments, port) |
| 92 | + config_home = tmp_path / "config" |
| 93 | + user_data_dir = config_home / "google-chrome" |
| 94 | + browser_server = await browser_type.launch_persistent_context( |
| 95 | + user_data_dir, **launch_arguments, args=["--remote-debugging-port=0"] |
| 96 | + ) |
89 | 97 | try: |
| 98 | + port = (user_data_dir / "DevToolsActivePort").read_text().splitlines()[0] |
90 | 99 | cdp_browser = await browser_type.connect_over_cdp( |
91 | 100 | endpoint_url=f"http://127.0.0.1:{port}/", |
92 | 101 | ) |
93 | 102 | contexts = cdp_browser.contexts |
94 | 103 | assert len(contexts) == 1 |
95 | 104 | await cdp_browser.close() |
| 105 | + |
| 106 | + if is_linux: |
| 107 | + environments: List[Dict[str, Optional[str]]] = [ |
| 108 | + { |
| 109 | + "CHROME_USER_DATA_DIR": "", |
| 110 | + "CHROME_CONFIG_HOME": "", |
| 111 | + "XDG_CONFIG_HOME": str(config_home), |
| 112 | + }, |
| 113 | + { |
| 114 | + "CHROME_USER_DATA_DIR": "", |
| 115 | + "CHROME_CONFIG_HOME": str(config_home), |
| 116 | + "XDG_CONFIG_HOME": str(tmp_path / "unused-config"), |
| 117 | + }, |
| 118 | + { |
| 119 | + "CHROME_USER_DATA_DIR": str(user_data_dir), |
| 120 | + "CHROME_CONFIG_HOME": str(tmp_path / "unused-config"), |
| 121 | + "XDG_CONFIG_HOME": str(tmp_path / "unused-xdg-config"), |
| 122 | + }, |
| 123 | + ] |
| 124 | + for env in environments: |
| 125 | + # Python: use the Python client in the child process. |
| 126 | + child = ChildProcess( |
| 127 | + [ |
| 128 | + sys.executable, |
| 129 | + "-c", |
| 130 | + """ |
| 131 | +import asyncio |
| 132 | +from playwright.async_api import async_playwright |
| 133 | +
|
| 134 | +async def main(): |
| 135 | + async with async_playwright() as playwright: |
| 136 | + browser = await playwright.chromium.connect_over_cdp('chrome') |
| 137 | + print('contexts: ' + str(len(browser.contexts))) |
| 138 | + await browser.close() |
| 139 | +
|
| 140 | +asyncio.run(main()) |
| 141 | +""", |
| 142 | + ], |
| 143 | + env=env, |
| 144 | + ) |
| 145 | + assert await asyncio.to_thread(child.exit_code) == 0, child.output |
| 146 | + assert "contexts: 1" in child.output |
96 | 147 | finally: |
97 | 148 | await browser_server.close() |
98 | 149 |
|
|
0 commit comments