Pytest plugin for Playwright 
Write end-to-end tests for your web apps with Playwright and pytest.
- Support for all modern browsers including Chromium, WebKit and Firefox.
- Support for headless and headful execution.
- Built-in fixtures that provide browser primitives to test functions.
Usage
pip install pytest-playwright
Use the page fixture to write a basic test. See more examples.
def test_example_is_working(page):
page.goto("https://example.com")
assert page.innerText('h1') == 'Example Domain'
page.click("text=More information")
To run your tests, use pytest CLI.
# Run tests (Chromium and headless by default)
pytest
# Run tests in headful mode
pytest --headful
# Run tests in a different browser (chromium, firefox, webkit)
pytest --browser firefox
# Run tests in multiple browsers
pytest --browser chromium --browser webkit
If you want to add the CLI arguments automatically without specifying them, you can use the pytest.ini file:
# content of pytest.ini
[pytest]
# Run firefox with UI
addopts = --headful --browser firefox
Fixtures
This plugin configures Playwright-specific fixtures for pytest. To use these fixtures, use the fixture name as an argument to the test function.
def test_my_app_is_working(fixture_name):
# Test using fixture_name
# ...
Function scope: These fixtures are created when requested in a test function and destroyed when the test ends.
context: New browser context for a test.page: New browser page for a test.
Session scope: These fixtures are created when requested in a test function and destroyed when all tests end.
browser: Browser instance launched by Playwright.browser_name: Browser name as string.is_chromium,is_webkit,is_firefox: Booleans for the respective browser types.
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 forbrowserType.launch(). It should return a Dict.browser_context_args: Override the options forbrowser.newContext(). It should return a Dict.
Examples
Configure Mypy typings for auto-completion
from playwright.sync_api import Page
def test_visit_admin_dashboard(page: Page):
page.goto("/admin")
# ...
Skip test by browser
import pytest
@pytest.mark.skip_browser("firefox")
def test_visit_example(page):
page.goto("https://example.com")
# ...
Run on a specific browser
import pytest
@pytest.mark.only_browser("chromium")
def test_visit_example(page):
page.goto("https://example.com")
# ...
Configure base-url
Start Pytest with the base-url argument.
pytest --base-url http://localhost:8080
def test_visit_example(page):
page.goto("/admin")
# -> Will result in http://localhost:8080/admin
Ignore HTTPS errors
conftest.py
import pytest
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
return {
**browser_context_args,
"ignoreHTTPSErrors": True
}
Use custom viewport size
conftest.py
import pytest
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
return {
**browser_context_args,
"viewport": {
"width": 1920,
"height": 1080,
}
}
Device emulation
conftest.py
import pytest
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright):
iphone_11 = playwright.devices['iPhone 11 Pro']
return {
**browser_context_args,
**iphone_11,
}
Debugging
Use with pdb
Use the breakpoint() statement in your test code to pause execution and get a pdb REPL.
def test_bing_is_working(page):
page.goto("https://bing.com")
breakpoint()
# ...
Screenshot on test failure
You can capture screenshots for failed tests with a pytest runtest hook. Add this to your conftest.py file.
Note that this snippet uses slugify to convert test names to file paths, which can be installed with pip install python-slugify.
# In conftest.py
from slugify import slugify
from pathlib import Path
def pytest_runtest_makereport(item, call) -> None:
if call.when == "call":
if call.excinfo is not None:
page = item.funcargs["page"]
screenshot_dir = Path(".playwright-screenshots")
screenshot_dir.mkdir(exist_ok=True)
page.screenshot(path=str(screenshot_dir / f"{slugify(item.nodeid)}.png"))
Deploy to CI
Use the Playwright GitHub Action or guides for other CI providers to deploy your tests to CI/CD
Special thanks
Thanks to Max Schmitt for creating and maintaining this project.
Metadata
Release files for pytest-playwright 0.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pytest-playwright-0.0.8.tar.gz | 17.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pytest_playwright-0.0.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.7 kB
Release files / pytest-playwright-0.0.8.tar.gz
| Download URL | pytest-playwright-0.0.8.tar.gz |
|---|---|
| Size | 17.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19c7dab4eebaa97d324faf4cfec2c3e2c7ad889e127346828286db86282d9b01
|
|
BLAKE2b-256 checksum How to use checksums |
d8f9111e665d00fb0a03f814706b13cfef1b57799a528cee33c392c6a033a79f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
|
Release files / pytest_playwright-0.0.8-py3-none-any.whl
| Download URL | pytest_playwright-0.0.8-py3-none-any.whl |
|---|---|
| Size | 10.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60d9b1d425525ea7205c08bcad16f52a91a9b11100f7ac02526ff2437a48b8d9
|
|
BLAKE2b-256 checksum How to use checksums |
0483072020e1669f2cebbf225c37df9192a6636ce1dc26c430f54668c9a36602
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
|