Last active
February 14, 2018 10:35
-
-
Save abiyasa/659536102a6f4e41a7499e429291f219 to your computer and use it in GitHub Desktop.
NightWatch.js execute() and executeAsync() with input script as a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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