Protractor find element inside a repeater
Asked Answered
R

6

22

Below is my markup

<tr ng-repeat="post in posts">
  <td ng-click="activePost(post)" class="title">{{post.title}}</td>
  <td><button class="btn btn-danger" ng-click="delete(post)">Delete</button>
  </td>
</tr>

I'm trying to get the element with the title class.

The code I use to access the repeater is:

postsList = element.all(by.repeater('post in posts'));

Is there a way to get the element by doing something like the following in jQuery:

var titleText = $("tr:first").find(".title").text();

Is there a way of doing something similar to this with protractor?

Ranking answered 31/3, 2014 at 8:53 Comment(0)
B
31

this should work for your example:

element.all(by.repeater('post in posts')).then(function(posts) {
   var titleElement = posts[0].element(by.className('title'));
   expect(titleElement.getText()).toEqual('YourEnteredTitle');
});
Berbera answered 31/3, 2014 at 12:7 Comment(4)
Wasn't able to find it in the documentation. Thank you, that did it.Ranking
Thanks - missing bracket on first line as pointed out by VolesHalfbaked
I'd be interested to know how this works now that element locators don't return a promise and there for using .then no longer works, do you just do element.all(by.repeater('post in posts))[0].element(by.className('title')); ?Skinner
a little late ... sorry, i cant help you with this. i changed the job (back to C#) and i am not fimiliar with current protractor syntax. if you are still interested to know this, please open a new question.Berbera
O
21

The answer from nilsK helped me, but didn't work completely. The code below did the trick:

element.all(by.repeater('post in posts')).then(function(posts) {
   var titleElement = posts[0].element(by.className('title'));
   expect(titleElement.getText()).toEqual('YourEnteredTitle');
});
Overhang answered 30/7, 2014 at 11:38 Comment(0)
C
6

you have to find a element inside a array as explained here https://github.com/angular/protractor/issues/877

var items = element.all(by.repeater('userGroup in userGroups')).filter(function(item) {
     return item.element(by.binding('userGroup.name')).getText().then(function(label) {
           return label === 'MyGroupName';
     });
  });
items.get(0).element(by.css('.buttongochose')).click();
Cheerless answered 17/6, 2015 at 15:0 Comment(1)
I think this is the best answer, because it allow you to filter by text.Cadi
G
4

From the docs: https://github.com/angular/protractor/blob/master/docs/locators.md

var clickable = element.all(by.repeater('post in posts')).first().all(by.tagName('td')).first();
Gossett answered 26/8, 2015 at 11:30 Comment(1)
It's not exactly what I was looking for, I mean, it's not the exact chunk of code, but gave me an awesome idea! Thanks! :-)Doublepark
E
2

An even better solution:

expect( $$(by.repeater('post in posts')).get(0).$('.title').getText() ).toBe('Your title');

Empoison answered 13/5, 2015 at 16:40 Comment(0)
R
0

More readable way doing the same these days would be

it('test case', async () => {
  let repeaters = element.all(by.repeater('post in posts'));
  let titleElement = repeaters.get(0).element(by.className('title'));
  expect(await titleElement.getText()).toEqual('YourEnteredTitle');
});
Refugiorefulgence answered 9/2, 2021 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.