Metadata-Version: 2.4
Name: pytest-playwright-visual-snapshot
Version: 0.1.0
Summary: Easy pytest visual regression testing using playwright
Project-URL: Repository, https://github.com/iloveitaly/pytest-playwright-visual-snapshot
Author-email: Michael Bianco <mike@mikebian.co>
Keywords: playwright,pytest,regression,testing,visual
Requires-Python: >=3.10
Requires-Dist: pillow>=11.1.0
Requires-Dist: pixelmatch>=0.3.0
Requires-Dist: pytest-playwright>=0.7.0
Description-Content-Type: text/markdown

# Pytest Plugin for Visual Testing with Playwright

As of 2025-03-22 of all of the existing packages to do simple visual regression testing using playwright are long dead. I had a bunch of updates I wanted to make to existing systems so I rewrote the plugin with a bunch of updates:

- snapshots are always written on CI
- ability to mask out certain elements
- failing on `--update-snapshots` to make users manually review images
- snapshot name is optional, `test_name[browser][os].png` is auto-generated by default
- multiple snapshots in a single test, file names are auto incremented
- updated folder structure: `snapshots/file_name/test_name/test_name[browser][os].png`
- ability to configure directories, etc via ini + pytest config.

You can see this implemented in a [working project here](https://github.com/iloveitaly/python-starter-template/).

## Installation

```bash
pip install pytest-playwright-visual-snapshot
```

## Usage

This plugin provides a `assert_snapshot` fixture which is used to create snapshots and compare it.

```python
def test_myapp(page, assert_snapshot):
    page.goto("https://example.com")
    assert_snapshot(page)
```

Then, run pytest:

```bash
pytest
```

The first time you run pytest, snapshots will be created, and you will get the error:

```console
New snapshot(s) created. Please review images
```

The next run, the snapshots comparison will take place. To update snapshots, run:

```bash
pytest --update-snapshots
```

After updating, tests will fail and you will need to review images.

In case of a mismatch, `snapshot_tests_failures` folder will be created with `actual_..`, `expected_..` and `diff_..` images generated.

## Configuration

View all configuration options by running `pytest --help`. Here's a quick example:

```python
# NOTE this runs on any pytest invocation, even if no tests are run
def pytest_configure(config: Config):
  config.option.playwright_visual_snapshots_path = Path("...")
  config.option.playwright_visual_snapshot_failures_path = Path("...")
```

## API

- `threshold` - sets the threshold for the comparison of the screenshots:`0` to `1`. Default is `0.1`
<!-- - `name` - `.png` extensions only. Default is `test_name[browser][os].png` (recommended) -->
- `fail_fast` - If `True`, will fail after first different pixel. `False` by default

## Alternatives

- https://github.com/kumaraditya303/pytest-playwright-snapshot - long dead
- https://github.com/symon-storozhenko/pytest-playwright-visual - fork of the above repo, long dead
- https://github.com/Visual-Regression-Tracker/Visual-Regression-Tracker - requires a separate server to run
