Loading WURK...
RomanMotion
RomanMotion
Product ad & launch videos · 3D product visuals · app & AI builder. Concept to final cut in days. Open for projects.

Stop eyeballing releases: catch visual bugs with screenshot diffs

Visual regression testing that isn't flaky: baselines, diff thresholds, masking dynamic content, a browser and device matrix, and a CI gate.

Published on October 22, 20267 min read

Every release, someone opens the site on their laptop, scrolls through a few pages, and says "looks fine." Then a CSS change meant for the blog shifts the checkout button under the cookie banner on iPhones, and nobody notices until sales dip. Eyeballing doesn't scale past a handful of pages, and it never covers the phone you didn't pick up.

Visual regression testing does that check for you: it takes screenshots of your pages, compares them to approved versions, and fails when something changed that you didn't approve. Done badly, it's the flakiest test you'll ever own. Done well, it catches the bugs your functional tests can't see.

Short answer: screenshot a small set of key pages and components across a deliberate browser and device matrix, freeze everything that changes on its own, keep thresholds strict, store baselines next to the code, and make an intended change something a reviewer approves in the same pull request. Here's how, with Playwright as the example.

How visual regression works

Three pieces:

  1. Baseline. An approved screenshot of a page or component, committed to the repo.
  2. Capture. On every run, the same page is rendered and screenshotted under the same conditions.
  3. Diff. The two images are compared pixel by pixel. If too many pixels differ, the test fails and you get the expected image, the actual one and a diff that highlights what changed.

The hard part isn't the comparison. It's making sure that the only reason two screenshots differ is a real change in your UI, not a clock, an ad, a font that loaded late or a different operating system.

Step 1: Pick what to screenshot

Don't screenshot everything. Two levels work well together:

  • Pages: your 5 to 10 most important ones. Home, pricing, signup, checkout, the main dashboard.
  • Components: the shared pieces that appear everywhere, like the header, nav, buttons, forms and modals. A component screenshot is smaller, more stable and points straight at the culprit.
import { test, expect } from '@playwright/test';

test('header', async ({ page }) => {
  await page.goto('/');
  await expect(page.locator('header')).toHaveScreenshot('header.png');
});

test('pricing page', async ({ page }) => {
  await page.goto('/pricing');
  await expect(page).toHaveScreenshot('pricing.png', { fullPage: true });
});

The first run writes the baselines. Review them like code before you commit them: a baseline that already contains a bug will defend that bug forever.

Step 2: Build a browser and device matrix

Pick the matrix from where your users are, not from a list of every device. Your analytics will show which browsers and screen sizes matter. A sensible starting point:

// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  projects: [
    { name: 'chromium-desktop', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox-desktop',  use: { ...devices['Desktop Firefox'] } },
    { name: 'webkit-desktop',   use: { ...devices['Desktop Safari'] } },
    { name: 'phone',            use: { ...devices['iPhone 13'] } },
    { name: 'tablet',           use: { ...devices['iPad Mini'] } },
  ],
});

Each project gets its own set of baselines automatically. A few rules for the matrix:

  • Test the edges of your breakpoints, not the middle. Layouts break at the smallest width a breakpoint allows and just before the next one kicks in. If your CSS switches at 768px, test 767 and 768.
  • Include one narrow phone. Overflow bugs show up first on small screens.
  • Know what an emulated device is. Playwright's WebKit engine and a device profile get you close to Safari on an iPhone, but it isn't the same as the real phone. Keep a short manual pass on one real iPhone and one real Android phone before big releases.

Every cell in the matrix multiplies run time and baselines to maintain. Add a cell when a real bug escapes through it.

Step 3: Kill the noise from dynamic content

This step decides whether your team trusts the suite or mutes it.

Freeze time. Dates, "posted 3 minutes ago" and countdowns change every run:

await page.clock.setFixedTime(new Date('2026-01-15T10:00:00Z'));

Mock data. Feeds, prices and user content should come from fixtures, not live APIs:

await page.route('**/api/products', route =>
  route.fulfill({ path: 'fixtures/products.json' }));

Mask what you can't control. Ads, maps, embedded videos and avatars:

await expect(page).toHaveScreenshot('home.png', {
  mask: [page.locator('.ad-slot'), page.locator('iframe')],
  animations: 'disabled',
  caret: 'hide',
});

Hide things with CSS. For elements that shouldn't be in the picture at all, pass a stylesheet just for screenshots:

await expect(page).toHaveScreenshot('home.png', { stylePath: 'tests/screenshot.css' });
/* tests/screenshot.css */
.cookie-banner, .chat-widget { display: none !important; }

Wait for the page to settle. Wait for fonts and the main content before capturing, never for a fixed number of seconds:

await page.evaluate(() => document.fonts.ready);
await expect(page.getByRole('heading', { level: 1 })).toBeVisible();

Step 4: Set thresholds on purpose

Playwright gives you three knobs:

  • threshold: how different one pixel's color has to be before it counts as changed, from 0 to 1. The default is 0.2.
  • maxDiffPixels: how many changed pixels are allowed in total.
  • maxDiffPixelRatio: the same, as a share of all pixels.

Start strict, close to zero changed pixels, and loosen only for a specific screenshot with a specific reason. A loose global tolerance looks harmless until a missing icon or a one-line text shift fits inside it. If one test needs a high tolerance to pass, that's a sign it needs masking or better fixtures, not a bigger number.

Step 5: Make it a CI gate

Run visual tests on every pull request, and fail the build on an unapproved diff.

The biggest source of false failures here is rendering: fonts and anti-aliasing differ between macOS, Windows and Linux, so baselines made on a laptop won't match a CI runner. Generate and update baselines in the same environment CI uses. Playwright publishes an official Docker image for exactly this, and running your update command inside it keeps everyone on identical pixels.

The workflow that holds up in a team:

  1. A PR changes UI. The visual tests fail and the HTML report shows expected, actual and diff side by side.
  2. If the change is intended, the author runs npx playwright test --update-snapshots in the Docker image and commits the new baselines in the same PR.
  3. The reviewer sees the code and the image changes together and approves both.

Upload the report as a CI artifact so reviewers can open the diffs without running anything. Never let a bot auto-update baselines on the main branch: an auto-approved baseline is a picture of today's bug.

What visual tests don't cover

Screenshots prove a page looks the same as yesterday. They don't prove the signup button submits or that checkout charges the right amount. Pair them with end-to-end flow tests for your critical paths and the occasional exploratory session (here's how to run those).

If writing and maintaining those flow tests is where your QA stalls, AnyTest can take that part: give it a URL, and its agents explore the product, find paths like signup, login and checkout, and write end-to-end tests for you to review and keep. When one fails, you get a step timeline of what happened instead of just a red X. Upkeep is built into the workflow: test-agent jobs on its MCP server (testagent_enqueue) come in create, edit, heal and explore modes, and anything the agents change waits in a pending-changes queue until you approve or discard it. Your screenshot suite and its flow tests answer two different questions, and you want both answered before you ship.

Quick setup checklist

  • 5 to 10 key pages plus shared components picked
  • Matrix chosen from analytics, including breakpoint edges and one narrow phone
  • Clock frozen, data mocked, ads and iframes masked
  • Animations disabled, caret hidden, fonts awaited
  • Strict thresholds, exceptions documented per test
  • Baselines generated in the CI Docker image
  • HTML report uploaded as an artifact
  • Baseline updates only inside the PR that caused them
  • Short manual pass on one real iPhone and one real Android before big releases
  • Flow tests for the critical paths alongside

Where to go from here

Start with the header and your pricing page in three browsers. Get those stable before adding more; a small suite people trust beats a big one they ignore. If you want flow coverage without writing it by hand, anytest.dev has a free capability audit: no account, it reads your public pages and lists the tests it could run there (it runs none). If you go further, a project can carry its own run schedule, set through the project_schedule_upsert tool, so the flows get checked between releases too.


If this saved you a round of "looks fine on my machine," a small tip on this post helps me keep writing guides like it. Related: How to automate UI, UX and localization QA with AI agents.

Conversation

Join the conversation

Share your thoughts with the author and other readers.

Comments0

Checking your account…

Latest comments

0
No comments yet. Be the first to share your thoughts.