diff --git a/pytest_playwright/pytest_playwright.py b/pytest_playwright/pytest_playwright.py index b6c661a..e1d4ecf 100644 --- a/pytest_playwright/pytest_playwright.py +++ b/pytest_playwright/pytest_playwright.py @@ -59,6 +59,10 @@ def pytest_configure(config: Any) -> None: config.addinivalue_line( "markers", "only_browser(name): mark test to run only on a specific browser" ) + config.addinivalue_line( + "markers", + "browser_context_args(**kwargs): provide additional arguments to browser.new_context()", + ) # Making test result information available in fixtures @@ -206,6 +210,11 @@ def context( request: pytest.FixtureRequest, ) -> Generator[BrowserContext, None, None]: pages: List[Page] = [] + + context_args_marker = next(request.node.iter_markers("browser_context_args"), None) + additional_context_args = context_args_marker.kwargs if context_args_marker else {} + browser_context_args.update(additional_context_args) + context = browser.new_context(**browser_context_args) context.on("page", lambda page: pages.append(page)) diff --git a/tests/test_playwright.py b/tests/test_playwright.py index 254c7aa..c054ff0 100644 --- a/tests/test_playwright.py +++ b/tests/test_playwright.py @@ -170,6 +170,30 @@ def test_browser_context_args(page): result.assert_outcomes(passed=1) +def test_user_defined_browser_context_args(testdir: pytest.Testdir) -> None: + testdir.makeconftest( + """ + import pytest + + @pytest.fixture(scope="session") + def browser_context_args(): + return {"user_agent": "foobar"} + """ + ) + testdir.makepyfile( + """ + import pytest + + @pytest.mark.browser_context_args(user_agent="overwritten", locale="new-locale") + def test_browser_context_args(page): + assert page.evaluate("window.navigator.userAgent") == "overwritten" + assert page.evaluate("window.navigator.languages") == ["new-locale"] + """ + ) + result = testdir.runpytest() + result.assert_outcomes(passed=1) + + def test_chromium(testdir: pytest.Testdir) -> None: testdir.makepyfile( """