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?

2 Answers 2

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
});
Sign up to request clarification or add additional context in comments.

Comments

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.