Prompt
I need to compare UI screenshots in CI without flaky results
Latest observation
To compare UI screenshots in CI without flaky results, the key is to make the screenshot environment as deterministic as possible and to use a comparison method that tolerates tiny rendering differences.
Practical approach
1) Use a consistent rendering environment
Flakiness usually comes from differences in:
- OS / browser version
- font availability
- GPU / anti-aliasing
- viewport size / device scale factor
- animations or async loading
Best practices:
- Run screenshots in a Docker container or fixed VM image
- Pin browser version
- Pin OS/image version
- Install the same fonts in CI and local
- Disable GPU if possible
- Set explicit viewport dimensions and device scale factor
- Use the same locale/timezone everywhere
2) Freeze the UI before capturing
Wait for the page to be stable before taking the screenshot:
- wait for network idle
- wait for fonts to load
- disable animations/transitions
- stub random data, dates, and timers
- avoid loading live third-party content
Common techniques:
- set CSS to disable animations:
*, *::before, *::after { animation: none !important; transition: none !important; caret-color: transparent !important; } - use
document.fonts.ready - mock APIs and timestamps
3) Compare with tolerance, not exact pixels
Exact pixel matching is often too strict. Use:
- pixel diff threshold
- per-channel tolerance
- ignore small changed regions
- structural similarity if supported
For example, allow a small percentage of differing pixels:
- fail only if diff > 0.1% or 0.5%
- or ignore anti-aliasing differences
4) Mask or exclude unstable areas
Some parts of the UI naturally vary:
- timestamps
- ads
- avatars
- random IDs
- blinking carets
- charts with live data
Mask those regions during comparison or replace them with placeholders.
5) Capture at the right granularity
Instead of full-page screenshots, consider:
- component-level screenshots
- storybook/isolated component snapshots
- specific page sections
This reduces noise and makes failures easier to diagnose.
6) Use a visual testing tool with baseline management
Tools like:
- Playwright screenshot assertions
- Percy
- Chromatic
- Applitools
These help manage:
- baselines
- approvals
- diff thresholds
- review workflows
Recommended setup with Playwright
If you use Playwright, a good baseline config is:
- fixed viewport
- fixed browser version in CI
- disable animations
- set color scheme/locale/timezone
- use
expect(page).toHaveScreenshot({ maxDiffPixels, threshold })
Example:
await page.addStyleTag({
content: `
*, *::before, *::after {
animation: none !important;
transition: none !important;
}
`
});
await page.waitForLoadState('networkidle');
await expect(page).toHaveScreenshot('home.png', {
maxDiffPixels: 50,
});
CI checklist for stability
- Same browser version in CI
- Same OS/container image
- Fixed viewport
- Fixed fonts
- Animations disabled
- Network mocked/stabilized
- Randomness/time mocked
- Mask dynamic regions
- Use diff tolerance
- Review baselines intentionally
If you want the most robust route
For the least flaky visual tests:
- Use component-level screenshots
- Run in a pinned Docker image
- Mock all dynamic data
- Use Playwright/Chromatic/Percy
- Allow a small diff threshold
If you want, I can give you:
- a Playwright CI screenshot setup
- a Dockerfile for deterministic rendering
- or a step-by-step flake-proof visual testing checklist.