QA AND TEST AUTOMATION · PART 5 · UI WEB AUTOMATION · TECHNIQUE · 58 OF 112

Visual Regression Testing With Screenshots

Catching changes you can see but rarely assert: screenshot comparison with Playwright toHaveScreenshot, run for real on ShopRung. The first run writes a baseline, the second passes, and a price changed from $24.99 to $249.90 fails with a diff of only 99 pixels, about 0.01% of the image, which shows why loose thresholds are dangerous. Masks, stable environments, baseline updates and hosted tools.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Baseline vs new
  • diff images
  • tight tolerances
  • stable rendering and masks
  • reviewed updates
  • choose targets

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.

The loop
  • 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.

tests/vis.spec.ts
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.

Run 3 (price changed)
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.

Tolerance settings
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.

Avoiding false failures
  • 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.

When the change is right
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.

Options
  • 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.

Good targets
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is visual regression testing?

Comparing screenshots of the UI with approved baselines to detect unintended visual changes such as layout shifts, overlaps or wrong content.

Q2How do you do visual testing in Playwright?

expect(page or locator).toHaveScreenshot(name, options); the first run writes the baseline, later runs compare and save expected, actual and diff images.

Q3Why do visual tests produce false failures?

Different OS/fonts, dynamic content, animations, unloaded images and viewport differences; fix with a stable CI image, masks, fixed data and disabled animations.

Q4What is the risk of a large maxDiffPixelRatio?

It can hide real bugs that change few pixels, such as a wrong price or a missing character.

Q5How do you update visual baselines?

Run with --update-snapshots after confirming the change is intended, and commit the new images in the same PR for review.

Q6What should you mask in screenshots?

Content that legitimately changes: dates, times, counters, ads, avatars, random IDs and live data.

Q7Why are baseline names OS-specific in Playwright?

Rendering differs by platform, so snapshots are stored per OS and browser (for example grid-linux.png) and should be generated where CI runs.

Q8Where is visual testing most valuable?

Key user journeys, shared components, responsive breakpoints, themes and generated documents, rather than every state of every page.