量angular器:filter直到find第一个有效的元素

我正在一个包含一个我需要迭代的表格的e2etesting上,“直到find一个不会失败的时候,我点击它。

我尝试使用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 (或不同的方法),并停止迭代,当第一个有效的事件出现?或有人提出一个更好的方法?

你是对的, filter()和其他内置的量angular器“函数式编程”方法并不能解决“停止迭代时出现第一个有效发生”的情况。 您需要“在某些条件评估为真时采取某些元素”(如Python世界中的itertools.takewhile() )。

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

  • 在条件评估为true时使用元素(扩展ElementArrayFinder)

请注意,我已经提出它是内置的,但function请求仍然是打开的:

  • 将takewhile()方法添加到ElementArrayFinder

如果你喜欢处理这种情况的非量angular器的方法,我会build议async.whilst 。 asynchronous是一个非常stream行的模块,它很可能是你的应用程序正在使用它。 我在编辑器里写了下面的代码,但是它应该可以工作,你可以根据你的需要来定制它。 希望你能了解我在这里做什么。

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