【问题标题】:protractor: filter until finding first valid element量角器:过滤直到找到第一个有效元素
【发布时间】:2017-03-26 15:13:05
【问题描述】:

我正在一个站点上进行 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(或其他方法)并在出现第一个有效事件时停止迭代?或者有人能想出更好的方法吗?

【问题讨论】:

  • 但是你怎么知道是一个有效的行呢?只要点击它就可以看到没有错误?
  • 是的,就我而言,我确实需要访问其中一个行页面,并且某些行会出现错误(因为它们各自的页面不存在或其他原因)。我只是想要一种方法来找到不会给我错误的行。

标签: node.js protractor


【解决方案1】:

如果您更喜欢使用非量角器方法来处理这种情况,我建议您使用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();
});

【讨论】:

    【解决方案2】:

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

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

    请注意,我建议它是内置的,但功能请求仍然开放:

    【讨论】:

      猜你喜欢
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多