Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {}),
},
}
Expand Down
15 changes: 14 additions & 1 deletion pytest-playwright/pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {}),
},
}
Expand Down
12 changes: 11 additions & 1 deletion tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<button>Click</button>")
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()
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<button>Click</button>")
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()
Expand All @@ -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
Expand Down
Loading