量角器:count()在expect()之外不起作用

时间:2015-03-23 18:43:05

标签: angularjs testing protractor end-to-end

我试着写一个函数来等待某些元素的数量是一个特定的数字,我的代码是:

waitelementnum= function (num) {
    var elmnum=element.all(by.xpath("//div[@class='notification-content']")).count();
    browser.wait(function () {
        return (elmnum>num);
    }, 10000);
};

但它总是超时。然后我尝试打印计数结果,如:

console.log(element.all(by.xpath("//div[@class='notification-content']")).count());

没有打印出来;

但是当我尝试时:

expect(element.all(by.xpath("//div[@class='notification-content']")).count()).toEqual(2);

报道:

expecting 1 to be 2.

所以世界上有人告诉我我的功能有什么问题吗?

1 个答案:

答案 0 :(得分:1)

这是因为count() protractor 中的许多其他方法都会返回一个承诺expect()有意加强jasminewd以解决承诺。

如果您想查看实际的count()值,请解析承诺:

var notifications = element.all(by.xpath("//div[@class='notification-content']"));
notifications.count().then(function (value) {
    console.log(value);
});

另请参阅:Promises and the Control Flow