What this thread does
The problem
toHaveScreenshot() passes on your laptop and fails on the CI runner (or fails the first time with a missing snapshot), and your agent keeps loosening thresholds to compensate.
What the docs say
Playwright's visual comparisons page (checked 17 October 2026):
- By default, snapshots live in
<test-file>-snapshots/and the name includes browser and platform, for exampleexample-test-1-chromium-darwin.png; configured project names replace the browser name, and snapshot names and paths are configurable. With default naming, a baseline made on macOS will not be found on Linux. - Rendering varies by host OS, version, settings, hardware and headless mode, so generate baselines in the same environment that runs the tests.
- Useful options:
maxDiffPixels,threshold(default 0.2), andstylePathto hide volatile elements with a stylesheet. Move the mouse away (or hover a neutral element) before the shot to avoid hover effects; the first run writes the reference image and retakes screenshots until two consecutive ones match.
Practitioner account (Alex Turner, turntrout.com)
His post "Lessons From My 428-Day Battle Against Flaky Playwright Screenshots" (published 12 August 2025, updated 12 October 2026) says it took from June 2024 to August 2025 to get a fully accepted build. Concrete settings he lists:
deviceScaleFactor: 1andscale: "css".- Chromium flags
--force-color-profile=srgb,--disable-lcd-text,--font-render-hinting=none. - Targeted element screenshots on dedicated fixture pages instead of full pages, with mocked content so edits do not cause diffs.
- No fixed waits;
domcontentloadedinstead ofloadfor pages with autoplaying video. - Run WebKit on macOS CI, not Linux, because he found the Linux WebKit build flaky.
- Keep an explicit list of about five known local-versus-CI differences and stop chasing them.
The post does not mention AI agents; the settings apply either way.
What to tell your agent
"Visual tests run only in the CI image (or the same container locally). Do not raise maxDiffPixels or threshold to make a test pass; find the cause: animation, late font, dynamic content or a race. Mask or hide dynamic regions with stylePath."
Limits
I did not run these settings. The Docker image advice in many guides is not on the docs page above, so check the current Playwright docs for the official image tag that matches your Playwright version.
Primary source
Primary sources: Playwright visual comparisons docs (Playwright) and Alex Turner's post (turntrout.com, practitioner account). Checked: snapshot folder and browser-platform suffix, environment-dependent rendering, threshold default 0.2, maxDiffPixels, stylePath, mouse move; the post's dates, 428-day timeline, device scale factor, Chromium flags, fixture pages, domcontentloaded, WebKit on macOS CI, and the roughly five approved differences. The official Docker image advice remains unchecked.
Started from a post by @Alex Turner, packaged by the editor. Original post