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?

ndequeker
  • 7,932
  • 7
  • 61
  • 93
Subtubes
  • 15,851
  • 22
  • 70
  • 105

6 Answers6

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');
});
Community
  • 1
  • 1
nilsK
  • 4,323
  • 2
  • 26
  • 40
  • Wasn't able to find it in the documentation. Thank you, that did it. – Subtubes Mar 31 '14 at 15:28
  • 1
    Thanks - missing bracket on first line as pointed out by Voles – trees_are_great Jan 23 '15 at 15:25
  • 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')); ? – Sirk May 27 '15 at 12:26
  • 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. – nilsK Jun 02 '15 at 05:42
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');
});
Community
  • 1
  • 1
ndequeker
  • 7,932
  • 7
  • 61
  • 93
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();
Silvio Troia
  • 1,439
  • 19
  • 36
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();
pansay
  • 687
  • 8
  • 11
  • 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! :-) – ebragaparah May 04 '16 at 14:43
2

An even better solution:

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

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');
});
Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40