Select element from ng-repeat using protractor

You can select any element from ng-repeat using Protractor.

Consider you have ng-repeat like this
<div ng-repeat="item in items">
    <span>{{item.name}}</span>
</div>

If you have 5 items then your div will be repeated 5 times.

So, if you want to select an element from ng-repeat use below code
var elm = element.all(by.repeater('item in items')).get(0);

The above code returns the first element in ng-repeat.

We use element.all because we are getting more than one element when selecting through ng-repeat. So to select all the elements we have to use element.all.


Click on first element in ng-repeat using Protractor


To click on the first element in ng-repeat use below code
element.all(by.repeater('item in items')).get(0).click();

similarly you can click on any element in ng-repeat just by changing the index in get()

Example :
element.all(by.repeater('item in items')).get(n).click();


Get Text from first element in repeater using protractor


To read the text from the first element in ng-repeat use below code
element.all(by.repeater('item in items')).get(0).getText();

In our example we are showing item name in a span. So, to get the text we have to use the following code
var elm = element.all(by.repeater('item in items')).get(0);
elm.findElement(by.css('span')).getText();

Similarly you can get text of any element in ng-repeat by changing the index in get()

Example
element.all(by.repeater('item in items')).get(n).getText();

In this way you can select element from ng-repeat using protractor

No comments:

Post a Comment