1

I am new to Nightwatch.js and am moving some tests over from the WD.js library. One nifty feature of WD.js is .waitForConditionInBrowser(), which waits for an arbitrary JavaScript snippet to be truthy. I am however unable to figure out how to interact with the JavaScript this way in Nightwatch.js, or if it's even possible (after perusing the docs). Here is an example of what I want to do:

Let's say I am loading a page where after about 3 seconds my_js_var becomes 5. In WD.js, it would look like this (wait max. 5 seconds):

.get(my_page_url)
.waitForConditionInBrowser('my_js_var === 5', 5000)
// continue if my_js_var === 5 within 5 s

I even tried doing something more dirty in Nightwatch, but I am probably misunderstanding how interaction with page JS works at all:

.url(my_page_url)
.pause(5000)
.execute(function() {
    return (my_js_var === 5);
})
// continue if my_js_var === 5 within 5 s

But it just continues no matter what is returned, and it's unclear where the result goes to.

Is there a proper waitFor function in Nightwatch, and if not, how do I run basic assertions on evaluated JavaScript code?

1 Answer 1

2

It's been a while, but I used to do something like this:

exports.command = function(callback) {
  var self = this;
  this.timeoutsAsyncScript(5000, function() {
    this.executeAsync(function(done) {
        if(my_js_var === 5) {
          return my_js_var === 5
        }
        else {
          done();
        }
      },
      [], 
      function(result) {
        if(typeof(callback) === "function") {
          callback.call(self, result);
        }
      });
  });
  return this;
};

The timeout in timeoutsAsyncScript lets you assure a reasonable wait time so that the test can fail accordingly when it should fail.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.