gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:21:49 33 4
gpt4 key购买 nike

我有一个小型的夜间测试,它正在读取价格并检查该值是否为正数。

    browser.getText ('.price', function(result) {
var myPrice = Number(result.value.replace('-.','').trim());
browser.assert.ok(isPositive(myPrice), 'price is a positive number');
});

如果我将它直接放在测试本身中,那么效果非常好,但是当我将它作为函数移动到页面对象文件中时(如下所示):

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

它抛出此错误:“无法读取未定义的属性“ok””。我尝试使用 assert.equal 更改我的检查,但它给出了相同的错误,即该属性不可访问。我在这里读到( NIghtwatch.js error: "Cannot read propery 'equal' of undefined ), assert 范围可能存在一些问题,因为它是 client 的成员,但如果我使用 client 而不是 this ,它将不再看到断言:“无法读取未定义的属性'assert'”

我还读到,this 有时使用起来可能很棘手,但我的 JavaScript 编程技能有限,所以从这个 Angular 来看,我可能会错过一些东西。

您能否给我一个建议,为什么这不起作用以及我该如何尝试修复它?

最佳答案

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

关于javascript - 断言在页面对象中不可用? - 无法读取未定义的属性 'ok'。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35460883/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com