Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to overwrite browser_context_args using a marker #146

Merged
merged 1 commit into from
Jun 19, 2023
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
9 changes: 9 additions & 0 deletions pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down
24 changes: 24 additions & 0 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down