I'm using Nightwatch for writing browser automation. I'm having an issue with executeAsync function of Nightwatch command.
Nightwatch documentation for executeAsync:
Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be asynchronous and the result of evaluating the script is returned to the client.
Asynchronous script commands may not span page loads. If an unload event is fired while waiting for a script result, an error should be returned to the client.
this.demoTest = function (browser) {
browser.executeAsync(function(data, done) {
someAsyncOperation(function() {
done(true);
});
}, [imagedata], function(result) {
// ...
});
};
The last optional parameter which should be a function is invoked when the Async task is completed.
How do I check if the Async task has started execution? I want to do something as soon as the Javascript body for the async task has been executed by the browser. Is there way to find out if executeAsync has started execution in Nightwatch code?
The executeAsync
call remains synchronous and behave like execute
in the excution flow.
To execute some code asynchroniously you first need to lauch you script with execute
and then wait for a result with executeAsync
.
Here is an example:
'Demo asynchronous script' : function (client) {
client.timeoutsAsyncScript(10000);
client.url('http://stackoverflow.com/');
// execute a piece of script asynchroniously
client.execute(function(data) {
window._asyncResult = undefined;
setTimeout(function(){
window._asyncResult = "abcde";
}, 2000);
}, ["1234"]);
// execute a task while the asynchroniously script is running
client.assert.title('Stack Overflow');
// wait for the asynchronous script to set a result
client.executeAsync(function(done) {
(function fn(){
if(window._asyncResult !== undefined)
return done(window._asyncResult);
setTimeout(fn, 30);
})();
}, [], function(result) {
// evaluate the result
client.assert.equal(result.value, "abcde");
});
client.end();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With