In brief
Visual tests compare a screenshot of the application with an approved baseline and report pixel differences. They catch defects that functional tests cannot see: overlapping elements, truncated labels on a phone, a missing icon. You avoid false alarms in four ways: generate baselines in the same environment as the tests, disable animations and the text caret, mask changing data, and set the difference threshold deliberately for each view.
What visual tests catch and what they miss
A functional test checks that the "Pay" button exists and works. It will not notice that after a style change the button is white on a white background, or that on a 390-pixel-wide screen the product name covers the price. These are typical defects after CSS changes, a component library update or a new font, and they are what visual tests are for.
| Functional test | Visual test | |
|---|---|---|
| Checks | behavior: a click, a result, a message | appearance: layout, colors, fonts, spacing |
| Catches | wrong logic, a missing element, a data error | overlapping elements, truncated text, a wrong color, a missing icon |
| Misses | appearance changes when behavior is correct | logic errors when the screen looks the same |
| Result | pass or fail | an image of differences for a person to judge |
A visual test does not judge whether a screen is pleasant or readable. It only says that something changed compared with the baseline. Whether the change is intended is for a person to decide. It also does not replace accessibility testing, although it will catch some issues along the way, such as contrast after a color change. The full criteria are covered in WCAG accessibility testing.
Where false alarms come from
Teams abandon visual testing for one reason: every run reports dozens of differences that are not defects. Each cause has its fix.
| Cause | Symptom | Fix |
|---|---|---|
| A different rendering environment | fonts half a pixel wider on a laptop than in CI | baselines and tests in the same container image |
| Animations and transitions | a screenshot taken mid-animation | disable animations during capture |
| A blinking caret in a text field | a difference of a few pixels | hide the caret |
| Changing data | date, time, a notification counter, a random recommendation | a mask on the element or fixed test data |
| Third-party content | a map, a video, a frame with someone else's content | a mask or a stylesheet that hides the element during capture |
| Lazy-loaded images | an empty space instead of a photo | wait for loading, scroll the page before capture |
| Hover state | a highlighted button | move the mouse away before capture |
Some causes come from data, not the tool. A search results screen on a shared test environment changes every day because someone added a product. A visual test needs fixed data like any other test; otherwise it becomes a flaky test, as described in flaky tests.
How to set up comparison in Playwright
Playwright has a built-in toHaveScreenshot assertion. On the first run it takes screenshots until two consecutive ones are identical and saves the last one as the baseline. The baseline file name includes the browser and platform, for example chromium-linux, because the documentation notes that rendering depends on the operating system, browser version, settings and hardware. That is why baselines should be created in the same environment where CI runs them, usually a container.
The most important assertion options:
- animations defaults to "disabled" and caret to "hide", so animations and the caret do not spoil screenshots,
- mask covers the given elements with a solid-color box,
- stylePath adds a stylesheet used only during capture, for example to hide frames with third-party content,
- threshold is the per-pixel color difference tolerance from 0 to 1, with a default of 0.2,
- maxDiffPixels and maxDiffPixelRatio set how many pixels, or what share of them, may differ.
test("cart on a phone", async ({ page }) => {
await page.setViewportSize({ width: 390, height: 844 });
await page.goto("/cart?data=cart-3-items");
await expect(page).toHaveScreenshot("cart-390.png", {
mask: [page.getByTestId("delivery-date")],
maxDiffPixelRatio: 0.001,
});
});Set the threshold per view, not globally high "just in case". A threshold that is too loose lets a shifted button through; one that is too strict reports anti-aliasing differences. A good practice is to start with strict values, run the test a dozen or so times on unchanged code, and loosen it only where differences appear without changes to the application.
What to compare: pages, components or fragments
A screenshot of an entire long page changes with every content update, so it produces many alarms and little information. A split into three levels works better:
- Components from the UI library (a button, a form, a product card) in isolation, in every state: empty, with an error, disabled. Component catalog tools such as Storybook can take a screenshot of every saved component version and compare it with its baseline.
- Key screens of a process (cart, order summary, application form) on fixed data, at two or three widths: phone, tablet, desktop.
- Fragments of pages that break often, such as the header with the menu or a table with many columns.
When the application has several themes (light and dark) or languages, the number of screenshots grows quickly. Choose combinations by risk: the language with the longest words on the narrowest screen is usually enough to catch truncated labels.
Reviewing changes and updating baselines
A visual test produces an image of differences, not a verdict. That is why the process matters more than the tool:
- Visual tests run on every change before merge, as shown in the map in Shift-left and shift-right testing.
- When there is a difference, the report shows the baseline, the new screenshot and the diff image side by side.
- The author of the change decides whether the difference is intended. If it is, they update the baseline (in Playwright with the --update-snapshots option) in the same change, and the new baseline is reviewed like code.
- If the difference is not intended, it is a defect found.
The worst practice is a bulk update of all baselines "because something shifted". After a few such updates, the tests no longer guard anything.
Visual tests are best added to existing automation rather than built separately. At B2Bnetwork we add them as part of test automation for the highest-risk screens, running in the same pipeline as the functional tests. A comparison of browser testing tools is in Playwright, Cypress or Selenium.
Questions and answers
Do visual tests replace functional tests?
No. They check something else: appearance rather than behavior. A screen can look identical while a button sends the wrong data, and the other way round. Visual tests are added to key screens alongside functional tests, often in the same test: first the steps and behavior assertions, then a screenshot at the end.
How many screen widths should you check?
Two or three are usually enough: the narrowest phone you support, a tablet or the mid-size width where the layout changes, and a typical desktop screen. More widths mean more screenshots to maintain. Pick the widths at which the page layout actually switches.
An open source tool or a cloud service?
Playwright's built-in screenshot comparison is enough for many projects, as long as baselines are created in a container and reviews happen in the repository. Cloud services add a convenient diff review, history and rendering in many browsers, but they send screenshots outside your environment. For screens with customer data, assess this from a data protection angle.
Why does a visual test pass on my machine but fail in CI?
Most often because of a different rendering environment: another operating system, browser version, installed fonts or display scaling. The fix is to generate baselines and run tests in the same container image, locally as well. Baselines made on a laptop and compared in CI produce false alarms on every run.