Test Runners
You can use our Pytest integration to write end-to-end tests in Python.
Usage#
Use the page fixture to write a basic test. See more examples.
To run your tests, use pytest CLI.
If you want to add the CLI arguments automatically without specifying them, you can use the pytest.ini file:
Fixtures#
This plugin configures Playwright-specific fixtures for pytest. To use these fixtures, use the fixture name as an argument to the test function.
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 for browser_type.launch(**kwargs). It should return a Dict.browser_context_args: Override the options for browser.new_context(**kwargs). It should return a Dict.
Examples#
Configure Mypy typings for auto-completion#
Skip test by browser#
Run on a specific browser#
Configure base-url#
Start Pytest with the base-url argument.
Ignore HTTPS errors#
conftest.py
Use custom viewport size#
conftest.py
Device emulation#
conftest.py
Debugging#
Use with pdb#
Use the breakpoint() statement in your test code to pause execution and get a pdb REPL.
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.
Deploy to CI#
Use the Playwright GitHub Action or guides for other CI providers to deploy your tests to CI/CD