Testing web applications

Visual regression testing: when comparing screenshots makes sense

A functional test can confirm that a button submits a form while missing clipped text or an incorrect layout. Visual regression testing fills this gap by comparing the current rendering with an approved reference image, commonly called a baseline. The comparison becomes useful only when the team controls the environment, test data, and approval process.

Why an image difference is not automatically a defect

A changed pixel does not by itself prove that the product is wrong. A live clock, rotating promotion, animation, random avatar, or different font can alter a screenshot even though the application behaves correctly. If these differences appear on every run, the team may start ignoring failures or replacing baselines without reviewing them.

An overly generous tolerance causes the opposite problem. It removes minor noise but may also conceal a shifted button, clipped label, or missing icon. The goal is therefore not to force a perfect match at any cost. It is to create a signal that a person can assess quickly and consistently.

Visual automation is most valuable where appearance is part of the expected result: responsive layouts, shared components, charts, document previews, and meaningful form states. A one-off design assessment still needs human judgement. Our guide to tests that are not worth automating explains where that distinction matters.

How to make visual checks repeatable

Standardise the execution environment. Keep the browser version, operating system, fonts, viewport, and rendering settings consistent. Playwright’s visual comparison documentation notes that the host platform, hardware, settings, and browser mode can all affect rendering. Generate a baseline in the same controlled environment that will perform subsequent comparisons.

Create a deterministic application state. Use known data, locale, time zone, and viewport dimensions. Wait for an observable application state before taking the screenshot rather than sleeping for a fixed interval. You may disable or mask an animation, cursor, or live timestamp when it is outside the purpose of the check. Masking an entire dynamic panel, however, could hide the very problem the test should reveal.

Choose a useful comparison boundary. A full-page image provides context but changes whenever any content on the page moves. Screenshots of stable components or important regions usually identify a difference more precisely. Select browsers according to user data and risk, as you would for a cross-browser testing strategy. Separate platforms may require separate baselines because they do not render identically.

Review baselines as code changes. The first screenshot is not correct merely because a test generated it. A reviewer should inspect its content, data state, and viewport before accepting it. For later changes, compare the expected, actual, and difference images. Update the baseline only after confirming that the visual change is intentional.

Apply tolerances narrowly. A small pixel threshold can accommodate unavoidable rendering noise. One large project-wide threshold weakens every comparison. Remove the source of instability first, then add a local exception only where its purpose is understood and documented.

What a visual test cannot prove

Matching screenshots do not show that a button works, validation rules are correct, or an order was created in the database. They also do not assess the meaning of copy, keyboard operation, or accessibility as a whole. Pair visual comparisons with behavioural assertions and, where appropriate, API-level checks.

A screenshot difference does not have to block every release immediately. It first needs an owner and a review process. The guide to automated tests in CI/CD shows how different signals can run at suitable stages.

A small matrix: basket, error and open dialog

For a model shop, choose a basket with a longer product name, a form with an address error and an item-removal confirmation dialog. Prepare each state with the same data at widths of 390 and 1280 CSS pixels. This is not a universal resolution list. Add others according to supported devices and problematic layout boundaries.

State What the screenshot reviewer checks
Basket Complete name and price, separation of rows and a visible control for continuing.
Address error A readable message beside the correct field and an unobscured correction button.
Open dialog Decision text, both options and the intended background overlay.

Use the relevant approved copy for SK, CZ and EN. A longer translation may wrap a button label or increase an error’s height. A passing Slovak screenshot does not confirm the other languages. A separate functional dialog test must verify that cancelling keeps the item and confirming removes it.

Diagnose the difference before approving it

After a failure, work from environment to content:

  1. Check that the reference uses the same platform, browser, language and dimensions.
  2. Check the product, error state, open dialog and image loading.
  3. Compare mouse position, animations and fonts. An accidental hover can alter an otherwise stable button.
  4. Inspect the complete current image and the difference. A pixel map alone does not establish that text remains readable.
  5. Decide whether to repair the product, test setup or approve an intentional reference change.

For fonts, MDN explains the distinction between used and declared fonts. Completed font waiting does not necessarily establish that the intended optional font is displayed. A wrapping change therefore requires checking the actual result, rather than adding another pause.

Tolerances and masks need a deliberate-defect check

Playwright distinguishes colour threshold, differing pixel count and differing pixel ratio. The ratio relates to the size of the entire compared image. A small missing icon can therefore represent a low proportion of a large page despite being important to users. Add a targeted region check or functional assertion where necessary.

In an isolated environment, deliberately clip a name or shift a control and verify that the configured check fails. Revert the change afterwards. If a live date is covered, check the mask’s bounds after a layout change too. It must not hide an adjacent price or error message. Success after adding an exception is insufficient if the test no longer sees its subject.

In Python, you can capture a screenshot through Playwright and pass it to image comparison. With pytest-playwright, you need a chosen comparator and reference-management rules. Built-in image assertion toHaveScreenshot belongs to Playwright Test for Node.js. Do not rely on identical setting names between these tools. An ARIA snapshot compares accessible structure, rather than pixels.

When approving a change, retain the reason, version and reviewer with the images. An intentional basket change should not automatically approve new references for every screen. If a reference or required environment is missing in CI, mark the unverified scope. Do not create a new “correct” screenshot merely to make the pipeline pass.

What you gain

Next step

Choose three stable, business-critical screens. Run the comparisons repeatedly without changing the application, remove incidental differences, and only then approve the first baselines. Assign a reviewer and agree what should happen when a visual comparison fails.

Related topics

You might also be interested in

Catch bugs before they reach users

End-to-end tests of key scenarios can run automatically in CI/CD to catch bugs before they reach customers.