简体   繁体   English

NIghtwatch.js错误:“无法读取未定义的属性'等于'

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

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. 我正在尝试运行一个简单的Nightwatch.js测试以登录,告诉表单记住该登录,然后测试cookie以查看是否存在布尔值。 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() . 绑定到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');
      });
  }
};

A function is a lexical scope in javascript, so it sets a new reference to the keyword this . 函数是javascript中的词法范围,因此它为关键字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 好像assertclient的成员,因此如果框架支持,则调用应类似于

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

As per the latest Nightwatch version, cookie support is now native! 根据最新的Nightwatch版本,cookie支持现在是本地的!

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'? 我唯一要注意的就是问-您确定要检查字符串值'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. 调试的另一点建议是,要确保您正在访问正确的cookie,也请检查它是否是.name

Finally, try chaining the getCookie command by removing the semicolon after waitForElementVisible so your code looks like this: 最后,尝试通过在waitForElementVisible之后删除分号来链接getCookie命令,以便您的代码如下所示:

  .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. 我不约JS范围的建议的最佳人选,但它看起来好像上下文this可能是太嵌套一个级别。 Try replacing it with client if you are still having trouble. 如果仍然遇到问题,请尝试用client替换它。

Luck! 运气!

事实证明Nightwatch.js尚不支持此功能: https : //twitter.com/nightwatchjs/status/434603106442432512

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM