pytest-playwright
pytest-playwright is a Pytest plugin that provides robust fixtures for Playwright, enabling efficient end-to-end web testing in Python. It automatically manages browser instances, contexts, and pages, integrating seamlessly with the pytest testing framework. As of version 0.7.2, it supports Playwright Python 1.39.0 and higher, with new versions typically released in sync with Playwright's own update cadence.
Warnings
- gotcha Playwright requires browser binaries to be installed separately. Tests will fail with 'Playwright was not installed. Please run `playwright install`' if browsers are missing.
- gotcha By default, `page`, `browser`, and `context` fixtures are created and torn down for *each* test function. While ensuring isolation, this can be slow for large test suites.
- gotcha Playwright runs browsers in headless mode by default (no visible UI). This can make debugging visual issues difficult.
- gotcha pytest-playwright provides synchronous Playwright API fixtures (e.g., `page`, `browser`) by default. If your tests require `async`/`await` patterns, you must explicitly use the asynchronous fixtures (e.g., `apage`, `abrowser`).
- breaking pytest-playwright has strict version compatibility requirements with the `playwright` library. Installing incompatible versions can lead to runtime errors or unexpected behavior due to API mismatches.
Install
-
pip install pytest-playwright -
playwright install
Imports
- page
def test_example(page):
- Page (for type hinting)
from playwright.sync_api import Page
Quickstart
import pytest
def test_example_is_working(page):
page.goto("https://www.google.com")
assert "Google" in page.title()
page.screenshot(path="screenshot.png")
# To run this test file:
# 1. Save as e.g., `test_my_app.py`
# 2. Ensure browsers are installed: `playwright install`
# 3. Run: `pytest test_my_app.py`