Protractor - compare numbers

2019-04-29 21:18发布

问题:

In my program I'm calculating two numbers, and I want to make sure that subtraction of them equals 1.

this is the code:

var firstCount=element.all(by.repeater('app in userApps')).count();
var secondCount=element.all(by.repeater('app in userApps')).count();

so far it's good- I'm getting the numbers. the problem comes next:

var sub=secondCount-firstCount;
expect(sub).toEqual(1);

I'm getting this error:

Expected NaN to equal 1.

any idea?

回答1:

Both firstCount and secondCount are promises that are needed to be resolved:

element.all(by.repeater('app in userApps')).count().then(function (first) {
    element.all(by.repeater('app in userApps')).count().then(function(second) {
        expect(first - second).toEqual(1);
    })
});


回答2:

It's possible to resolve only the first promise. Protractor adapts expect to "understand" promises. Refer https://github.com/angular/protractor/blob/master/docs/control-flow.md#protractor-adaptations and https://github.com/angular/protractor/issues/128.

element.all(by.repeater('app in userApps')).count().then(function (first) {
    // Do any changes here...
    var second = element.all(by.repeater('app in userApps')).count();
    // Here expect() resolves 'second'
    expect(second).toEqual(first + 1);
})

});



回答3:

You are doing absolutely right. But Before comparison, Check whether your result value is number type or not.

Example-

expect(sub).toEqual(jasmine.any(Number));

Then perform an operation for expected conditions.