量角器:“等待"不适用于“element.all"; [英] Protractor: 'wait' doesn't work with "element.all"

查看:12
本文介绍了量角器:“等待"不适用于“element.all";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写 Protractor 自动化测试并遇到了一个问题.等待命令 实际上并不等待其中一个数组元素.请参阅下面的示例:导航到网页后,我尝试等待第一个元素.

I write Protractor automation tests and faced an issue. Wait command doesn't actually wait for one of the array elements. See the example below: I try to wait for the first element after navigating to webpage.

var category = element.all(by.repeater('category in listCtrl.categories'));
var category2 = $$('.category-name.custom-tooltip-link.ng-binding');
var EC = protractor.ExpectedConditions;

describe('wait for the first category', function() {

    it('wait', function() {
        browser.get('http://www.deep.mg/');

        browser.wait(EC.visibilityOf(category.get(0)), 20000);

        browser.wait(EC.visibilityOf(category2.get(0)), 20000);
    });
});

但测试失败并出现以下错误:Failed: Index out of bound.尝试访问索引为 0 的元素,但只有 0 个元素与定位器 by.repeater(listCtrl.categories 中的类别") 匹配.

But test fails with the following error: Failed: Index out of bound. Trying to access element at index: 0, but there are only 0 elements that match locator by.repeater("category in listCtrl.categories").

错误不依赖于定位器类型,因为两者都会出现:by.repeater"和by.css".选择器没问题,添加 'sleep' 命令后测试通过:

Error doesn't depend on locator type, because appears for both: "by.repeater" and "by.css". The selectors are ok, test passes after adding 'sleep' command:

var category = element.all(by.repeater('category in listCtrl.categories'));
var category2 = $$('.category-name.custom-tooltip-link.ng-binding');
var EC = protractor.ExpectedConditions;

describe('wait for the first category', function() {

    it('wait', function() {
        browser.get('http://www.deep.mg/');

        browser.sleep(15000);

        browser.wait(EC.visibilityOf(category.get(0)), 20000);

        browser.wait(EC.visibilityOf(category2.get(0)), 20000);

        category.count().then(function(count1) {
            console.log(count1);  //count returns 5, which means there are actually elements in array
        });

        category2.count().then(function(count2) {
            console.log(count2);
        });
    });
});

也超时参数没有帮助,它只是忽略它并立即失败.

Also timeout parameter doesn't help, it just ignores it and fails immediately.

所以问题是如何等待数组的某个元素?我错过了什么吗?谢谢.

So the question is how to wait for a certain element of an array? Am I missing something? Thanks.

推荐答案

制作一个自定义预期条件来等待数组中的元素计数大于0:

Make a custom Expected Condition to wait for count of elements in an array to be more than 0:

function presenceOfAll(elementArrayFinder) {
    return function () {
        return elementArrayFinder.count(function (count) {
            return count > 0;
        });
    };
}

用法:

browser.wait(presenceOfAll(category), 10000);
browser.wait(presenceOfAll(category2), 10000);

为我工作.

这篇关于量角器:“等待"不适用于“element.all";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆