Search code examples
angularjstestingprotractorangularjs-e2eend-to-end

Protractor get element by model in repeater array


For example, in HTML page:

<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>
  <td><input type="checkbox" ng-model="post.active" 
        id="{{post.id}}" /></td>
 </tr>

Then, I want something like:

element.all(by.repeater('post in posts')).then(function(posts) {
   var activePost = posts[0].element(by.model('active'));
   expect(activePost).toEqual(true);
});

This returns an unable to find element. I am basing this on this question and answer: Protractor find element inside a repeater


Solution

  • The value passed into by.model() should be as is on the page:

    var activePost = posts[0].element(by.model('post.active'));
    

    Note that activePost variable would refer to an element, hence even if it's value would be false (unchecked checkbox), the expect(activePost).toEqual(true); expectation would be met. You need to assert the checkbox's value instead using isSelected():

    expect(activePost.isSelected()).toBeTruthy();