Notes
How it works
The loop is simple. The first run saves a screenshot as the baseline, which a person reviews and commits. Every later run takes a new screenshot and compares it with the baseline. If they match, the test passes. If not, it fails and saves three images: expected, actual and a diff with changes highlighted. A person then decides: a bug to fix, or an intended change, in which case the baseline is updated.
- 1 first run: save a baseline (reviewed by a person)
- 2 every later run: take a new screenshot, compare
- 3 same → pass
- 4 different → fail, with expected, actual and diff
- 5 human decides: bug, or intended → update baseline
In Playwright
In Playwright, the assertion is to have screenshot, on the whole page or on one element, here ShopRung's main area. First wait until all twelve product cards are there, so the page is complete. The mask option covers parts you do not want compared. Our first run failed on purpose with a snapshot doesn't exist, writing actual, which created the baseline. The second run passed.
await page.goto("/");
await expect(page.getByTestId(
"product-card")).toHaveCount(12);
await expect(page.locator("main"))
.toHaveScreenshot("grid.png", {
mask: [page.getByTestId(
"result-count")] });
run 1: "A snapshot doesn't
exist ... writing actual"
run 2: 1 passed A real diff
Then we changed the Testing Field Guide's price from twenty four ninety nine to two hundred forty nine ninety, with a network route. The screenshot test failed: ninety nine pixels different, out of about eight hundred thousand, roughly one hundredth of one percent of the image. The report rounds that ratio up to zero point zero one. The diff image greys out everything and highlights just the price. A ten times price error, caught by a test that never mentioned prices.
route: items[1].price = "$249.90" (was $24.99) toHaveScreenshot failed 99 pixels (ratio 0.01 of all image pixels) are different Expected: grid-linux.png Diff: grid-diff.png the diff highlights only the Testing Field Guide price
Thresholds
Tolerance settings exist to absorb tiny rendering noise: max diff pixels, max diff pixel ratio, and threshold, which is how different one pixel's colour may be. But look at our bug: about one hundredth of a percent of the image. We tried a max diff pixel ratio of zero point zero zero one, a tenth of a percent, and the ten times price error passed. So keep tolerances tight, make rendering stable instead, and prefer smaller element screenshots, where one wrong price is a big share of the pixels.
maxDiffPixels: 50
maxDiffPixelRatio: 0.02
threshold: 0.2 (per-pixel
colour difference, 0-1)
our price bug: 99 px, 0.012%
→ maxDiffPixelRatio 0.001
PASSED it (we tried)
prefer: stable rendering +
small, element-level shots Stable rendering
Most visual test pain is false failures. Fonts render differently on Linux and macOS, which is why Playwright names the file grid linux dot png; generate baselines in the same Docker image CI uses, not on laptops. Reset data so content is fixed. Mask dates, counters, ads, avatars and live data. Disable animations, wait for fonts and images, and fix the viewport size.
- baselines per OS/browser (grid-linux.png ≠ macOS)
- generate in CI or Docker, not on laptops
- fixed test data (reset)
- mask dates, counts, ads, avatars, live data
- animations: "disabled"
- wait for fonts and images
- fixed viewport size
Updating baselines
When a change is intended, such as a redesign, update the baselines with the update snapshots flag, and commit the new images in the same pull request as the UI change, so reviewers see the image diff next to the code. Never update all snapshots blindly just to make CI green; that turns visual testing into a rubber stamp and hides exactly the bugs it exists to catch.
npx playwright test --update-snapshots commit the new PNGs in the same PR as the UI change reviewers look at the image diff in the PR never: update all snapshots blindly to make CI green
Tools
Tools. Playwright's built in comparison is free and good for many teams. With Selenium, you take screenshots and compare them with an image diff library. Hosted visual testing services add review dashboards, comparisons across many browsers, and smarter diffing that ignores rendering noise while flagging meaningful change. Teams with component libraries often run visual tests per component in Storybook.
- built in: Playwright toHaveScreenshot
- Selenium: screenshots + an image diff library
- hosted services (Applitools, Percy, Chromatic...): review UIs, smarter "visual AI" diffs, many browsers
- Storybook: test components one by one
What to cover
Use visual tests where appearance matters most: key pages like home, product, cart and checkout; shared components such as the header, buttons and forms; responsive breakpoints; light and dark themes; and generated emails or PDFs. Do not screenshot every state of every page: each baseline is something to maintain, and a suite of hundreds of noisy images gets ignored.
- key pages: home, product, cart, checkout
- shared components: header, buttons, forms
- responsive breakpoints
- themes: light and dark
- emails and PDFs
- not: every state of every page (maintenance cost)
Common mistakes
Visual testing mistakes. Baselines created on laptops while CI runs Linux. Dynamic content left unmasked. Loose thresholds added to stop flakiness, which also hide real bugs, as our ninety nine pixel price showed. Full page screenshots of everything. Blindly updating snapshots. And diffs that no human ever looks at.
- baselines from laptops
- dynamic content not masked
- loose thresholds "to stop flakiness"
- full-page shots of everything
- blind --update-snapshots
- no human ever looks at the diffs
Say it like this
In the interview: I compare screenshots against reviewed baselines, for example with Playwright's to have screenshot, on key pages and shared components. Baselines are generated in the same Docker image as CI, data is fixed, dynamic parts are masked and animations disabled. I keep tolerances tight, because a diff of a few dozen pixels can be a serious bug, and baseline updates go in the same pull request, reviewed as image diffs.
- "Screenshot comparison against
- reviewed baselines, e.g.
- toHaveScreenshot, on key pages
- and components. Baselines are made in the CI Docker image;
- data is fixed, dynamic parts
- masked, animations off.
- Tolerances stay tight; updates
- go in the same PR and are reviewed as image diffs."