Nightwatch returns 'null' back to runner before my .execute() block finishes

228 views
Skip to first unread message

Eric Mumford

unread,
Feb 22, 2017, 5:58:29 PM2/22/17
to NightwatchJs
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"?

David Linse

unread,
Feb 23, 2017, 7:15:15 AM2/23/17
to nightw...@googlegroups.com
Did you tried the `async` variant of `.execute` .. ?
http://nightwatchjs.org/api/executeAsync.html

regards
~david

--
You received this message because you are subscribed to the Google Groups "NightwatchJs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nightwatchjs...@googlegroups.com.
To post to this group, send email to nightw...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nightwatchjs/a3a9da2a-33d0-45d8-9b1f-d5b28266cc2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages