Produce before/after visual evidence for a front-end change
Instruments and display rungs: load desktop-probing before the first
capture; it defines the host’s and the target’s platform file that this skill
cites by section. To decide which states
show the change, also load the sibling that owns its subject:
desktop-theme-colors for a forced-colors, contrast or color-scheme state,
desktop-theme-layout for geometry, desktop-theme-tokens for a token value.
1. Decide what state makes the change visible, before writing any capture code
An identical pair does not prove the change neutral: the state that shows the
difference may never have been on screen. Work out from the diff which rendered
state differs. A rule that only applies under [selected], :hover,
:focus-visible or a media query produces byte-identical screenshots in the
default state.
When the shots are for a bug, shoot the scenario it reports first, reproducing the conditions it names, such as a placement, a window type, settings or steps, and then the states the diff touches.
When the diff changes a color, read the “Colors that resolve per realm”
section of docs/code-quality/coding-style/css_guidelines.md before choosing
what to shoot. The doc lives in the source tree, not in this skill’s
directory, and it’s kept up to date with how different parts of the code base
interact in non-obvious ways; it may differ from what you remember.
Sort the diff into these cases by where its difference shows; the case decides
what you capture, force or log. One diff often falls under several, such as a
focus ring that shows only under :focus-visible beside a hit area that moves
no pixels; handle every case it falls under, each with its own pair.
Visible in the default state. Capture and move on.
Visible only in a state. Force the state rather than driving the UI to it: synthesized keys and mouse events fail quietly, leaving the pair identical with nothing in the log. An attribute takes
element.toggleAttribute("selected", true). An element-state pseudo-class, focus included (e.g.:hover,:focus-visible,:checked), takesInspectorUtils.addPseudoClassLockon the element the selector names, often an ancestor of the one you shoot (the lock rule indesktop-probing§2 “Keep the probe honest”). A media-query state takes its pref (desktop-theme-colors§2). Focus that a widget’s own key handling moves between items (e.g. in a menu or list) is the exception: a lock neither moves focus nor picks the item the keys would, so drive it withkeyssteps and print:focus-visiblebeside each reading. In a content page, the probe runner’s--stepsmovestep makes:hoverfollow the pointer.capture.jsfinishes running transitions before each shot; elsewhere finish them yourself (desktop-probing§2 “Reach and read the state”). Restore the state if later captures depend on it.Too small for the capture to isolate. A color that moves a few sRGB units in any painted property, or a box that moves less than a device pixel, is lost to rounding or to
./mach theme pixel-diff’s threshold (§5). Log the computed value of each property the diff changes, on the element it changes, in both halves, and hand over the measurement, as for a 1pxcolor-mix()border. A hue change across a wide area (a translucent shadow’s color) can stay visible below the threshold: diff it at--threshold 0, and hand it over withsheet.py(§7), which counts every differing pixel and enlarges them below the pair.Visible only for some content or configuration. A change to what clips, overflows, truncates or scrolls shows only where content reaches the boundary (e.g. a long label, an icon drawn to its corners, RTL), and a rule under a feature pref, a density, a theme or a device pixel ratio shows only there; a swap between a snapped and an unsnapped length shows only at some device pixel ratios (
desktop-theme-layout§2 “Device pixels”). Find the instances first with an inline old-against-new differential over every candidate (desktop-probing§4), and shoot those;desktop-theme-layout§1 owns the configuration sweep. Enumerate the candidates from the changed rule: every element its selector matches (querySelectorAllwith the state pseudo-classes stripped, in each document and shadow root that loads the sheet), and on each, every edge or property the declaration paints.Not visible in any state. A change to a hit area, a clip boundary, a scroll extent, a focus target or the stacking order moves no pixels. Draw or log the invisible thing (§4).
2. Build the capture harness
Surface |
Route |
|---|---|
Chrome, rendered by a suite’s fixture |
a scratch browser-chrome test pasting |
Chrome, no fixture needed |
|
Content, such as a web page or an about: page |
|
Chrome ink inside the browser’s rect, such as an outline or border drawn over the page’s edge |
a compositor grab: |
An open panel or menupopup |
a compositor grab (§2 “The compositor’s output” in |
A subject that matches two rows, such as a button a suite’s fixture renders
inside an open panel, takes the popup row: no fixture makes drawWindow draw a
popup.
drawWindow and the chrome window’s drawSnapshot both leave an open popup out
and draw what lies under it. A popup here is a <panel> or <menupopup>
element; a dropdown built from ordinary elements in the window captures like
the rest of the chrome. On Linux without a compositing manager, as on Xvfb,
popup.css paints a popup square with no shadow margin. That shows a change to
what the popup holds, but a change to its radius or shadow yields an identical
pair: for one, pass --setpref ui.GtkCSDTransparencyAvailable=1 in both
halves, and expect the corners and the margin to paint black on Xvfb, since a
pixel the popup leaves transparent has nothing composited under it
(platform-linux.md, section COMPOSITING, in desktop-probing).
A compositor grab shoots the screen as it is, so whatever the real pointer
leaves behind lands in every later grab: a move step’s :hover stays on the
last element it reached, and that element’s tooltip opens after a moment. Before
grabbing any other state, move the pointer off the chrome ({"move": [x, y]}
onto the page) and lock the state in a chrome step (§1). A tooltip or a hover
you did not ask for in a frame means the pointer drew it.
capture.js. Paste scripts/capture.js at the head
of the test or probe and call await capture(name, element) once per shot (it
is async; a call left unawaited ends the probe before any SHOT line), with
every axis the probe varies (e.g. density, ratio, state) in name, since a
repeated name overwrites the earlier image. Paste it rather than load it by
path, and don’t retype it: without its ctx.scale(dpr, dpr), drawWindow draws
at 1x into a canvas sized for the device pixel ratio, so above a ratio of 1 the
image fills only part of it. It draws the element’s border box plus 8 CSS px
with drawWindow (capture(name, element, { pad: 24 }) keeps a wider shadow or
outline in frame), in the CSS pixels of the element’s window, after lifting the
root’s remotecontrol attribute (the harness’s stripes over the address bar).
The background is white; for white ink, pass { background: "magenta" } and
check the ink’s pixel count. Its SHOT line gives the frame’s size, dpr= and
origin= in the window, all in whole CSS px; it throws for an element with no
box or outside the window. The frame follows the element, so an element the
change resizes gives frames of different sizes in the two halves, which compare
out of alignment: capture an ancestor whose size the change leaves alone, such
as the toolbar around a button, and crop both halves to the element with
sheet.py --rect (§7). Log the element’s rect and the computed value of each
property the diff changes beside it (§5).
The label and the output directory come from prefs,
shots.labelandshots.dir, read at module scope: pass them at launch (--setpref, to the mochitest harness or the probe runner) or in the profile. ApushPrefEnvinside a task is too late and silently yieldsunlabeledunderPathUtils.tempDir.shots.dirmust be absolute and must already exist. The same unedited file produces both halves of the pair.
Test route. Drop the file in the browser/ test directory nearest the UI
under test and register it (§2 “Register and run it” in
scratch-probe.md).
Reuse whatever renders the surface in the suite that covers it (e.g. a mock
server, a test provider), not a hand-built node that merely matches the
selectors.
Probe route. Paste capture.js into an --async body, which may await
and returns its result (§2 “The chrome sandbox and --async” in
desktop-probing’s
references/probe-runner.md).
The suite’s fixture is out of reach: a state the product’s own providers cannot
produce on that profile is forced on a real node or built by hand, with the
caption saying so. Open a tab with gBrowser.addTrustedTab(url);
gBrowser.addTab throws without a triggeringPrincipal. The SHOT lines go to
the browser’s stdout, so include globalThis.__shots in what the probe returns
({...readings, shots: globalThis.__shots}), or pass --gecko-log and grep
SHOT from it. A string-typed pref that looks like an integer takes single
quotes, as in --setpref "layout.css.devPixelsPerPx='2'".
Content route. A --steps list run with --realm content --url <page>
drives the page headless, and a chrome step calling
gBrowser.selectedBrowser.browsingContext.currentWindowGlobal.drawSnapshot(rect, scale, "white") composites the content process into an ImageBitmap; draw it
on a canvas and write the PNG with IOUtils.write. Put the work in an async
function and return its promise from the step: Marionette waits for a returned
promise, and without the return the step ends before the file is written. In a
chrome step document is the browser window’s, so
document.createElement("canvas") gives an HTML canvas. --help lists the
step kinds. capture.js cannot see the page (§1 “Chrome and content canvases”
in
measuring-pixels.md).
capture-pair.sh drives only the test and chrome-probe routes; for a content
page or a compositor grab (which needs --no-headless on the display the
host’s platform file in desktop-probing, section GRABS, allows), run
each half yourself with the by-hand revert in desktop-probing §6
between them.
Grab route. Each half is a --no-headless chrome probe on a private Xvfb
display. Its steps file sets the surface up and opens the popup in a chrome
step, then takes {"grab": "<name>"}, which writes <name>.png beside the
steps file, so each half needs its own directory. A step waits only for a
promise it returns, so write a setup that awaits as
return (async () => { …; await shown; })(), with shown the popup’s
popupshown promise, taken before the call that opens it. The snippet runs
after the last step and reads the state that step left: return the popup’s
getOuterScreenRect() from it for sheet.py --rect. Xvfb takes the desktop’s
color scheme, so pin it, and with no window manager the popup hides itself
unless ui.popup.disable_autohide is set. Run the whole of this as one call,
with a tool timeout that covers both launches:
half() { # $1: after or before
env -u WAYLAND_DISPLAY GDK_BACKEND=x11 MOZ_ENABLE_WAYLAND=0 \
xvfb-run -a -s "-screen 0 1600x1000x24" timeout 300 \
./mach python --virtualenv desktop-theme .claude/skills/desktop-probing/scripts/firefox-probe.py \
--binary "$PWD/<objdir>/dist/bin/firefox" --no-headless --realm chrome \
--window-size 1200x800 \
--setpref ui.systemUsesDarkTheme=0 --setpref ui.popup.disable_autohide=true \
--steps <dir>/$1/steps.json --script-file <dir>/read.js \
--gecko-log <dir>/$1/gecko.log --result-file <dir>/$1/result.json
}
half after
if git diff --quiet HEAD -- <paths>; then
( trap 'git restore --source=HEAD --worktree -- <paths>' EXIT
git restore --source=<base> --worktree -- <paths> && half before )
else
echo '<paths> differ from HEAD; commit the change first' >&2
fi
git status --short
One change often needs two routes, such as a grab for the button in an open
panel and capture-pair.sh for the same button in the toolbar. Give the pairs
from both to one sheet.py call.
Firefox MCP route. Use screenshot_page or screenshot_by_uid, headless,
on this tree’s binary (desktop-probing §1’s binary check). Before the
first restart_firefox, read §1 “Binary and profile”, §2 “Prefs” and §5
“Screenshots” (where saveTo may write) in
firefox-mcp.md. The
before half is the by-hand revert in desktop-probing §6 followed by
restart_firefox. The screenshots clip to the viewport, and raising
layout.css.devPixelsPerPx shrinks the CSS viewport at a
fixed window size, so call set_viewport_size after the ratio is in force and
check the frame size against the element’s rect: a pair cropped alike looks
consistent.
On every route the profile is empty and online. Seed what the surface lists
and undo the launch’s other differences per desktop-probing §2 “A probe
launch is not a user’s profile”. Anything fed from the network or the clock
(e.g. suggestions, stories, remote settings, dates) can differ between the
halves; turn those sources off or mock them (a mock server through the
provider’s endpoint pref).
3. Capture before and after without building twice
./mach mochitest installs the test file itself, so one committed change and
one scratch test give both halves.
scripts/capture-pair.sh reverts each <path> to
--base for the before half and restores it afterwards; read its --help
before passing any option. Its --probe form takes a chrome-context script
pasting capture.js in place of a test, for a chrome surface no suite’s fixture
renders. One call captures one cell of a regime matrix.
For a change that reaches many surfaces in many regimes,
scripts/matrix.py runs capture-pair.sh --probe once per
cell from a JSON spec of regimes and surface sets, and its render mode draws
the run into PNG sheets: one for the shots where a reading changed, one for
those that failed or measured nothing the same way before and after, one for
those where only pixels differ, and one for those with only a contrast failure
before and after (which the change did not cause). Each row shows before,
after and the difference under a numbered caption, with a zoomed crop when the
change is narrow. notes.md beside the sheets gives each numbered row’s notes
and names the identical shots last: a surface you expected to change that shows
up there is one the change does not reach. render prints one line per shot;
read the details from rows.json. Read its --help for the spec and the set
script’s contract.
A spec needs only the surfaces the change reaches: the run covers eleven
built-in regimes (light and dark under Nova and classic, the same four in high
contrast, the native theme, an add-on theme and an approximation of Windows
high contrast chrome) and reverts every modified file linked into the objdir;
list a copied file in paths. A set lists its shots as selectors with an
optional pseudo-class lock and attributes to set, and records the colors,
borders, shadows and opacity of each element and of every rendered element
inside it; a set script reaches any other state and calls the same shot(). A
popup such as the app menu is a widget of its own that capture() cannot
draw, and the screen grab it needs runs only with --no-headless, which the
matrix does not pass. In a set script, record in
readings[name] any other value the change is meant to move, such as a
contrast() result for another subject, so notes.md can say what changed. A
reading that changed with no pixel changing means the value does not paint
where you expected, such as an icon that takes its color from fill and not
color.
Under Nova, contrast() judges no tab strip text, because its walk ends on a
background image on body.
To post the result, attach the sheets to the bug with the bug-filing skill’s
attach-to-bug.py, with notes.md as the comment text, and link that bug
comment from the revision if a reviewer needs it. A sheet over
Phabricator’s 8 MiB upload limit comes split into numbered parts.
Each
<path>is a file the browser loads. For a*.tokens.jsonchange, pass the generated token CSS that./mach buildtokenswrites into the tree.The change is committed, and each path exists at both ends. Pair an uncommitted change with the by-hand revert in
desktop-probing§6.A test file reached through several manifests runs once per manifest, and each run overwrites the last one’s output, so a pair keyed only on
before/aftercompares the last variant to run. Put the variant in the filename, from a marker each manifest sets (prefs = ["shots.variant=nova"], read withServices.prefs.getCharPref("shots.variant", "base")). The feature pref cannot tell them apart when the base manifest’s default equals another manifest’s value. The other variants are a free control (§5).
Run headless: the capture needs rendering, not a window manager or an input
seat. Shoot in the report’s regime (desktop-theme-colors §1), and in both
Linux native-theme branches where that section says to. A headless run paints
the host’s look and feel, not the user’s: before shooting a native-theme,
system-color or scrollbar change, read the host’s platform file in
desktop-probing, section HEADLESS (the host is the machine the run is
on, even when the report names another platform), for the prefs to pin or the
reason to take a real display rung (section DISPLAYS).
4. Draw what has no pixels
Each invisible kind gets a marker or a reading, the same in both halves, and the caption says what the marker means:
Hit area or border box: an outline,
element.style.outline = "2px solid red"withoutlineOffset = "-2px". The negative offset traces the box just inside its border edge and survives an ancestor withoverflow: hidden; at0pxit draws outside the border box, where such an ancestor clips it into two brackets. Where nothing clips and the border box itself is the thing shown,0pxtraces that edge truer; the inset one reads a couple of pixels small. Aborderwould change the box under measurement andbox-shadowblurs the edge. Outline every peer in the group so the spacing reads.Painted box, for a background the theme never sets: a marker
background-coloron the element.Clip boundary: an oversized marker child. An absolutely positioned child with a solid background, larger than the clipping box on every side, makes the clip edge the ink edge, so a moved clip or an
overflow-clip-marginshows as the marker’s edge. An element that renders no children, such as animg, cannot hold the marker: log the clip’s geometry and outline that box with an inset outline, or shoot the element again withoverflow: visibleand compare.Scroll extent: a reading. Log
scrollWidth/scrollHeightagainstclientWidth/clientHeightbeside an outline of the scrolled content.Focus target: an outline on
document.activeElementafter the focusing action, with its id logged.Stacking order: a reading. Log
elementFromPointover the overlap, or give the overlapping boxes distinct marker backgrounds.
Under forced colors the adjustment repaints a literal marker (a red outline
takes the text color, a marker background turns Canvas): mark per
desktop-probing §4.
5. Verify the pair actually differs
Look at every image before handing it over. A blank or half-painted shot, such
as a panel grabbed before it painted, differs from its partner under cmp and
pixel-diff like a real change. After a step that changes the state, such as
opening a popup or switching a mode, let the surface settle before the shot:
wait for its event (popupshown), finish its animations and wait two animation
frames.
cmp the two files. Differing byte sizes prove a difference; equal ones prove
nothing. An identical pair means one of three things; say which:
you captured the wrong state (§1), the usual cause;
the change genuinely does not affect that surface;
the run did not pick up the revert.
To tell them apart, log the computed value of each property the diff changes,
and the subject’s state, in each half. Values that differ mean the revert took;
a selector that matches (getMatchingCSSRules, desktop-probing §2
“Reach and read the state”) means the state was right. A control pair, a variant
the patch does not apply to (e.g. a different theme, a pref-off manifest),
guards the other direction: if it differs, the capture picks up run-to-run noise
and neither half is trustworthy.
To learn whether the files differ where you changed something,
./mach theme pixel-diff prints the count of differing pixels and their bounding
box, confined by --rect to the element; a zero count also reports the
differences under --threshold, which is §1’s “too small” case. --help
covers the rect’s origin, --dpr, --offset, --lines (what a caption for a
change one or two device pixels wide quotes), --diff-image (a picture of
where they differ, to attach beside the pair) and the exit codes; the host’s
platform file in desktop-probing, section GRABS, gives a grab’s scale.
It needs Pillow, which the desktop-theme site carries. A completed comparison
exits 0 when no pixel inside --rect differs by more than --threshold, 1 when
some do and 2 on an error. Mach itself exits 1 when it fails before the
comparison runs (e.g. the desktop-theme site fails to set up), so a 1 is a result only
when the count and bounding box were printed:
./mach theme pixel-diff \
after-<name>.png before-<name>.png --rect <x>,<y>,<w>,<h>
Chrome text antialiasing moves between runs, so a window-level pair routinely
differs across the whole toolbar while the surface under test is byte-identical.
--serve picks a fresh port per launch, so the address bar’s URL differs as
well; load a file:// URL or about:blank when the page needs no origin.
Report the --rect count for the element. When the change moves layout (every
row a pixel taller), no fixed rect isolates it and the count covers the whole
view; then the geometry table beside the shots is the evidence, not the count.
6. Read a reporter’s screenshot by sampling it
Exhaust the live measurements first; open someone else’s image only for what
their state alone can answer, and re-render a state you need as a pair (§1–§3).
Where the result depends on an OS backdrop this host cannot paint (the target’s
platform file, section COMPOSITING), a local capture shows nothing: sample
the reporter’s backdrop and composite the computed layers over it by
arithmetic instead, and say so.
Sample it rather than stare at it, through ./mach python --virtualenv desktop-theme
(§5 has the Pillow caveats):
from PIL import Image
im = Image.open("attachment.png").convert("RGB")
for y in range(y0, y1): # walk across the feature in question
print(y, im.getpixel((x, y))) # x inside the element under test
Walk a line across the boundary or fill in question and read where the color changes and what it changes to; for a clip, check whether the content’s own pixels continue past the box’s edge. Divide by the device pixel ratio before comparing against CSS pixels you measured yourself. A reporter’s image does not state its ratio: take it from a length you know in CSS pixels, such as a 1px border’s run of device rows or the size of an element you measured on your build in the reporter’s density and theme, and check it on a second one.
7. Clean up, and hand the images over
Clean up per desktop-probing §6. Name the files so the pair is obvious
(before-<variant>-<subject>.png), tell the user the directory, and say for
each pair what to look at and what it was taken in: the element state and
whether it was forced or the node hand-built, the color regime and theme,
feature prefs, density, device pixel ratio and platform.
Hand the pairs over as one image, to the user as on a bug, so that nobody has
to open the files one by one: scripts/sheet.py draws each
pair under a caption as before and after, with both enlarged below them when
the change is narrow or the capture small (--help). Caption each pair with
its surface and state, such as “Overflow panel, :focus-visible”, without a
number, since sheet.py numbers the pairs. Put what every pair shares, such as
the platform, theme and device pixel ratio, in --title, and what differs
between pairs, such as a grab on Xvfb beside a headless capture, in each
caption. Crop a screen grab to the popup with --rect N=X,Y,W,H, from its
getOuterScreenRect(), and any pair to the region it is about, such as a
narrow change in a capture where something wider differs too.
sheet.py draws the pixel difference beside a pair only when it is hard to
see: no channel differs by more than 64, or the differing pixels cover at most
1% of the capture. For the others it prints “no difference drawn” with the
numbers. When a user is there to ask, ask whether to add those differences,
and rerun with --diff all if they want them; without one, keep the default.
A matrix.py run already draws its sheets. sheet.py counts every differing
pixel (--threshold 0) where pixel-diff ignores differences up to 20, so its
count for a pair is the larger; say which threshold a quoted count used.
Attach the sheet with the bug-filing skill’s
.agents/skills/bug-filing/attach-to-bug.py, following that skill’s dry-run
steps. The comment names what every pair shares, as --title does, then gives
one line per numbered pair saying what changed from before to after and whether
the state was forced or the node hand-built. Attaching is outward-facing and permanent: confirm with the user
first and say which bug.