簡體   English   中英

角度量角器中的錯誤處理

[英]Error Handling in Angular Protractor

我是量角器的自動化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");
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM