Compares screenshots between two environments to catch visual changes.
npm install
npx playwright install chromiumThe base url is passed in as an argument when running the test suite.
The slugs to test are defined in tests/visual/pages.js
Pages with a YouTube iframe also need their slug added to IFRAME_SLUGS in tests/visual/visual-diff.spec.js.
The screen sizes to test are defined in tests/visual/pages.js.
The helpers.js file contains a number of functions that are meant to put the page into the right state before taking a screenshot (eg freeze animations, wait for iframes). Not all of them may be needed for every site. Some of them may need to be adjusted.
On first run, always use update-snapshots, otherwise Playwright will report an error due to missing baseline screenshots.
Use update-snapshots every time you require overwriting the original screenshots.
BASE_URL=https://live.site/ npm run test:update-snapshotsPlaywright writes the .png files to tests/visual/snapshots/. Commit that folder to version control.
(e.g. a visual change has been approved):
BASE_URL=https://... npm run test:update-snapshotsBASE_URL=https://feature-branch.site/ npm testPlaywright diffs every screenshot against the saved baseline.
When a test fails, three images are saved to test-results/: *-expected.png (baseline), *-actual.png (new screenshot), and *-diff.png (pixel differences).
The HTML report shows all three with a side-by-side and a slider tool to compare them.
npm run test:reportNote that these reports get overwritten every time you run the test again. To preserve a report:
- rename
./playwright-reportto something else. when running tests again, this directory will just get re-created. - to retrieve this saved report, run
npx playwright show-report name-of-backed-up-playwright-report
Adjust maxDiffPixelRatio and threshold in playwright.config.js if you get false positives from antialiasing differences between environments.