Angular 量角器中的错误处理

Error Handling in Angular Protractor

我不熟悉用于自动化 angularJs 应用程序的量角器。我正在尝试 select 元素列表中的一个元素。我正在尝试进行错误处理,但由于承诺,没有像我预期的那样工作。

在下面的代码中,如果我传递了一个无效的类别名称,它永远不会打印错误,而是转到验证部分(预期)并失败。

请帮助我理解这一点以及如何解决这个问题。我尝试使用回调但不是运气。我也试过 try catch 但仍然没有运气。感谢这里的任何帮助。谢谢

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");
   }
}