6

I am looking for an easy way to return an array of values from protractor's all.(by.repeater)

Basically, I just want an easy way to make an array of usernames given a repeater like user in users.

Right now I'm building it like this:

allUsers = element.all(by.repeater('user in users').column('user.username')).then(function(array){
  var results = []
  var elemLength = array.length
  for(var n = 0; n < elemLength; n++){
    array[n].getText().then(function(username){
      results.push(username)
    })
  }
  return results
});
expect(allUsers).toContain(newUser)

Is there a more concise, reusable way to do this built into protractor/jasmine that I just can't find?

Hooked
  • 84,485
  • 43
  • 192
  • 261
user2936314
  • 1,734
  • 4
  • 18
  • 32

2 Answers2

11

AS alecxe said, use map to do this. This will return a deferred that will resolve with the values in an array, so if you have this:

var mappedVals = element.all(by.repeater('user in users').column('user.username')).map(function (elm) {
    return elm.getText();
});

It will resolve like this:

mappedVals.then(function (textArr) {
    // textArr will be an actual JS array of the text from each node in your repeater
});
sma
  • 9,449
  • 8
  • 51
  • 80
3

I've successfully used map() for this before:

element.all(by.repeater('user in users').column('user.username')).map(function (elm) {
    return elm.getText();
});

When I researched the topic I took the solution from:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195