在for循环中返回promise

时间:2016-12-08 12:17:39

标签: angularjs protractor angular-promise angularjs-e2e

我的逻辑如下,

getSpecificCell: function(tableObject, rowText, columnCss) {
    var ele = element.all(by.repeater(tableObject)).count().then(function(count) {
      for (var i = 0; i < count; i++) {
         return element(by.repeater(tableObject).row(i)).getText().then(function(txt) {
        if (txt.indexOf(rowText) !== -1) {
          return element(by.repeater(tableObject).row(i)).element(by.css('[' + columnCss + ']'));
        }
      });
    }
  });
  return ele;
}

但它在第一次迭代中返回值。 是否可以在这种for循环中返回promise或者我们还有其他解决方案吗?

1 个答案:

答案 0 :(得分:2)

首先,您不需要使用ElementArrayFinder使用for循环。这就是each()方法的用途。

其次,你根本不需要循环。听起来您应该使用filter()来获取符合您规范的表格单元格,但我不确定您到底想要完成的是什么。

var table = element.all(by.repeater(tableObject));
// list is an ElementArrayFinder of all elements that matched the filter
var list = table.filter(function (elem) {
    return elem.getText().then(function (text) {
        return txt.indexOf(rowText) !== -1
    })
});

// do something with list
list.count().then(function (count) {
    console.log(count);
});
相关问题