Skip to content

Instantly share code, notes, and snippets.

@abiyasa
Last active February 14, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abiyasa/659536102a6f4e41a7499e429291f219 to your computer and use it in GitHub Desktop.
Save abiyasa/659536102a6f4e41a7499e429291f219 to your computer and use it in GitHub Desktop.
NightWatch.js execute() and executeAsync() with input script as a string
/**
* Examples how to inject scripts on NightWatch.js test.
* The following test will open a page, inject & run the scripts, and get the results.
* The injected script will be run synchronously & asynchronously.
*
* Both examples use script as a string since I could not find any example on this.
* Normally, you have the input script as a JS function but there are cases where you
* have to load & inject external script (e.g third party library).
*/
module.exports = {
'@tags': ['critical'],
'Visit main page': client => {
client.page.mainPage()
.visitPage();
},
'Execute script synchronously': client => {
const scriptString = `
return $('.welcome-message').text();
`;
const params = [];
client.execute(scriptString, params, result => {
console.log('SYNC RESULT: ', result);
});
},
'Execute script asynchronously': client => {
const scriptString = `
const done = arguments[arguments.length - 1];
setTimeout(() => {
done($('.welcome-message').text());
}, 2000);
`;
const params = [];
client.executeAsync(scriptString, params, result => {
console.log('ASYNC RESULT: ', result);
});
},
'Ends test properly': client => {
client.end();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment