diff --git a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py index ba9ccd8..ccc1432 100644 --- a/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py +++ b/pytest-playwright-asyncio/pytest_playwright_asyncio/pytest_playwright.py @@ -507,6 +507,17 @@ def connect_options() -> Optional[Dict]: return None +def _to_remote_launch_options(launch_options: Dict) -> Dict: + # The server reads the launch options header with the protocol option + # names, e.g. slowMo, while the Python API takes slow_mo. Only the option + # names are converted, values like firefox_user_prefs are passed as is. + def to_camel_case(name: str) -> str: + first, *rest = name.split("_") + return first + "".join(word[:1].upper() + word[1:] for word in rest) + + return {to_camel_case(name): value for name, value in launch_options.items()} + + @pytest.fixture(scope="session") def launch_browser( browser_type_launch_args: Dict, @@ -521,7 +532,9 @@ async def launch(**kwargs: Dict) -> Browser: { **connect_options, "headers": { - "x-playwright-launch-options": json.dumps(launch_options), + "x-playwright-launch-options": json.dumps( + _to_remote_launch_options(launch_options) + ), **(connect_options.get("headers") or {}), }, } diff --git a/pytest-playwright/pytest_playwright/pytest_playwright.py b/pytest-playwright/pytest_playwright/pytest_playwright.py index aeb22fa..4a6a5a2 100644 --- a/pytest-playwright/pytest_playwright/pytest_playwright.py +++ b/pytest-playwright/pytest_playwright/pytest_playwright.py @@ -504,6 +504,17 @@ def connect_options() -> Optional[Dict]: return None +def _to_remote_launch_options(launch_options: Dict) -> Dict: + # The server reads the launch options header with the protocol option + # names, e.g. slowMo, while the Python API takes slow_mo. Only the option + # names are converted, values like firefox_user_prefs are passed as is. + def to_camel_case(name: str) -> str: + first, *rest = name.split("_") + return first + "".join(word[:1].upper() + word[1:] for word in rest) + + return {to_camel_case(name): value for name, value in launch_options.items()} + + @pytest.fixture(scope="session") def launch_browser( browser_type_launch_args: Dict, @@ -518,7 +529,9 @@ def launch(**kwargs: Dict) -> Browser: { **connect_options, "headers": { - "x-playwright-launch-options": json.dumps(launch_options), + "x-playwright-launch-options": json.dumps( + _to_remote_launch_options(launch_options) + ), **(connect_options.get("headers") or {}), }, } diff --git a/tests/test_asyncio.py b/tests/test_asyncio.py index 4175355..26e83a0 100644 --- a/tests/test_asyncio.py +++ b/tests/test_asyncio.py @@ -1670,11 +1670,18 @@ def connect_options(): ) testdir.makepyfile( """ + import time import pytest @pytest.mark.asyncio() - async def test_connect_options(page): + async def test_connect_options(page, pytestconfig): assert await page.evaluate("1 + 1") == 2 + await page.set_content("") + started = time.monotonic() + for _ in range(3): + await page.get_by_role("button").click() + slowmo = pytestconfig.getoption("--slowmo") + assert time.monotonic() - started >= 3 * slowmo / 1000 """ ) result = testdir.runpytest() @@ -1691,6 +1698,9 @@ async def test_connect_options(page): break result = testdir.runpytest() result.assert_outcomes(passed=1) + # Launch options are sent to the server with the protocol option names. + result = testdir.runpytest("--slowmo", "300") + result.assert_outcomes(passed=1) finally: assert server_process # TODO: Playwright CLI on Windows via Python does not forward the signal diff --git a/tests/test_sync.py b/tests/test_sync.py index 10f73e4..866967d 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -1619,8 +1619,16 @@ def connect_options(): ) testdir.makepyfile( """ - def test_connect_options(page): + import time + + def test_connect_options(page, pytestconfig): assert page.evaluate("1 + 1") == 2 + page.set_content("") + started = time.monotonic() + for _ in range(3): + page.get_by_role("button").click() + slowmo = pytestconfig.getoption("--slowmo") + assert time.monotonic() - started >= 3 * slowmo / 1000 """ ) result = testdir.runpytest() @@ -1636,6 +1644,9 @@ def test_connect_options(page): break result = testdir.runpytest() result.assert_outcomes(passed=1) + # Launch options are sent to the server with the protocol option names. + result = testdir.runpytest("--slowmo", "300") + result.assert_outcomes(passed=1) finally: assert server_process # TODO: Playwright CLI on Windows via Python does not forward the signal