On this pageThe rule: one renderer for baselines and checks
Visual regression tests compare screenshots of your pages against approved baselines and fail when pixels change. They catch what unit tests cannot: a CSS refactor that shifts a button, a font that stopped loading, a dark mode that lost its contrast. Their reputation for flakiness nearly always has one cause, a baseline rendered on one machine and compared on another. This guide is built around a single rule that removes it, then covers the tests, the CI job and the triage habits that keep the suite trustworthy. It uses Playwright's built-in screenshot assertions.
The rule: one renderer for baselines and checks
Playwright's documentation is blunt about rendering: it varies with the host operating system, its version and settings, the hardware, the power source and headless mode. Fonts fall back differently, anti-aliasing changes across graphics stacks, and each browser release can move a pixel or two. You cannot make two different machines render identically, so do not try. Render baselines and comparisons in the same pinned container image, on Linux, everywhere: on your laptop when you update baselines, and on the runner when CI checks them.
Playwright even encodes the platform in file names. A test with the screenshot name pricing.png in a project called chromium produces pricing-chromium-linux.png on Linux and pricing-chromium-darwin.png on a Mac. A baseline recorded in your Mac's own browser is simply not used on a Linux runner, which is the most common way teams end up with a suite that passes for one person and fails for everyone else.
Build the pinned renderer
The official Playwright image contains the browsers that match one exact library version. Pin both to the same number:
npm install --save-dev @playwright/test@1.64.1
docker run --rm --init --ipc=host \
--user "$(id -u):$(id -g)" -e HOME=/tmp \
-v "$PWD":/work -w /work \
mcr.microsoft.com/playwright:v1.64.1-noble \
sh -c "npm ci && npx playwright test --update-snapshots"
If the image tag and the npm version differ, the browsers inside the image will not be the ones your test runner expects. --ipc=host is recommended for Chromium, which can otherwise run out of shared memory and crash, and --init avoids zombie processes. Running as your own user ID keeps generated files owned by you, and HOME=/tmp gives npm a writable home inside the container. Commit the new PNG files and review them in the pull request like any other change.
Write tests that hold still
A single renderer removes machine noise. The page itself can still move, so fix everything that varies inside the browser: viewport, locale, time zone and the server under test. A configuration for a site served locally by your build tool:
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './visual',
workers: 1,
reporter: [['html', { open: 'never' }]],
use: {
baseURL: 'http://127.0.0.1:4173',
locale: 'en-US',
timezoneId: 'UTC',
},
expect: {
toHaveScreenshot: { maxDiffPixelRatio: 0.01 },
},
webServer: {
command: 'npm run preview -- --host 127.0.0.1 --port 4173',
url: 'http://127.0.0.1:4173',
reuseExistingServer: !process.env.CI,
},
projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
});
Then a test that masks what legitimately changes and waits for fonts:
import { test, expect } from '@playwright/test';
test('pricing page', async ({ page }) => {
await page.clock.setFixedTime(new Date('2026-01-15T10:00:00Z'));
await page.goto('/pricing');
await page.evaluate(async () => { await document.fonts.ready; });
await expect(page).toHaveScreenshot('pricing.png', {
fullPage: true,
mask: [page.getByTestId('live-clock')],
});
});
- Animations and carets are handled for you:
toHaveScreenshot()disables CSS animations and hides the text caret by default. - Clocks are frozen by the clock API before the page loads, so relative dates and countdowns render the same on every run. It only controls the browser; dates computed by your server need fixtures or a test mode.
- Dynamic regions such as a live clock, an avatar or an ad slot get a
mask, which paints a solid box in both baseline and actual. For larger areas, thestylePathoption applies a stylesheet during the screenshot, for example to hide embedded iframes. - Fonts are awaited through
document.fonts.ready, which avoids the classic screenshot of fallback fonts taken a few milliseconds too early. - Data comes from fixed fixtures. A page that shows "3 minutes ago" will never match twice.
Run it on your own runner
A self-hosted runner with Docker runs exactly the same image. In GitHub Actions:
name: visual
on: [pull_request]
jobs:
screenshots:
runs-on: [self-hosted, linux]
steps:
- uses: actions/checkout@v6
- name: Visual tests in the pinned Playwright image
run: |
docker run --rm --init --ipc=host \
--user "$(id -u):$(id -g)" -e HOME=/tmp -e CI=1 \
-v "$PWD":/work -w /work \
mcr.microsoft.com/playwright:v1.64.1-noble \
sh -c "npm ci && npx playwright test"
- uses: actions/upload-artifact@v7
if: failure()
with:
name: playwright-report
path: playwright-report/
When a test fails, the uploaded HTML report shows expected, actual and diff images side by side. The same docker run step works unchanged in Forgejo or Gitea Actions on a runner with Docker access. In GitLab CI, set the Playwright image as the job's image and run the two npm commands directly.
Keep this runner for trusted code. Its account needs Docker access, which is root-equivalent on the host, so do not attach it to public repositories where forks can submit pull requests.
Triage a failing diff
Open the HTML report and look at the diff image before touching anything. Then sort the failure into one of three cases:
- An intended change. Update the baselines in the same branch and let reviewers approve the new images.
- A real regression. Fix the CSS or markup; the test did its job.
- Noise. Find the source, usually a font, an animation, live data or a time value, and remove it with masking, fixtures or the clock API.
Resist raising the threshold to make noise go away. A maxDiffPixelRatio of 1% absorbs rendering noise; each increase beyond that lowers the size of regression the suite can catch, and the noise tends to come back slightly larger. Two habits keep failures readable: start with one browser and one viewport, and prefer screenshots of a component or section over full pages when only part of a page matters. Run --update-snapshots only in a branch whose visual change you intend, never as an automatic CI step.
Capacity planning for headless browsers
Headless Chromium is the heavy part of the job. Playwright's CI guidance recommends a single worker for stability and reproducibility; on a dedicated self-hosted machine you can raise the count once you have measured headroom. Run the suite while watching docker stats to see peak memory per worker, then size the server for your worker count plus the app under test. Images, browsers and npm caches also take disk space, so keep an eye on docker system df. CloudSat dev and test servers bill by the hour, so a runner can be resized or rebuilt as the suite grows without a long commitment.
FAQ
Should I use BackstopJS instead?
BackstopJS is the main alternative, configured through a scenario file rather than test code, with a --docker mode for consistent rendering. Its maintainers have asked for a new owner, so check its activity before adopting it for a long-lived project. If you already write Playwright tests, the built-in assertions avoid a second tool.
Why do baselines from my laptop keep failing in CI?
They were rendered by a different browser build on a different operating system, and on a Mac they carry a different platform suffix. Regenerate them with the pinned container command above and the noise disappears.
Should I test Firefox and WebKit too?
Eventually, if your users rely on them. Each browser needs its own baselines and adds run time, so add them after the Chromium suite is stable.
How do I review intended changes?
Update the baselines in a branch, commit the PNG files, and review them in the pull request. Most Git platforms show image diffs, and the HTML report gives a side-by-side view.
Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.




