How to Run Visual Regression Tests with GitHub Actions
Playwright toHaveScreenshot compares rendered pixels to a committed baseline and fails the job on a visual diff.
Use Playwright's toHaveScreenshot assertion to capture and compare screenshots against committed baselines. On a diff, the test fails; upload the report so reviewers can see the before and after.
Steps
- Commit baseline snapshots generated on a Linux runner to match CI rendering.
- Run
npx playwright testsotoHaveScreenshotassertions compare against them. - Upload the report with
if: always()to inspect any diffs.
Workflow
.github/workflows/ci.yml
jobs:
visual:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npx playwright install --with-deps chromium
- run: npx playwright test --grep @visual
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/Gotchas
- Font and rendering differ across OSes; generate baselines on the same runner OS you test on.
- Update baselines with
--update-snapshotsonly after a reviewed, intentional UI change.
Related guides
How to Run Playwright End-to-End Tests with GitHub ActionsRun Playwright end-to-end tests in GitHub Actions, installing browsers with their system dependencies and upl…
How to Run Cypress End-to-End Tests with GitHub ActionsRun Cypress end-to-end tests in GitHub Actions with cypress-io/github-action, starting your app server, waiti…