Skip to content

iloveitaly/pytest-playwright-artifacts

Repository files navigation

# pytest-playwright-artifacts Pytest plugin for enhanced Playwright testing with automatic artifact capture and console logging. ## Features - **Automatic Console Logging**: Captures and logs all console messages from Playwright pages during tests - **Failure Artifacts**: On test failure, automatically saves: - Rendered page HTML (`failure.html`) - Full-page screenshot (`screenshot.png`) - Console logs (`console_logs.log`) - Concise failure summary (`failure.txt`) - **Console Error Assertion**: Provides `assert_no_console_errors()` helper to fail tests if any error-level console logs are detected - **Configurable Filtering**: Filter out noisy console messages using regex patterns ## Installation ```bash pip install pytest-playwright-artifacts ``` This plugin requires `pytest-playwright` to be installed and configured: ```bash pip install pytest-playwright playwright install ``` ## Usage The plugin activates automatically once installed. No configuration needed for basic usage. ### Automatic Console Logging All console messages from Playwright pages are automatically captured and logged: ```python def test_my_app(page): page.goto("https://example.com") # All console.log, console.warn, console.error messages are captured ``` ### Failure Artifacts When a test fails, artifacts are automatically saved to `test-results//`: ``` test-results/ � tests-test_file-py-test_name/ � failure.html # Rendered page HTML � screenshot.png # Full-page screenshot � console_logs.log # All captured console logs � failure.txt # Failure summary ``` ### Assert No Console Errors Use the `assert_no_console_errors` helper to ensure no JavaScript errors occurred: ```python from pytest_playwright_artifacts import assert_no_console_errors def test_no_js_errors(page, request): page.goto("https://example.com") page.click("#some-button") # Fails if any console.error() messages were logged assert_no_console_errors(request) ``` ### Filtering Console Messages Filter out noisy console messages using regex patterns in `pytest.ini`: ```ini [pytest] playwright_console_ignore = Invalid Sentry Dsn:.* Radar SDK: initialized.* \[Meta Pixel\].* ``` Or in `pyproject.toml`: ```toml [tool.pytest.ini_options] playwright_console_ignore = [ "Invalid Sentry Dsn:.*", "Radar SDK: initialized.*", "\\[Meta Pixel\\].*", ] ``` Filtered messages are excluded from: - Console output - Console logs saved to artifacts - In-memory buffer used for `assert_no_console_errors()` ## Configuration ### Output Directory By default, artifacts are saved to `test-results/`. Change this with pytest's `--output` option: ```bash pytest --output=my-test-results ``` ### Disable Features The plugin is opt-out by default. To disable: **Console logging**: Edit `pytest_playwright_artifacts/plugin.py` and change `autouse=True` to `autouse=False` in the `playwright_console_logging` fixture. **Failure artifacts**: Remove or comment out the `pytest_runtest_makereport` hook. **Console error assertions**: Simply don't call `assert_no_console_errors()` in your tests. ## How It Works 1. **Console Logging**: The plugin attaches a `console` event listener to all Playwright pages, capturing messages in a structured format 2. **Filtering**: Console messages are matched against configured regex patterns and filtered early 3. **Failure Detection**: The `pytest_runtest_makereport` hook detects test failures and captures page state 4. **Artifact Creation**: On failure, HTML, screenshot, logs, and summary are written to a per-test directory ## Requirements - Python e 3.11 - pytest e 8.0 - playwright e 1.40 - pytest-playwright (for the `page` fixture) ## Development ```bash # Install dependencies uv sync # Run tests uv run pytest # Lint uv run ruff check . # Type check uv run pyright ``` ## License MIT ## Credits Created by [Michael Bianco](https://github.com/iloveitaly)

About

Pytest plugin to capture Playwright console logs and create failure artifacts (HTML, screenshots, logs)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Contributors 2

  •  
  •