đŸĨœNut.js

Nuts.js is a new desktop automation library that we encourage use of as a test driver for simple applications. It allows you to both search for images on screen and interact with the desktop through keyboard, mouse, and more.

Classic approaches to desktop UI testing often include unique approaches per operating system and rely on selectors that are not human-readable. Nut.js on the other hand, is cross-platform and uses screenshots instead, which are easy enough for anyone to understand.

Testing with Nut.js

Most tests in Nut.js follow a common pattern:

  1. Wait for an image to appear

  2. Click or validate the image

  3. Go to 1

Here's what this looks like in code:

// Load the image in Nut.js
let img1 = imageResource('button.png');

// Wait for the image
const img = await screen.waitFor(img1);

// Move mouse to the center of the image
await mouse.move(straightTo(centerOf(img)));

// Click the mouse
await mouse.leftClick();

If any of these actions fail, like the image is not found, Nut.js will throw an error.

Using a test framework

Now, you'll likely want to make use of a testing framework like Mocha (our recommendation) or Jest. Here's an example of what that looks like:

const { screen, Region, imageResource } = require("@nut-tree/nut-js");
require("@nut-tree/template-matcher");

describe("Screen test", () => {

    describe("waitFor", () => {
        it("should wait for a match", async () => {
            await screen.waitFor(imageResource("mouse.png"), 2500);
        });
    });
    
});

Previewing images in VS Code

There is a helpful VS Code extension called Image Preview that will display previews of your image within the VS Code gutter as well as on hover.

Last updated