node.js - Protractor :过滤直到找到第一个有效元素

标签 node.js protractor

我正在一个网站上进行 e2e 测试,该网站包含一个表,我需要迭代该表“直到”找到一个在单击它时不会失败的表。

我尝试使用过滤器并且它正在工作:

this.selectValidRow = function () {
    return Rows.filter(function (row, idx) {
        row.click();
        showRowPage.click();
        return errorMessage.isDisplayed().then(function (displayed) {
            if (!displayed) {
                rowsPage.click(); // go back to rows Page, all the rows
                return true;
            }
        });
    }).first().click();
}; 

这里的问题是它正在迭代所有可用行,而我只需要第一个有效的行(不显示errorMessage)。

我当前方法的问题是它花费的时间太长,因为我当前的表可能包含数百行。

是否可以过滤(或不同的方法)并在第一个有效出现出现时停止迭代?或者有人可以想出更好的方法吗?

最佳答案

如果您更喜欢使用非 Protractor 方法来处理这种情况,我建议 async.whilst 。 async 是一个非常流行的模块,您的应用程序很可能正在使用它。我在编辑器中编写了以下代码,但它应该可以工作,您可以根据您的需要对其进行自定义。希望您了解我在这里做什么。

var found = false, count = 0;
async.whilst(function iterator() {
   return !found &&  count < Rows.length;
}, function search(callback) {
    Rows[count].click();
    showRowPage.click();
    errorMessage.isDisplayed().then(function (displayed) {
        if (!displayed) {
            rowsPage.click(); // go back to rows Page, all the rows
            found = true; //break the loop
            callback(null, Rows[count]); //all good, lets get out of here
        } else {
           count = count + 1;
           callback(null); //continue looking
        }
    });
}, function aboutToExit(err, rowIwant) {
    if(err) {
      //if search sent an error here;
    }
    if(!found) {
      //row was not found;
    }
    //otherwise as you were doing
    rowIwant.click();
});

关于node.js - Protractor :过滤直到找到第一个有效元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030508/

相关文章:

angularjs - 如何检测我处于测试模式angularjs

javascript - 在 Protractor 中断言数组列表中值的部分关键字

javascript - 使用 Protractor 测试 Angularjs 警报

javascript - Koa Auth 流程与 compose

javascript - 通过 Node Express 将对象作为字符串而不是 JSON 发送

node.js - 使用 connect-assetmanager 缩小 bootstrap.js 会导致语法错误。为什么?

angularjs - 可以/如何将 Protractor 与 chutzpah 一起使用?

node.js - 错误 : 14 UNAVAILABLE: No connection established - Cloud Firestore

node.js - 与 socket.io 的私有(private)聊天

javascript - Protractor :捕获 AssertionError