Baselines across machines
Why This Matters
The same page renders differently on macOS, Windows and Linux: different fonts, different anti-aliasing, sometimes a pixel more line height. A baseline made on your Mac fails in Linux CI with tiny differences everywhere, and the usual reaction (raise the threshold until it passes) also hides real changes. The fix is to make and compare baselines in one environment.
One environment: the Playwright Docker image
Playwright publishes a Docker image per version, with the browsers and the system fonts they need. The example repository makes and checks its baselines in that image, through two npm scripts:
"test:visual": "docker run --rm --ipc=host --platform linux/amd64 -v \"$PWD\":/work -w /work mcr.microsoft.com/playwright:v1.65.1-noble npx playwright test --project=visual",
"test:visual:update": "docker run --rm --ipc=host --platform linux/amd64 -v \"$PWD\":/work -w /work mcr.microsoft.com/playwright:v1.65.1-noble npx playwright test --project=visual --update-snapshots"
| Part | Why |
|---|---|
mcr.microsoft.com/playwright:v1.65.1-noble | The official image, Ubuntu 24.04 (“noble”), with the browsers for exactly this version |
v1.65.1 | Must equal the @playwright/test version in package.json, which is pinned to 1.65.1 for that reason |
--platform linux/amd64 | The image also exists for arm64. Apple Silicon Macs would pick arm64; GitHub’s runners are amd64. Forcing amd64 makes your baselines match CI |
-v "$PWD":/work -w /work | Your project folder inside the container, so the baselines land in your repository |
--ipc=host | Recommended for Chromium in Docker, so it does not run out of shared memory |
On Apple Silicon the amd64 image runs under emulation: slower, but it is a handful of screenshots.
One baseline per screenshot
By default Playwright adds the browser and platform to every baseline’s file name (login-form-visual-linux.png), so a team ends up with one baseline per operating system. Since baselines are only ever made on Linux here, the config drops that part:
snapshotPathTemplate: "{testDir}/__screenshots__/{testFilePath}/{arg}{ext}",
The baselines are then simply tests/__screenshots__/module-6/visual.spec.ts/login-form.png and so on.
Outside Docker: skipped, with a reason
What happens if someone runs the visual tests on their Mac anyway? Their screenshots would not match the Linux baselines. So the test file says so, once, at the top:
test.skip(process.platform !== "linux", "Visual tests run in the Playwright Docker image: npm run test:visual");
The real run on a Mac:
Running 5 tests using 5 workers
- 1 [visual] › tests/module-6/visual.spec.ts:9:1 › the login form
- 2 …ual] › tests/module-6/visual.spec.ts:14:1 › the login form with an error
- 3 …-6/visual.spec.ts:24:1 › a product card, with the image and price masked
- 4 [visual] › tests/module-6/visual.spec.ts:35:1 › the empty shop (stubbed)
- 5 [visual] › tests/module-6/visual.spec.ts:45:1 › the whole login page
5 skipped
A skip with a reason beats a red test that everyone learns to ignore.
CI in the same image
The GitHub Actions workflow runs the whole job in that image too:
jobs:
test:
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/playwright:v1.65.1-noble
options: --user 1001
steps:
- uses: actions/checkout@v6
- run: npm ci
- run: npx playwright test
The browsers are already in the image, so the npx playwright install --with-deps step is gone. CI compares against baselines made in the same image and version: a failure there means the page changed.
Updating baselines
When a design change is intended, the baselines must change with it:
- Make the change.
- Run
npm run test:visual:update. - Look at the new images before you commit them. In a pull request, GitHub shows changed PNGs side by side.
- Commit the code and the new baselines together, so the review sees both.
Step 3 is the one people skip. --update-snapshots accepts whatever the page looks like now, including the bug you were supposed to catch.
What looking at the baselines found
The five baselines in the example repository were made, and the comparison run after them passed: 5 out of 5. Then someone opened the images. This is login-form-error.png, the baseline that should show the form with its error message:
There is no error. The button says Logging in…: the screenshot was taken during the 0.8 seconds the form waits before it answers. The test had waited for the error like this:
await expect(page.getByRole("alert")).toBeVisible();
But the site is built with Next.js, which puts an invisible element with role="alert" on every page (its route announcer, for screen readers). That element was there before the click, so the wait passed at once. The fix waits for the error itself, with its text:
await expect(page.getByTestId("login-error")).toHaveText("Invalid username or password.");
And then the second attempt failed
Regenerating that one baseline (npm run test:visual:update -- -g "with an error") now failed, with a different message:
Error: expect(locator).toHaveText(expected) failed
Locator: getByTestId('login-error')
Expected: "Invalid username or password."
Received: "Username is required."
The test had typed practice into the username field, yet the form said the username was empty. The cause is hydration. The site is a Next.js app: the HTML appears first, and React attaches to it a moment later. Docker under amd64 emulation is slow, so the test typed before React was attached. The text was in the input, but not in React’s state, and on submit the form saw an empty username. On a laptop and in CI (native, fast) React was always ready in time.
The fix repeats the whole interaction until the form gives the answer the test is about:
await expect(async () => {
await page.getByLabel("Username").fill("practice");
await page.getByLabel("Password", { exact: true }).fill("wrong");
await page.getByRole("button", { name: "Log in" }).click();
await expect(page.getByTestId("login-error")).toHaveText("Invalid username or password.", { timeout: 3000 });
}).toPass({ timeout: 20_000 });
toPass retries the block until every assertion in it passes, or the outer timeout runs out. Use it for interactions that can legitimately need a second go, like this one, and not to paper over a test that fails for reasons you do not understand. Module 7 comes back to that line.
The third baseline is finally the one the test name promises:
Three lessons in one image:
- A green visual test only proves the page looks like the baseline. If the baseline is wrong, so is the test, forever.
- This test would not even have stayed reliably green: it compared one moment of a loading state with another, and would have failed the first time the timing changed.
- A wait on a locator that matches something you did not expect is no wait at all. Wait for the specific thing, with its text.
When you upgrade Playwright
A new Playwright version ships new browser builds, which can render a few pixels differently. Upgrade @playwright/test, the image tag in package.json and the image tag in the workflow together, then re-run npm run test:visual:update and review the diff.
Exercise
Run npm run test:visual:update without --platform linux/amd64 on an Apple Silicon Mac (or ask a colleague with one), then run npm run test:visual with the flag. What happens, and what does that tell you about where baselines may be made?
Show suggested answer
Without the flag, Docker uses the arm64 image, and the baselines are rendered on arm64. Comparing them later on amd64 can show small differences, typically in anti-aliased text edges, and then the tests fail although nothing on the page changed. Whether it happens depends on the browser build and fonts, which is exactly the problem: you cannot rely on it not happening.
So baselines must be made on the same platform as the comparison: same image, same version, same architecture. Write that down once, in an npm script, so nobody has to remember the flags.

