Screenshots from a script
brokit as a Node.js library: a script that serves this folder, starts Chrome, takes a screenshot of one page on a laptop, a tablet and a phone, and compares each with the screenshot from the run before, with decodePng and diffImages.
Run it#
cd examples/library
node screenshots.mjs$ node screenshots.mjs
laptop 1280x800 at 1x → out/laptop.png, 1280×800, new
tablet 820x1180 at 1x → out/tablet.png, 820×1180, new
phone 393x852 at 2x → out/phone.png, 786×1704, newInside this repository, import ... from 'brokit' finds brokit itself. In your own project, install it first: npm install --save-dev brokit (until brokit is on npm, npm install --save-dev github:radufarcas10/brokit).
The first run writes out/laptop.png, out/tablet.png and out/phone.png. Each run after it compares the new screenshots with them, says which changed, and exits 1 if any did. In a real project, screenshots you compare against are committed. They're not committed here, since they depend on the fonts of the machine that made them.

What the script saved: laptop, tablet and phone.
The page#
index.html is a small landing page whose cards sit in three columns on a laptop and in one on a phone.
The script#
The script, screenshots.mjs:
// Screenshots of one page on three screens, each compared with the last run's.
//
// node screenshots.mjs
//
// Writes out/laptop.png, out/tablet.png and out/phone.png. When a shot from an
// earlier run is there, it's compared first; a changed one also gets a difference image
// (out/<name>.diff.png), and the script exits 1.
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { decodePng, diffImages, encodePng, launch, serve } from 'brokit';
const screens = [
{ name: 'laptop', size: '1280x800', dpr: 1 },
{ name: 'tablet', size: '820x1180', dpr: 1, mobile: true },
{ name: 'phone', size: '393x852', dpr: 2, mobile: true },
];
const out = join(import.meta.dirname, 'out');
const server = await serve(import.meta.dirname); // this folder, on 127.0.0.1
const browser = await launch({ gpu: 'software' }); // the same pixels on every machine
let changed = 0;
try {
await mkdir(out, { recursive: true });
for (const { name, size, dpr, mobile } of screens) {
const page = await browser.newPage({ size, dpr, mobile });
await page.goto(new URL('index.html', server.url).href, { ready: "document.readyState === 'complete'" });
const { png, width, height } = await page.screenshot();
await page.close();
const file = join(out, `${name}.png`);
const diffFile = join(out, `${name}.diff.png`);
const before = await readFile(file).catch(() => null);
await writeFile(file, png);
await rm(diffFile, { force: true });
let verdict = 'new';
if (before) {
// Up to 2 of 255 in a channel: what Chrome's drawing varies by, and nobody can see.
const d = diffImages(decodePng(before), decodePng(png), { tolerance: { max: 2 } });
if (d.pass) verdict = 'same as last run';
else {
changed++;
verdict = `changed: ${d.failures.join('; ')}`;
if (d.image) {
await writeFile(diffFile, encodePng(d.image));
verdict += ` (difference: out/${name}.diff.png)`;
}
}
}
console.log(`${name.padEnd(6)} ${size} at ${dpr}x → out/${name}.png, ${width}×${height}, ${verdict}`);
}
} finally {
await browser.close();
await server.close();
}
if (changed) {
console.log(`${changed} of ${screens.length} changed`);
process.exitCode = 1;
}serve(folder)serves a folder on 127.0.0.1, on a free port: only files inside it, no dotfiles, no directory listings.server.urlis where it is.launch({ gpu: 'software' })starts a headless Chrome that draws with SwiftShader, so the same page gives the same pixels on every machine. That's whatbrokit checkuses too.browser.newPage({ size, dpr, mobile })opens a tab with that screen:dpr: 2makes the phone's screenshot 786×1704 pixels for its 393×852 CSS pixels, andmobile: truegives it a phone's viewport and touch.page.goto(url, { ready })waits until the expression is truthy, then for the page's fonts and two frames, so the screenshot is the page as it settled.page.screenshot()resolves to{ png, width, height }: the PNG's bytes, in device pixels. It also takesselector,fullPageandscale, as thescreenshotstep does.decodePngturns PNG bytes into{ width, height, data }(RGBA), anddiffImagescompares two of them. Its result says whether theypassthe tolerance, and if not, why (failures), with a differenceimageto save withencodePng.
Every step is also a page method (click, tap, drag, key, type, emulate, waitFor, eval, ...), and runScenario(browser, scenario) runs a whole scenario.
Learn more#
- Library: what brokit exports, and each
pagemethod. - Checks and baselines: tolerance, difference images and
--gpu. - Steps: the steps, which are also
pagemethods.