As a test running remote javascript execution and wanting to parse the boolean value returned, I am always returning null, instead of waiting for the javascript execution to finish and return a true/false.
My question is twofold: Why is this working this way, and what do I need to learn to have my test wait for the execution to return a value?
Presently, if I run my execute code in the console of a browser on the page in question, it works (returns true/false).
When I run my test through nightwatch, I can see my remote console.log debug statements logging a correct callback, but the callback happens after Nightwatch has already returned a null back to the runner.
The sanitized code takes the following form (note I've removed uninteresting logic chunks not related to my question):
browser
.api.execute(function(day) { // The following sanitized code does run correctly in the console
var sessions = document.querySelectorAll('.itemlst');
var clickFirstInstanceOfDay = function (d,callback) {
for (var i=0,found=false;i<sessions.length && found==false;i++) {
if (sessions[i].innerText.toString().indexOf(d)>-1) {
callback((containsCheckBeforeClick != containsCheckAfterClick));
}
}
}
clickFirstInstanceOfDay('Tuesday', function(bool){
console.log('>> clickFirstInstanceOfDay(callback): returning value ',bool);
return bool; // This verifies the right value is being returned
});
}, ['Tuesday'], function(res) { // This will always output null
console.log("<< NW test scope: The response back from the remote toggle check is ",res.value);
Any ideas? Do I really need to use promises instead of callbacks? Is there a Nightwatch library method I can employ to say "Hey do this and wait for a response"?