量角器在转发器阵列中逐个模型获取

时间:2015-01-23 15:51:15

标签: angularjs testing protractor angularjs-e2e end-to-end

例如,在HTML页面中:

<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>

然后,我想要类似的东西:

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

这将返回一个无法找到的元素。我基于这个问题并回答: Protractor find element inside a repeater

1 个答案:

答案 0 :(得分:3)

传递到by.model()的值应该与页面上的值相同:

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

请注意,activePost变量将引用一个元素,因此即使它的值为false(未选中复选框),也会满足expect(activePost).toEqual(true);期望值。您需要assert the checkbox's value instead using isSelected()

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