📚 Encyclopedia

Web Development · updated 2026-11-30

Headless Chrome testing gotchas

headless-chromecdptestingscreenshotswebgl

Driving headless Chrome (over the DevTools protocol or with --screenshot / --dump-dom) is the main way UI claims get verified on Justin's projects. It is also easy to get a test that silently proves nothing. Each item below has produced at least one wrong conclusion; most look like a product bug when they are really a test artifact.

Starting it

Things that silently invalidate a test

  1. Headless reports hover: none. matchMedia('(hover: none)').matches is true even at desktop width with touch emulation off, so code takes the touch branch. Emulation.setEmulatedMedia with a hover feature does not change it. Override matchMedia before page scripts run with Page.addScriptToEvaluateOnNewDocument, returning {matches:false, ...} for that one query. :hover styles need CSS.forcePseudoState. Corollary: anything hidden by (hover: hover) and (pointer: fine) cannot be asserted headlessly.
  2. Element.click() is not a user gesture. It dispatches an untrusted event with no transient activation, so unmuted play() and requestFullscreen() fail and the test "proves" a bug. Use Input.dispatchMouseEvent (pressed + released) or Input.dispatchTouchEvent (start + end). Touch dispatch also exercises the compatibility mouse events, where real bugs live (see Touch compatibility mouse events (mousemove before click)).
  3. scroll-behavior: smooth moves the target after you measure it. A rect read right after scrollIntoView() is where the element was. Set document.documentElement.style.scrollBehavior = 'auto' or wait ~900 ms, and sanity-check with document.elementFromPoint(x, y).
  4. Emulated mobile uses the desktop autoplay policy. An unmuted autoplay under setDeviceMetricsOverride({mobile:true}) proves nothing about a real phone. Say so.
  5. Minimum window width. --window-size=390,844 does not give a 390 px viewport; Chrome enforces a minimum (~500 px wide, ~757 tall) and crops the screenshot, which looks like horizontal overflow. Measure phone layouts inside a sized <iframe> (390 to 412 px) or use Emulation.setDeviceMetricsOverride. Probe scrollWidth == clientWidth in the game document itself; a scrollbar in the screenshot may belong to the outer page.
  6. captureBeyondViewport: true paints position: fixed layers once at the top, so a full-page shot makes a fixed background look like it stops. Judge fixed layers from viewport-sized shots.
  7. Virtual time. Under --virtual-time-budget: performance.now() does not advance (a timing assertion passes at 0.0 ms), rAF barely fires (and is starved inside iframes), Web Workers are not waited for, and CSS transitions in an iframe section far outside the viewport do not advance. Pages should expose a tick() hook the harness can pump (every ~50 ms; at 16 ms a loaded desktop timed out and looked like a hang). For workers, hold the load event open with a deliberately slow resource (/__slow?ms=) and run in real time.
  8. --dump-dom fires before ES modules finish. Benchmarks used the difference method instead: wall clock of a 3300-frame run minus a 300-frame run.
  9. Synthetic pointer ids. canvas.setPointerCapture(id) throws for synthetic PointerEvents; wrap it in try/catch.
  10. Navigating twice quickly gives "Failed to fetch" from the aborted first load, and navigating mid view-transition logs "AbortError: Transition was skipped". Wait for the page's own ready signal, not a sleep.
  11. Media elements that are not in the DOM. Flutter's just_audio on web plays through an <audio> element it never attaches, so a CDP query finds nothing. Verify playback through app state instead (a persisted position that keeps advancing). That route also caught a real bug: a listener overwriting the saved position with 0 during session restore (see just_audio in Flutter, gotchas and patterns).
  12. Port collisions. A stale http.server already on the port (rooted somewhere else) means every screenshot is a 404 page while the "is it listening" guard happily skips starting yours. Pick a free port, start with --directory.

  13. The overflow check can lie. Under Chrome mobile emulation window.innerWidth grows to the overflowing layout width, so scrollWidth - innerWidth reads 0 on a page that really overflows. Compare document.documentElement.scrollWidth to the device width (e.g. 390) instead. (plumb, 2026-10-06)

  14. --dump-dom also serializes <script> source. A marker string that appears in both code and rendered output grep-counts about double; grep for something only the rendered DOM has.
  15. rAF-gated state under virtual time reads as "never ran". In Spacebar a working Esc-to-menu flow looked broken five different ways under --virtual-time-budget. A small CDP driver (tools/uicheck.mjs: real clock, real key events, screenshot at the end, steps like [["wait",2000],["key","Enter"]]) gave the right answer.
  16. Truncated output filenames overwrite each other. Two URLs mapped to the same shortened screenshot name; the second silently replaced the first and produced a false "campaign mode is broken". Use distinct names per URL.
  17. Screenshot byte size is a cheap state fingerprint: in Spacebar the menu was about 21 KB, gameplay about 7 KB, the editor about 48 KB. A dead band at the bottom of headless shots appeared in every mode identically (artifact, not bug).

When a test fails, suspect the harness first

Cheap checks without a full harness

Harness pattern that works

Gotchas

See also

Linked from

Sources: spacebar memory (reference_dev_tooling), justinahampton memory (headless-chrome-test-gotchas, 3d-subdomain, fun-freetime, arcade-recourse, arcade-orbdestroyer2, joyride-architecture), tldr posts #1616, #1653, #1666, #1737, #1739, #1760, hunsley memory (hhs-frontend), plumb memory (gotchas), jah memory (headless-chrome-verify), justinahampton memory (videos-drop-links), tldr memory (perf-post-load)