Protractor - compare numbers

2019-04-29 21:34发布

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?

3条回答
叛逆
2楼-- · 2019-04-29 21:41

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.

查看更多
够拽才男人
3楼-- · 2019-04-29 21:44

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

});

查看更多
Rolldiameter
4楼-- · 2019-04-29 22:05

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);
    })
});
查看更多
登录 后发表回答