Node.js 量角器:筛选直到找到第一个有效元素

Node.js 量角器:筛选直到找到第一个有效元素,node.js,protractor,Node.js,Protractor,我在一个站点上做e2e测试,该站点包含一个表,我需要迭代“直到”找到一个当我点击它时不会失败的表 我使用filter尝试了它,它正在工作: this.selectValidRow = function () { return Rows.filter(function (row, idx) { row.click(); showRowPage.click(); return errorMessage.isDisplayed().then(fu

我在一个站点上做e2e测试,该站点包含一个表,我需要迭代“直到”找到一个当我点击它时不会失败的表

我使用
filter
尝试了它,它正在工作:

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

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


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

你是对的,
filter()
和其他内置量角器“函数编程”方法无法解决“在第一个有效出现时停止迭代”的问题案例您需要“take some elements while some condition求值为true”(如Python世界中的
itertools.takewhile()

幸运的是,您可以扩展
ElementArrayFinder
(最好在
onPrepare()
)并添加
takewhile()
方法:

请注意,我建议将其内置,但功能请求仍处于打开状态:


如果您喜欢使用非量角器方法来处理这种情况,我建议您。异步是一个非常流行的模块,应用程序很可能正在使用它。我在编辑器中编写了下面的代码,但它应该可以工作,您可以根据需要自定义它。希望你知道我在这里做什么

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();
});
var found=false,count=0;
while(函数迭代器(){
return!找到并计数
但您如何知道这是一个有效的行呢?只需单击它并看到没有错误?是的,在我的情况下,我确实需要访问这些行中的一行页面,其中一些行给出了错误(因为它们各自的页面不存在或其他)。我只想找到一个不会给我错误的行。