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

百般思念 提交于 2019-12-11 03:41:13

问题


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');
      });
  }
};

回答1:


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:


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!




回答3:


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



来源:https://stackoverflow.com/questions/30789810/nightwatch-js-error-cannot-read-propery-equal-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!