角度量角器中的错误处理

时间:2016-02-19 22:01:51

标签: javascript angularjs protractor webdriverjs

我是自动化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;
        });
    })
};

2 个答案:

答案 0 :(得分:1)

使用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();

答案 1 :(得分:1)

如果您想记录无效案例,您可以这样做。

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