Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions frontend/content/blog/first-baseline-loading-skeleton.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
title: Your first screenshot baseline can be a loading skeleton
description: We added Playwright screenshot tests to a new app, and one of the first six screenshots caught a section halfway through loading. Two identical local runs hadn't shown it. Then a second flake, 23 pixels on rounded corners. What caused each, and the waits and Chromium flags that fixed them.
date: 2026-12-07T12:00:00+13:00
author: Nathan Broadbent
draft: true
---

On 5 December we added end-to-end tests to one of our apps, a Rust server with a React front end. Four Playwright tests click through it, and each step takes a full-page screenshot: six in all, sent to VisualCI as the first build.

Before opening the pull request we ran the suite twice on our build box, and the two runs gave byte-identical PNGs. That looked like a stable suite.

## The first build

Five of the six screenshots in CI's first build were right. The sixth, an admin page, had a grey skeleton block in the middle where one of its sections was still loading.

That section asks the API for its data when the page opens. While the feature behind it is turned off, the API answers 404, and the React Query hook then hides the section altogether. So the finished page has no section and no skeleton. The test waited for the page's heading, which appears at once, and took the screenshot. On the build box the 404 came back before the screenshot. On the CI runner the screenshot came first: the server log showed the 404 arriving 30 ms after the page loaded.

A page that hides part of itself on an error is the hard case. Nothing on the finished page tells a test what to wait for, because the thing it would wait for never appears.

We caught it because we looked at the build's contact sheet before approving it. The first build of a new suite is easy to approve without looking: there's nothing to compare it to, so every screenshot shows as added, and none shows as changed. But whatever you approve becomes the baseline. Had we approved the skeleton, later runs would have flipped between the skeleton and the finished page, and the diff would have looked like a flaky test rather than a bad baseline.

## The fix

The screenshot helper now waits for the page's requests to finish and for its fonts to load, and stops animations and hides the text cursor for the shot:

```ts
export async function shot(page: Page, name: string) {
await page.waitForLoadState('networkidle')
await page.evaluate(() => document.fonts.ready)
await page.screenshot({
path: `../tmp/e2e/screenshots/${name}.png`,
fullPage: true,
animations: 'disabled',
caret: 'hide',
})
}
```

`networkidle` means no requests for 500 ms. Playwright's docs discourage it for tests, and for most assertions they're right: waiting for the element you're about to check is faster and says what you mean. A screenshot asserts the whole page, though, and here the request that mattered had no element to wait for. It has a limit: a page with a websocket, long polling or a request every few hundred milliseconds never goes idle, and the wait times out. On such a page, wait for the specific response instead (`page.waitForResponse`), or for a marker the app sets when it has finished loading.

The rest of the suite's setup is about the same problem, making two runs the same:

- **A fresh database per run**, with fixed email addresses, so every run starts from the same data.
- **`retries: 0`.** A retry hides a flaky test by passing on the second try. We want it to fail, so we see it.

The second build had the finished page, and we approved that one.

## Then 23 pixels

Later that day a pull request that changed nothing on the waitlist page showed its screenshot as changed. VisualCI had found 23 pixels, 0.0017% of the 1280 × 1074 page, in three 16 × 16 regions. All of them were on the anti-aliased rounded corners of a text area and an input.

Chromium rasterises on the GPU, in tiles, on several threads, so the edge of a rounded corner can come out one colour step different from one run to the next. Our two runs on the build box hadn't shown it: the flake only appeared on CI's runners.

A threshold would have hidden it. But a threshold high enough to hide 23 pixels on every page also hides a real change of 23 pixels, such as a changed icon or a one-character typo. We fixed it where it starts instead, by launching Chromium with software rasterising on one thread, one colour profile and greyscale text:

```ts
// playwright.config.ts
use: {
reducedMotion: 'reduce',
launchOptions: {
args: [
'--disable-gpu',
'--force-color-profile=srgb',
'--disable-partial-raster',
'--disable-skia-runtime-opts',
'--disable-lcd-text',
'--font-render-hinting=none',
'--num-raster-threads=1',
],
},
},
```

These flags change every screenshot once, because greyscale text replaces subpixel text. In our build that was between 0.8% and 2% of each page's pixels, all on the edges of text, with no page changing size. Approve that one build as the new baseline, and rebase any open branches: until they do, they compare against the old baseline and show every screenshot as changed.

The builds since have matched their baselines exactly.

## What we'd tell ourselves on day one

- **Look at the first baselines before you approve them.** They're the only screenshots nobody diffs, and every later run is compared against them.
- **A visible heading doesn't mean the page has finished loading.** Look for sections that load separately, and above all for ones that hide themselves when their request fails.
- **Two identical runs on your machine don't prove the suite is stable.** CI's timing and hardware differ. Run it twice in CI, where the baselines come from.
- **Fix a flake at its source before you reach for a threshold.** Keep thresholds for real differences between machines.

The waits, the flags and the rest of the list (pinned clocks, seeded data, fonts, fixed viewports) are in our guide to [deterministic screenshots](/docs/screenshots/deterministic/).
Loading