From b1bb4381ac90c600c2c08de95514a03693afafa0 Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Thu, 21 Jan 2021 22:39:58 +0100 Subject: [PATCH] chore: upgrade to new Playwright with snake case (#37) --- README.md | 15 +++++++-------- pytest_playwright/pytest_playwright.py | 7 +++---- setup.py | 2 +- tests/test_playwright.py | 10 +++++----- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 7b316fe..828d14a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Use the `page` fixture to write a basic test. See [more examples](#examples). ```py def test_example_is_working(page): page.goto("https://example.com") - assert page.innerText('h1') == 'Example Domain' + assert page.inner_text('h1') == 'Example Domain' page.click("text=More information") ``` @@ -44,7 +44,6 @@ If you want to add the CLI arguments automatically without specifying them, you [pytest] # Run firefox with UI addopts = --headful --browser firefox - ``` ## Fixtures @@ -59,8 +58,8 @@ def test_my_app_is_working(fixture_name): **Function scope**: These fixtures are created when requested in a test function and destroyed when the test ends. -- `context`: New [browser context](https://playwright.dev/#path=docs%2Fcore-concepts.md&q=browser-contexts) for a test. -- `page`: New [browser page](https://playwright.dev/#path=docs%2Fcore-concepts.md&q=pages-and-frames) for a test. +- `context`: New [browser context](https://playwright.dev/python/docs/core-concepts#browser-contexts) for a test. +- `page`: New [browser page](https://playwright.dev/python/docs/core-concepts#pages-and-frames) for a test. **Session scope**: These fixtures are created when requested in a test function and destroyed when all tests end. @@ -70,8 +69,8 @@ def test_my_app_is_working(fixture_name): **Customizing fixture options**: For `browser` and `context` fixtures, use the the following fixtures to define custom launch options. -- `browser_type_launch_args`: Override launch arguments for [`browserType.launch()`](https://playwright.dev/#path=docs%2Fapi.md&q=browsertypelaunchoptions). It should return a Dict. -- `browser_context_args`: Override the options for [`browser.newContext()`](https://playwright.dev/#path=docs%2Fapi.md&q=browsernewcontextoptions). It should return a Dict. +- `browser_type_launch_args`: Override launch arguments for [`browserType.launch()`](https://playwright.dev/python/docs/api/class-browsertype#browser_typelaunchoptions). It should return a Dict. +- `browser_context_args`: Override the options for [`browser.new_context()`](https://playwright.dev/python/docs/api/class-browser#browsernew_contextoptions). It should return a Dict. ## Examples @@ -132,7 +131,7 @@ import pytest def browser_context_args(browser_context_args): return { **browser_context_args, - "ignoreHTTPSErrors": True + "ignore_https_errors": True } ``` @@ -205,7 +204,7 @@ def pytest_runtest_makereport(item, call) -> None: ## Deploy to CI -Use the [Playwright GitHub Action](https://github.com/microsoft/playwright-github-action) or [guides for other CI providers](https://playwright.dev/#path=docs%2Fci.md&q=) to deploy your tests to CI/CD +Use the [Playwright GitHub Action](https://github.com/microsoft/playwright-github-action) or [guides for other CI providers](https://playwright.dev/python/docs/ci) to deploy your tests to CI/CD ## Special thanks diff --git a/pytest_playwright/pytest_playwright.py b/pytest_playwright/pytest_playwright.py index 2e69441..329d42c 100644 --- a/pytest_playwright/pytest_playwright.py +++ b/pytest_playwright/pytest_playwright.py @@ -18,8 +18,7 @@ import pytest -from playwright import sync_playwright -from playwright.sync_api import Browser, BrowserContext, Page +from playwright.sync_api import sync_playwright, Browser, BrowserContext, Page def pytest_generate_tests(metafunc: Any) -> None: @@ -126,7 +125,7 @@ def browser(launch_browser: Callable[[], Browser]) -> Generator[Browser, None, N def context( browser: Browser, browser_context_args: Dict ) -> Generator[BrowserContext, None, None]: - context = browser.newContext(**browser_context_args) + context = browser.new_context(**browser_context_args) yield context context.close() @@ -142,7 +141,7 @@ def _handle_page_goto( @pytest.fixture def page(context: BrowserContext, base_url: str) -> Generator[Page, None, None]: - page = context.newPage() + page = context.new_page() page._goto = page.goto # type: ignore page.goto = lambda *args, **kwargs: _handle_page_goto( # type: ignore page, list(args), kwargs, base_url diff --git a/setup.py b/setup.py index 371077d..5024584 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ url="https://github.com/microsoft/playwright-pytest", packages=["pytest_playwright"], include_package_data=True, - install_requires=["playwright>=0.170.0", "pytest", "pytest-base-url"], + install_requires=["playwright==1.8.0a1", "pytest", "pytest-base-url"], entry_points={"pytest11": ["playwright = pytest_playwright.pytest_playwright"]}, classifiers=[ "Programming Language :: Python :: 3", diff --git a/tests/test_playwright.py b/tests/test_playwright.py index 3de019a..3bc7b98 100644 --- a/tests/test_playwright.py +++ b/tests/test_playwright.py @@ -24,8 +24,8 @@ def test_default(page, browser_name): assert browser_name == "chromium" user_agent = page.evaluate("window.navigator.userAgent") assert "HeadlessChrome" in user_agent - page.setContent('bar') - assert page.querySelector("#foo") + page.set_content('bar') + assert page.query_selector("#foo") """ ) result = testdir.runpytest() @@ -36,8 +36,8 @@ def test_multiple_browsers(testdir: Any) -> None: testdir.makepyfile( """ def test_multiple_browsers(page): - page.setContent('bar') - assert page.querySelector("#foo") + page.set_content('bar') + assert page.query_selector("#foo") """ ) result = testdir.runpytest( @@ -53,7 +53,7 @@ def test_browser_context_args(testdir: Any) -> None: @pytest.fixture(scope="session") def browser_context_args(): - return {"userAgent": "foobar"} + return {"user_agent": "foobar"} """ ) testdir.makepyfile(