我是量角器的新手,用于自动化 angularJs 应用程序。我正在尝试从元素列表中选择一个元素。我正在尝试进行错误处理,但由于承诺,没有任何工作符合我的预期。
在下面的代码中,如果我传递了一个无效的 categoryName,它永远不会打印错误,而是转到验证部分(期望)并失败。
请帮助我理解这一点以及如何解决此问题。我尝试使用回调,但没有运气。我也尝试过捕捉,仍然没有运气。感谢这里的任何帮助。谢谢
this.elements = element.all(by.css('.xyz'));
this.selectCategory = function (categoryName) {
this.elements.each(function (category) {
category.getText().then(function (text) {
if (text === categoryName) {
log.info("Selecting Category");
category.click();
}
}, function (err) {
log.error('error finding category ' + err);
throw err;
});
})
};
使用filter()
并检查匹配的元素数量:
var filteredCategories = this.elements.filter(function (category) {
return category.getText().then(function (text) {
return text === categoryName;
});
});
expect(filteredCategories.count()).toEqual(1);
filteredCategories.first().click();
如果要记录无效案例,可以这样做。
this.selectCategory = function (categoryName) {
var filteredCategories = this.categoryElements.filter(function (category) {
return category.getText().then(function (text) {
return text === categoryName;
})
})
filteredCategories.count().then(logInvalidCategory)
expect(filteredCategories.count()).toEqual(1);
filteredCategories.first().click();
}
function logInvalidCategory(count) {
if(count === 0) {
log.info("Invalid Category");
}
}