简体   繁体   中英

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: . I have tried to change my check using assert.equal instead, but it gives the same error, that the property is not accessible. 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: " .

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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