NIghtwatch.js error: "Cannot read propery 'equal' of undefined

3.1k Views Asked by At

I'm trying to run a simple Nightwatch.js test to login, tell the form to remember the login, and then test the cookie to see if a boolean value exists. Every time I try to run the test, I get an Error:

"Cannot read propery 'equal' of undefined"

which is tied to the callback function for client.getCookie().

Can anyone help me to understand how to fix this error?

Here is my code:

module.exports = {
  'Test "Keep me logged in." ' : function (client) {
    client
      .windowMaximize()
      .url('www.mysite.com')
      .setValue('#login > form > table > tbody > tr:nth-child(1) > td:nth-child(2) > input[type="text"]', 'testuser')
      .setValue('#login > form > table > tbody > tr:nth-child(2) > td:nth-child(2) > input[type="password"]', 'testpass')
      .click('#login > form > table > tbody > tr:nth-child(4) > td > label > input[type="checkbox"]')
      .click('#login > form > table > tbody > tr:nth-child(5) > td > input[type="submit"]')
      .waitForElementVisible('body', 2000);

      client.getCookie('RememberMe', function(result){ 
            this.assert.equal(result.value, 'True');
      });
  }
};
3

There are 3 best solutions below

3
On BEST ANSWER

A function is a lexical scope in javascript, so it sets a new reference to the keyword this.

Try something like:

  client.getCookie('RememberMe', function(result){ 
      this.assert.equal(result.value, 'True');
  }.bind(this))

Edit:

Seems like assert is a member of client, so the call, if supported by the framework should be something like

  client.getCookie('RememberMe', function(result){ 
      client.assert.equal(result.value, 'True');
  })
2
On

It turns out that Nightwatch.js doesn't yet support this feature: https://twitter.com/nightwatchjs/status/434603106442432512

0
On

As per the latest Nightwatch version, cookie support is now native!

Check the docs here. My only word of caution would be to ask - are you sure you really want to check for the string value 'True'? Or are you rather checking for a boolean value, which would look like this:

  client.getCookie('RememberMe', function(result){ 
        this.assert.equal(result.value, true);
  });

Another little bit of advice for debugging is, to make sure you are accessing the right cookie, check for it's .name too.

Finally, try chaining the getCookie command by removing the semicolon after waitForElementVisible so your code looks like this:

  .waitForElementVisible('body', 2000)
  .getCookie('RememberMe', function(result){ 
        this.assert.equal(result.value, 'True');
  });

I am not the best person for advice about JS scopes, but it looks as if the context of this might be one level too nested. Try replacing it with client if you are still having trouble.

Luck!