Skip to content

Testing your UIs

A screen test renders a UI, plays the mouse and the keyboard on it, and checks the pixels. The testkit does this offscreen, in a JUnit test, and gives the same pixels on every run.

You will learn

  • What your build needs to test screens
  • How to render a UI and save the picture
  • How to click, type and wait in a test
  • How to compare a picture with a reference
  • How the testkit keeps every run identical

Screen tests need three things on the test classpath:

Installation shows where the jars come from, and how to give the tests the dev jar while the application ships the prod jar.

The snapshot backend draws on a hidden surface, so the machine that runs the tests needs a graphics driver, even a software one on a CI server.

A SnapshotRunner starts the snapshot backend once for the test class, and stops it at the end:

@BeforeClass
public static void start() {
MenuSnapshotTest.runner = SnapshotRunner.start(new Lwjgl3SnapshotBackend());
}
@AfterClass
public static void stop() {
MenuSnapshotTest.runner.stop();
}

Full file: MenuSnapshotTest.java

start(...) creates a hidden 1920×1080 surface, registers the backend with the testkit’s own UI bridge, window, clock and audio bridges, and loads JOID. stop() closes every UI, destroys the surface and gives back the bridges that were there before.

Each test gives the runner a list of commands and gets the pictures back by name:

@Test
public void showsThePlayButton() {
final SnapshotImage shot = MenuSnapshotTest.runner
.execute("ui " + RunningMenuUI.class.getName(), "shot menu")
.get("menu");
shot.write(new File("build/snapshots/menu.png"));
Assert.assertEquals(0xFF262B35, shot.getPixels()[520 * shot.getWidth() + 870]);
}

Full file: MenuSnapshotTest.java

execute(...) starts from a clean state: no UI open, the window at 1920×1080, no key held, the clock at its start. ui <class> creates the UI with its constructor without argument, of any visibility, and shows it alone, with the mouse outside the window; shot menu captures the window. open <class> creates a UI the same way but opens it with JOID.open, over what is already open: use it for a popup or an overlay. Both play the opening transition: add a wait before the shot to capture the UI fully open.

A SnapshotImage holds the pixels as ARGB int values, row after row, from the top-left corner. write(file) saves it as a PNG, to look at it or to keep it as a reference.

A dark grey Play button on a dark background

The shot is the window as the user sees it, so a reference compares every pixel of the screen.

Commands play the input of a user, one line each:

@Test
public void startsTheGameOnClick() {
final SnapshotImage shot = MenuSnapshotTest.runner.execute(
"ui " + RunningMenuUI.class.getName(),
"moveto 960 540 300",
"press LEFT",
"release LEFT",
"wait 100",
"shot clicked"
).get("clicked");
final File references = new File("src/test/resources/snapshots", MenuSnapshotTest.runner.getRenderer());
final File reference = new File(references, "clicked.png");
if (!reference.exists() && SnapshotSettings.isRecord()) {
shot.write(reference);
}
final SnapshotDifference difference = shot.compare(SnapshotImage.read(reference), 2);
Assert.assertEquals(0, difference.getPixels());
}

Full file: MenuSnapshotTest.java

The Play button now reads Playing

The shot clicked: the pointer moved to the button, pressed and released, and the label changed.

moveto <x> <y> <ms> moves the pointer over that time, in screen pixels, one frame every 16 ms; press LEFT and release LEFT click; wait <ms> advances the clock by exactly that time, one frame every 16 ms. Other commands type text, press keys, scroll, resize the window, zoom, turn the dev mode on, check the cursor and mask what changes between runs: SnapshotRunner.execute lists every command.

compare(reference, tolerance) returns a SnapshotDifference:

  • getPixels() counts the pixels whose largest channel difference is above the tolerance;
  • getMaximum() gives the largest channel difference found, from 0 to 255.

isSame(other) checks that two pictures are identical.

To create a reference, let the test write the shot once, check it by eye, then copy it into your test resources. Drivers round colors differently, so a tolerance of 2 or 3 keeps a reference valid from one machine to another; record the references on the machine that runs the tests when it uses another GPU. getRenderer() of the runner names that GPU: the test above keeps one folder of references per GPU, and records a missing reference when SnapshotSettings.isRecord is true, as on a CI server.

The testkit removes what changes from one run to the next:

  • Time: the runner drives a manual clock that starts at 2025-01-01 00:00 UTC and moves only with wait and moveto. JOID reads every time on BridgeHandler.CLOCK, so animations, transitions, tasks, hover fades and the colors that change on their own follow it.
  • Loading: before each shot, the runner waits until every resource is loaded, then until two frames in a row are identical. It gives up after 30 seconds with A resource never finished loading in 30s, ….
  • Network: a resource read from an http or https URL is downloaded once into the snapshot area of the cache folder, then read from there, so later runs work offline.
  • Sound: the audio bridge of the testkit plays nothing, so no sound device is needed.
  • Dev tools: the dev mode stays off unless a dev true command turns it on.

Content that cannot be the same on two runs, such as the current date, goes under a mask.

  • Tests need the dev jar of the backend, which holds the snapshot backend, plus joid-tool-testkit and JUnit 4.
  • SnapshotRunner.start(...) once per test class, stop() at the end.
  • execute(...) plays commands from a clean state and returns the shots by name.
  • Commands play the user: ui, moveto, press, release, wait, shot; the reference lists the others.
  • compare(reference, tolerance) counts the pixels that differ; write the shot once to create the reference.
  • Time moves only with wait and moveto: read the time from BridgeHandler.CLOCK, never from the system.

Type in a field. Test that typing Ada in a text field at the center of the window shows it. Which commands do you play?

Solution
final String ui = "ui " + FormUI.class.getName();
runner.execute(ui, "moveto 960 540 200", "press LEFT", "release LEFT", "type Ada", "shot typed");

The click focuses the field at the center of the window, type sends the three characters, and the shot shows the result.

A popup fully open. A popup plays its opening transition, 130 ms by default (Transitions). Capture it fully open.

Solution

Play a wait at least as long as the transition, such as wait 200, between the open <class> and the shot. The clock of the test stands still between commands, so the transition advances only with wait.

Report a problem with this page