Protractor - compare numbers

匿名 (未验证) 提交于 2019-12-03 08:48: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?

回答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.



转载请标明出处:Protractor - compare numbers
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!