1

I have a logic like below,

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

But it is returning the value in first iteration itself. Is that possible to return the promise inside this kind of for loop or do we have any other solution for this?

Prabhu
  • 394
  • 1
  • 7
  • 23
  • You have multiple promises in your code. Which one do you want to return? If you want to combine the promises, put them in an array and use the promise.all function. See: http://stackoverflow.com/questions/24852126/is-there-a-way-to-resolve-multiple-promises-with-protractor – raichu Dec 08 '16 at 12:33
  • Need to return the promise which is inside the for loop – Prabhu Dec 08 '16 at 12:37
  • I mean you have multiple promises in your loop. Every iteration generates one or two promises. – raichu Dec 08 '16 at 12:56
  • Sorry, actually I need to return promise when the `if (txt.indexOf(rowText) !== -1) {` is satisfied Promise to be returned: `return element(by.repeater(tableObject).row(i)).element(by.css('[' + columnCss + ']'));` – Prabhu Dec 08 '16 at 12:57
  • Sorry, I didn't understand the intention of your code. I suppose your trying to return a promise only when that promise is going to be resolved. But you cannot do that. That's the nature promises. They hides the asynchrony, but you shouldn't forget about it. To solve your problem, I would check how to create your own promise using `protractor.promise.defer()`. I personally had some troubles understanding promises in the beginning, so if you don't know how to use defer(), ask me. – raichu Dec 08 '16 at 13:09
  • can you share the html of the table element along with row and column, just for example. And moreover you can not use for loop in async programing – radio_head Dec 08 '16 at 13:14

1 Answers1

2

First, you don't need to use for loops with an ElementArrayFinder. That's what the each() method is for.

Second, you shouldn't need to loop at all. It sounds like you should be using filter() to get the table cells that match your specification, though I'm not sure what exactly you're trying to accomplish.

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);
});
Gunderson
  • 3,263
  • 2
  • 16
  • 34