简体   繁体   English

断言页面对象中不可用? -无法读取未定义的属性“ ok”。

[英]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.

相关问题 “无法读取未定义的属性。” - "Cannot read property of undefined." 无法读取未定义的属性“材料”。 用嵌套对象反应useState - Cannot read property 'Materials' of undefined. React useState with a nested object 无法读取未定义的属性“ removeClass”。 Java脚本 - Cannot read property “removeClass” of Undefined. Javascript 无法读取未定义的属性。 如何定义? - Cannot read property of undefined. How to define? 无法读取未定义的属性“长度”。” - Cannot read property 'length' of undefined." “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 无法读取未定义的属性“indexOf”。 为什么是未定义的? - Cannot read property 'indexOf' of undefined. Why is it undefined? 无法读取未定义的属性。 但属性是进口 class 的 static function - Cannot read property of undefined. But property is a static function of an imported class 无法从未定义中读取属性“ 0”。 错误的Google Apps脚本 - Cannot read property “0” from undefined. Error Google Apps Script TypeError:无法读取未定义的属性“ get”。 是npm的问题吗? - TypeError: Cannot read property 'get' of undefined. Is npm the issue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM