我编写了量角器自动化测试并遇到了问题。Wait命令实际上并不等待数组元素之一。请参阅以下示例:导航到网页后,我尝试等待第一个元素。
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. Trying to access element at index: 0, but there are only 0 elements that match locator by.repeater("category in listCtrl.categories")。
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”命令后测试通过:
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); }); }); });
另外,timeout参数也无济于事,它会忽略它并立即失败。
那么问题是如何等待数组的某个元素?我想念什么吗?谢谢。
设置 自定义的“期望条件” 以等待数组中元素的数量大于0:
function presenceOfAll(elementArrayFinder) { return function () { return elementArrayFinder.count(function (count) { return count > 0; }); }; }
用法:
browser.wait(presenceOfAll(category), 10000); browser.wait(presenceOfAll(category2), 10000);
为我工作。