-
OS: Linux I'm looking to use nut.js to automate Godot editor (and project manager) UI testing. So far, it seems to work fine, but I'm looking to write compact code when possible. Here, I want to trigger a mouse click on a button that is found from an image. For this purpose, I want to find the center of the found image on the screen: const newProjectButton = await screen.find(imageResource("project_manager/new_project_button.png"));
console.log(newProjectButton);
console.log(centerOf(newProjectButton)); Full .js file for contextconst fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
const { screen, imageResource, mouse, keyboard, sleep, Key, Point, straightTo, left, right, up, down, centerOf } = require('@nut-tree/nut-js');
require("@nut-tree/template-matcher");
// Path to the compiled Godot editor binary to run.
const GODOT_EDITOR_BINARY_PATH = `${__dirname}/../../bin/godot.linuxbsd.tools.64.llvm`;
// Path to the self-contained file to create.
const SELF_CONTAINED_FILE_PATH = `${path.dirname(GODOT_EDITOR_BINARY_PATH)}/._sc_`;
// The time Godot takes to start (in milliseconds). Adjust this depending
// on your system performance.
// TODO: Use image finding to remove the reliance on a fixed delay.
const GODOT_START_DELAY = 2000;
(async () => {
screen.config.resourceDirectory = `${__dirname}/images`;
screen.config.autoHighlight = true;
// Enable self-contained mode to avoid conflict with user settings.
fs.open(SELF_CONTAINED_FILE_PATH, 'w', (err, file) => {
if (err) {
throw err;
}
console.log('Self-contained mode enabled.');
});
console.log('Running Godot editor binary...');
childProcess.execFile(GODOT_EDITOR_BINARY_PATH);
await sleep(GODOT_START_DELAY);
await keyboard.pressKey(Key.Escape);
try {
const newProjectButton = await screen.find(imageResource("project_manager/new_project_button.png"));
console.log(newProjectButton);
console.log(centerOf(newProjectButton));
await mouse.setPosition(centerOf(newProjectButton));
} catch (err) {
console.error(err);
}
// Disable self-contained mode once tests have run.
fs.unlink(SELF_CONTAINED_FILE_PATH, (err) => {
if (err) {
throw err;
}
console.log('Self-contained mode disabled.');
});
})(); The first
Is there a way to "unwrap" the promise to avoid callback hell? I would rather not have deeply nested |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I wrote my own function for it, which is pretty simple as you'd expect: function centerOf(region) {
return new Point(region.left + region.width * 0.5, region.top + region.height * 0.5);
} I wonder if this could be provided in nut.js itself. |
Beta Was this translation helpful? Give feedback.
I wrote my own function for it, which is pretty simple as you'd expect:
I wonder if this could be provided in nut.js itself.