[英]Assert not available in page object? - Cannot read property 'ok' of undefined.
I have a small nightwatch test that is reading a price and checks if the value is a positive number. 我有一个小型的夜视测试,正在读取价格,并检查该值是否为正数。
browser.getText ('.price', function(result) {
var myPrice = Number(result.value.replace('-.','').trim());
browser.assert.ok(isPositive(myPrice), 'price is a positive number');
});
This works perfectly fine if i put it directly in the test itself, but when i move it in my page object file as a function (like this): 如果我将其直接放在测试本身中,则可以正常工作,但是当我将其作为函数移到页面目标文件中时(如下所示):
checkPrice: function (){
this.expect.element('@price').text.to.be.visible.before(1000);
this.getText ('@price', function(result) {
var myPrice = Number(result.value.replace('-.','').trim());
this.assert.ok(isPositive(myPrice), 'price is a positive number');
});
},
it throws this error: "Cannot read property 'ok' of undefined" . 它将引发以下错误: “无法读取未定义的属性'ok'” 。 I have tried to change my check using assert.equal
instead, but it gives the same error, that the property is not accessible. 我尝试改为使用assert.equal
更改检查,但是它给出了相同的错误,即该属性不可访问。 I've read here ( NIghtwatch.js error: "Cannot read propery 'equal' of undefined ) that there might be some issue with the assert
scope as it's a member of client
, but if i use client
instead of this
it will not see the assert at all anymore: " Cannot read property 'assert' of undefined" . 我已经在这里阅读( NIghtwatch.js错误:“无法读取未定义的属性'等于'” ),因为它是client
的成员,所以assert
范围可能存在一些问题,但是如果我使用client
而不是this
,它将不会看到不再存在断言:“ 无法读取未定义的属性'assert'” 。
I have also read that this
can be a tricky thing to use sometimes, but i have limited javascript programming skills so there might be something that i miss from this point of view. 我还读到,有时候使用this
可能是一件棘手的事情,但是我的JavaScript编程技能很有限,因此从这个角度来看我可能会错过一些东西。
Could you please give me a suggestion why this doesn't work and how can i try in order to fix it? 您能否给我一个建议,为什么这不起作用,我如何尝试修复它?
this
changes when you enter the scope of the inner function, so you need to save a reference to it. 当您输入内部函数的范围时, this
发生变化,因此您需要保存对其的引用。
checkPrice: function (){
var _this = this;
this.expect.element('@price').text.to.be.visible.before(1000);
this.getText ('@price', function(result) {
var myPrice = Number(result.value.replace('-.','').trim());
_this.assert.ok(isPositive(myPrice), 'price is a positive number');
});
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.