【问题标题】:Protractor:count() doesn't work outside of expect()Protractor:count() 在 expect() 之外不起作用
【发布时间】:2015-03-23 18:43:05
【问题描述】:

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

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

但它总是超时。然后我尝试打印 count 的结果,例如:

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.

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

【问题讨论】:

    标签: angularjs testing protractor end-to-end


    【解决方案1】:

    这是因为count()protractor 中的许多其他方法一样返回一个承诺expect()jasminewd 有意增强以隐式地解决承诺。

    如果您想查看实际的count() 值,您可以使用.then() 明确地解决承诺:

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

    另请参阅:Promises and the Control Flow

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 2018-08-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多