量角器 - 比较数字

Protractor - compare numbers

在我的程序中,我正在计算两个数字,我想确保它们的减法等于 1。

这是代码:

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

到目前为止还不错 - 我正在获取数据。接下来的问题是:

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

我收到此错误:

Expected NaN to equal 1.

有什么想法吗?

firstCountsecondCount都是承诺需要解决

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

可以只解决第一个承诺。量角器使 expect 适应 "understand" 承诺。参考 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);
})

});

你做的完全正确。但 比较前请检查你的结果值是否为数字类型

示例-

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

然后执行预期条件的操作。