nut.js

a cross-platform native UI automation / testing tool

README

nut.js (Native UI Toolkit)


Console - Developer Tool of the Week

Please visit

nutjs.dev

for detailed documentation and tutorials


About


logo


nut.js is a cross-platform native UI automation / testing tool.

It allows for native UI interactions via keyboard and / or mouse,
but additionally gives you the possibility to navigate the screen based on image matching.

Sponsoring


nut.js is developed with community in mind.

A huge "Thank you!" goes out to all sponsors who make open source a bit more sustainable!

Demo


Check out this demo video to get a first impression of what nut.js is capable of.

nut.js demo video

Tutorials


Please consult the project website at nutjs.dev for in-depth tutorials

Examples


nut-tree/trailmix contains a set of ready to use examples which demo the usage ot nut.js.

API Docs


nut.js provides public API documentation auto-generated by TypeDoc.

Community


Feel free to join our Discord community

Modules


This list gives an overview on currently implemented and planned functionality.
It's work in progress and will undergo constant modification.

Clipboard


- [x] Copy text to clipboard
- [x] Paste text from clipboard

Keyboard


- [x] Support for standard US keyboard layout
- [x] Support for multimedia keys

Mouse


- [x] Support for mouse movement
- [x] Support for mouse scroll
- [x] Configurable movement speed
- [x] Mouse drag

Window


- [x] List all windows
- [x] Retrieve active window
- [x] Retrieve window title
- [x] Retrieve window size and position

Screen


- [x] Retrieve RGBA color information on screen
- [x] Highlighting screen regions
- [x] Find a single or multiple occurrences of an image on screen (requires an additional provider package like e.g. nut-tree/template-matcher)
- [x] Wait for an image to appear on screen (requires an additional provider package like e.g. nut-tree/template-matcher)
- [x] Find a single or multiple occurrences of text on screen (\*)
- [x] Wait for a piece of text to appear on screen (\*)
- [x] Find a single or multiple windows on screen (\*)
- [x] Wait for a window to appear on screen (\*)
- [x] Hooks to trigger actions based on detected text, images or windows (\*)

(\*) Requires an additional provider package, visit nutjs.dev for more info

Integration


- [x] Jest
- [x] Electron
- [x] Custom log integration

Sample


The following snippet shows a valid nut.js example (using multiple addons):

  1. ```js
  2. "use strict";

  3. const {
  4.   mouse,
  5.   screen,
  6.   singleWord,
  7.   sleep,
  8.   useConsoleLogger,
  9.   ConsoleLogLevel,
  10.   straightTo,
  11.   centerOf,
  12.   Button,
  13.   getActiveWindow,
  14. } = require("@nut-tree/nut-js");
  15. const {
  16.   preloadLanguages,
  17.   Language,
  18.   LanguageModelType,
  19.   configure,
  20. } = require("@nut-tree/plugin-ocr");

  21. configure({ languageModelType: LanguageModelType.BEST });

  22. useConsoleLogger({ logLevel: ConsoleLogLevel.DEBUG });

  23. screen.config.autoHighlight = true;
  24. screen.config.ocrConfidence = 0.8;

  25. function activeWindowRegion() {
  26.   return getActiveWindow().then((activeWindow) => activeWindow.region);
  27. }

  28. (async () => {
  29.   await preloadLanguages([Language.English], [LanguageModelType.BEST]);
  30.   await sleep(5000);
  31.   const result = await screen.find(singleWord("@nut-tree/nut-js"));
  32.   await mouse.move(straightTo(centerOf(result)));
  33.   await mouse.click(Button.LEFT);
  34.   await screen.waitFor(singleWord("Native"), 15000, 1000, {
  35.     providerData: { partialMatch: true },
  36.   });
  37.   const content = await screen.read({ searchRegion: activeWindowRegion() });
  38.   console.log(content);
  39. })();
  40. ```

Installation


Prerequisites


This section lists runtime requirements for nut.js on the respective target platform.

Windows


In case you're running Windows 10 N and want to use ImageFinder plugins, please make sure to have the Media Feature Pack installed.

macOS


On macOS, Xcode command line tools are required.
You can install them by running

  1. ```bash
  2. xcode-select --install
  3. ```

Permissions:

nut.js requires the executing application, e.g. your terminal, to be given both Accessibility and Screen Recording permissions.

Starting with release 2.3.0, nut.js will check for and request these permissions automatically:

Popup requesting screen recording permissions


It will also give you a subtle hint in case permissions are lacking:

- Accessibility: ##### WARNING! The application running this script is not a trusted process! Please visit https://github.com/nut-tree/nut.js#macos #####
- Screen Recording: ##### WARNING! The application running this script is not allowed to capture screen content! Please visit https://github.com/nut-tree/nut.js#macos #####

Attention:

Prior to release 2.3.0 you'll have to grant these permissions manually.

Settings -> Security & Privacy -> Privacy tab -> Accessibility -> Add...

For example, if you want to execute your node script in e.g. iTerm2, you'd have to add iTerm.app to the list.
When running your script from a built-in terminal in e.g. VSCode or IntelliJ, you'd have to add the respective IDE.

accessibility permissions screen


Linux


Depending on your distribution, Linux setups may differ.

In general, nut.js requires

- libXtst

Installation on *buntu distributions:

  1. ```bash
  2. sudo apt-get install libxtst-dev
  3. ```

Setups on other distributions might differ.

Install nut.js


Running

  1. ```bash
  2. npm i @nut-tree/nut-js
  3. ```

or

  1. ```bash
  2. yarn add @nut-tree/nut-js
  3. ```

will install nut.js and its required dependencies.

Snapshot releases


nut.js also provides snapshot releases which allows to test upcoming features.

Running

  1. ```bash
  2. npm i @nut-tree/nut-js@next
  3. ```

or

  1. ```bash
  2. yarn add @nut-tree/nut-js@next
  3. ```

will install the most recent development release of nut.js.

Attention: While snapshot releases are great to work with upcoming features before a new stable release, it is still a snapshot release.
Please bear in mind that things might change and / or break on snapshot releases, so it is not recommended using them in production.