Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protractor return array of values from repeater

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?

like image 703
user2936314 Avatar asked Sep 11 '14 19:09

user2936314


2 Answers

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
});
like image 169
sma Avatar answered Oct 02 '22 07:10

sma


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:

  • protractor - how to get the result of an array of promises into another array
like image 37
alecxe Avatar answered Sep 29 '22 07:09

alecxe