flutterware
All guides
Screens and tests

Pages

See what a branch changed in the HTML your server renders: its pages, the emails it sends, any page in any state you can serve. fw compare runs a command of yours on your branch and on its base, photographs every sample in Chrome, and reports each one that changed, beside your previews and scenarios.

Turn it on#

// tool/flutterware.dart
const server = Pkg('server');

void main() => Flutterware.configure((fw) {
  fw.use(
    Pages(
      packages: [
        .new(
          server,
          serve: StackRun.script('tool/pages_server.dart'),
          inputs: ['web/'],
        ),
      ],
    ),
  );
});

serve is the command that serves your samples, run in the package's folder. StackRun.script runs a Dart file with the SDK flutterware runs on. StackRun.command(['node', 'pages.js']) runs anything else.

The command#

The command starts an HTTP server on a free port, prints one line with the address it serves at, and serves until its standard input closes:

fw-pages-origin http://[::1]:51234

Bind a loopback address, such as ::1 or 127.0.0.1, and print that address. localhost is both of them, and another program on the machine can hold the same port on the other one, such as a container's published port or another project's test server. flutterware could then reach that program instead of yours.

A command that prints only its port, fw-pages-port 51234, is reached at http://localhost:51234.

It then answers three kinds of request:

{
  "samples": [
    {"id": "site/home", "name": "Home", "group": "Site", "path": "/site/home"},
    {
      "id": "email/welcome",
      "name": "Welcome",
      "group": "Emails",
      "path": "/email/welcome",
      "subject": "Welcome to the shop",
      "text": "Hello,\nYour account is ready.",
      "viewports": [{"name": "email", "width": 600, "height": 800}]
    }
  ]
}

id and path are required. An id can't contain @. name and group are what the comparison calls the sample. For an email, subject and text (its plain-text part) are compared word for word.

A Dart command can be this short:

// tool/pages_server.dart
import 'dart:io';

import 'package:shelf/shelf_io.dart' as shelf_io;

import 'package:my_server/pages.dart';

Future<void> main() async {
  var server = await shelf_io.serve(
    samplesHandler(),
    InternetAddress.loopbackIPv6,
    0,
  );
  print('fw-pages-origin http://[::1]:${server.port}');
  await stdin.drain<void>();
  await server.close(force: true);
}

Make samples from made-up data, with fixed dates, so a page draws the same thing every time it is served. A sample that answers anything but a 2xx fails on its own, and the rest of the samples are still compared.

Viewports#

Every sample is photographed at a phone's width (390×844) and a computer's (1280×800). The picture is the whole page, however tall it is. Change the sizes for a package with viewports:, or for one sample with viewports in index.json:

.new(
  server,
  serve: StackRun.script('tool/pages_server.dart'),
  viewports: [
    PageViewport('phone', width: 390, height: 844),
    PageViewport('tablet', width: 820, height: 1180, scale: 2),
  ],
)

Each sample at each viewport is one row, named <id>@<viewport>: site/home@phone.

What is compared#

Console errors, uncaught exceptions and requests that failed or answered 4xx or 5xx are noted on the row. A row notes them when they differ between the two sides.

When a package is skipped#

With StackRun.script, a package is skipped when nothing its script imports changed between the two sides, its pubspec.yaml and lockfile included. Anything else your pages read from disk, such as stylesheets, fonts, images or templates, belongs in inputs:, relative to the package. A change to a file that is neither imported nor listed there is reported as no change.

A StackRun.command always runs. Starting a side takes about two seconds.

Chrome#

Pages are photographed in chrome-headless-shell, the headless build of a pinned version of Chrome for Testing, the same on both sides. It draws without a display, so it also works in a macOS account that is logged in but not on screen, such as a CI runner started as a launch agent, where Chrome itself stops after its first page.

The first comparison with pages on a machine downloads it, about 100 MB, into the user cache, where every project and worktree shares it:

In CI, cache that folder between runs.

If the browser exits or stops answering, the pages it had not photographed yet are failed rows saying so, the pages half gets a note, and the rest of the comparison still finishes. fw compare then exits 1.

Look at one page#

fw run pages screenshot --sample=site/home --viewport=phone

It starts the command, photographs that sample the way a comparison does, and writes a PNG under the package's build/flutterware/pages/. An unknown sample is refused with the list of samples the command serves.

Reference#

Comparison for the report, the page and the pull-request comment, and the pages actions in the capabilities reference.

Edit this page on GitHub