Loading custom commands: "unhandledRejection: There was an error while trying to load the file"
Description of the bug/issue
This is another instance of #3699, but several years later. When I attempt to load any custom command, I get the (unhelpful) error message below. Just want to get some helper functions together to make my tests look a little less crazy! I've tried writing the tests as classes and as classless function exports, but I get the same error regardless.
I've tried reinstalling nightwatch and upgrading from 3.9 to 3.12; neither helped.
Steps to reproduce
- Create a
commandsfolder, and put the custom command below in it. - Run
nightwatchcommand (see below). - Error appears.
Example of custom command:
module.exports = class FlatpickrPickTime {
async command() {
const time = this.element.find(
"[id='event.date_time.time_bounds.start_time']"
);
await time.click();
await time.getNextElementSibling().find(".arrow-up").click();
}
};
Sample test
describe('Ecosia.org Demo', function() {
before(browser => browser.navigateTo('https://www.ecosia.org/'));
it('Demo test ecosia.org', function(browser) {
browser
.waitForElementVisible('body')
.assert.titleContains('Ecosia')
.assert.visible('input[type=search]')
.setValue('input[type=search]', 'nightwatch')
.assert.visible('button[type=submit]')
.click('button[type=submit]')
.assert.textContains('.layout__content', 'Nightwatch.js');
});
after(browser => browser.end());
});
Command to run
npx nightwatch .nightwatch/examples/basic/ecosia.js --verbose
Verbose Output
Error
unhandledRejection: There was an error while trying to load the file /Users/admin/project/formtests/commands/pickTime.js:
Error: There was an error while trying to load the file /Users/admin/project/formtests/commands/pickTime.js:
at BaseLoader.handleModuleError (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:26:19)
at CommandLoader.requireModuleAsync (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:248:18)
at async CommandLoader.loadModuleAsync (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:230:5)
at async ApiLoader.addCommandDefinitionAsync (/Users/admin/project/formtests/node_modules/nightwatch/lib/api/index.js:366:5)
at async Promise.all (index 0)
Nightwatch Configuration
module.exports = {
src_folders: ["test"],
// page_objects_path: ["nightwatch/page-objects"],
custom_commands_path: ["./commands"],
// custom_assertions_path: ["nightwatch/custom-assertions"],
plugins: [],
globals_path: "",
webdriver: {},
test_workers: {
enabled: true,
},
test_settings: {
default: {
disable_error_log: false,
launch_url: "http://localhost:8080",
screenshots: {
enabled: false,
path: "screens",
on_failure: true,
},
desiredCapabilities: {
browserName: "firefox",
acceptInsecureCerts: true,
},
webdriver: {
start_process: true,
server_path: "",
},
},
firefox: {
desiredCapabilities: {
browserName: "firefox",
alwaysMatch: {
acceptInsecureCerts: true,
"moz:firefoxOptions": {
args: [
// '-headless',
// '-verbose'
],
},
},
},
webdriver: {
start_process: true,
server_path: "",
cli_args: [
// very verbose geckodriver logs
// '-vv'
],
},
},
chrome: {
desiredCapabilities: {
browserName: "chrome",
"goog:chromeOptions": {
// More info on Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/
args: [
//'--no-sandbox',
//'--ignore-certificate-errors',
//'--allow-insecure-localhost',
//'--headless=new'
],
},
},
webdriver: {
start_process: true,
server_path: "",
cli_args: [
// --verbose
],
},
},
},
};
Nightwatch.js Version
3.12.2 and 3.9.0 (not simultaneously; tried both because previous issues mentioned up or downgrading Nightwatch to fix it)
Node Version
18.20.7
Browser
Firefox 144.0
Operating System
Mac OS 12.6.5
Additional Information
No response
turned out there were 2 issues:
1. folder path was wrong - i had custom_commands_path pointing to ./commands but the actual files were in ./nightwatch/commands. nightwatch silently fails when it cant find the files
2. command file itself - make sure its just a normal .js file, not .ts. and export it like this:
module.exports = class FlatpickrPickTime {
async command() {
const time = this.api.element.find('[id="event.date_time.time_bounds.start_time"]');
await time.click();
const next = await time.getNextElementSibling();
await next.find('.arrow-up').click();
return this.api;
}
};
no fancy stuff, just plain commonjs export
after i fixed those two things it loaded fine. the verbose output was just showing the requirement failing, nothing else