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#

shell
cd examples/library
node screenshots.mjs
terminal
$ 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, new

Inside 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.

A landing page on a laptop, three cards side by side The same page on a tablet The same page on a phone, the cards in one column

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.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;
}

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#

Edit this page on GitHub