Sub topic 15
Automating Screenshot Comparisons
This chapter assumes you can read basic JavaScript or TypeScript, have Node.js installed, and have finished the chapters on responsive breakpoints and on overlay comparison. Automation does not replace a manual pass. It stops regressions, meaning changes that break something that already matched the design, from slipping back in after you have fixed them.
How screenshot testing works
A visual regression test takes a screenshot of the page and compares it with a stored baseline image. The baseline is a screenshot you have already reviewed and accepted as matching the design. If the two images differ by more than a set tolerance, the test fails and the tool saves a diff image that highlights the changed pixels.
A Playwright test at three widths
Playwright is a browser automation tool with built-in screenshot comparison. The test below checks the home page at a phone, tablet and desktop width.
import { test, expect } from '@playwright/test';
const widths = [375, 768, 1280];
for (const width of widths) {
test(`home page matches baseline at ${width}px`, async ({ page }) => {
await page.setViewportSize({ width, height: 900 });
await page.goto('https://example.com/');
await expect(page).toHaveScreenshot(`home-${width}.png`, {
fullPage: true,
maxDiffPixelRatio: 0.01,
});
});
}The loop creates one test per width. fullPage: true captures the whole scrolling page, not only the visible part. maxDiffPixelRatio: 0.01 lets up to 1% of pixels differ before the test fails. That tolerance absorbs small anti-aliasing differences, which are tiny variations in how edges of text and shapes are smoothed.
Creating and updating baselines
The first run has no baseline, so it fails and writes the screenshots it captured as the new baselines. Open each image and compare it with the design before you trust it. A baseline of a page that is already wrong will pass forever.
After a deliberate design change, regenerate the baselines and review the changed images in version control:
npx playwright test --update-snapshotsCommon causes of unstable tests
Most failures that are not real bugs come from the page changing between runs.
| Cause | Typical fix |
|---|---|
| Web fonts not loaded when the screenshot is taken | Wait for the page to load fully, or wait for fonts to be ready |
| Animations and transitions | Playwright can disable them with the `animations: "disabled"` option |
| Dates, ads, carousels and other changing content | Mock the data, or hide the area with the `mask` option |
| Different operating systems render text differently | Create and run baselines in the same environment, such as one CI container |
Baselines made on your laptop will often fail on a CI server, which is the machine that runs tests automatically after each commit. Generate the baselines in that same environment.
What automation cannot tell you
A screenshot test reports that something changed. It does not report whether the page matches the design, because the baseline is only as correct as the page was when you saved it. It also does not judge severity. Use the triage steps from the later chapter to decide which failures matter.
Exercise
Your screenshot test passes on your laptop but fails on the CI server, and the diff image shows slightly different text edges everywhere. What is the likely cause, and what is the fix?
Show answer
The two machines render fonts differently, because the operating system or font settings differ. Generate and run the baselines in the same environment, for example the CI container, instead of raising the tolerance.
Exercise
Why must you review a new baseline against the design before committing it?
Show answer
The test only compares the page with the baseline. If the baseline was captured from a page that already differs from the design, the test will pass while the page stays wrong.