【问题标题】:How to check multiple checkboxes in Protractor如何在量角器中检查多个复选框
【发布时间】:2016-04-21 05:12:58
【问题描述】:

我已经看到如何通过此链接选中一个复选框: How to validate when a checkbox is checked in AngularJS e2e tests?

但是我想知道如何执行此操作以检查我在视图中的所有复选框是否都将被选中。

目前下面的代码只检查一个复选框是否被选中:

    .clickElementById('user-' + data.userIndex)
    .expect(
        element('input[ng-model="user.checked"]')
        .attr('checked')
    ).toBeTruthy()

这是我想要的,但是当我进行下一次通过按钮选择所有复选框的测试时,我想遍历所有复选框以测试它们是否已被选中。抱歉重复我自己。

【问题讨论】:

    标签: javascript angularjs checkbox protractor


    【解决方案1】:

    使用 element.all 创建指定元素的数组,然后您可以循环或直接调用它们,即

    element.all(by.model('user.checked').then(function(btn) {
        expect(btn[0].attr('checked')).toBe('true');
    });
    

    或者用循环

    element.all(by.model('user.checked').each(function(btn) {
         // iterates through each btn
         expect(btn.attr('checked')).toBe('true');
    });
    

    来源:https://angular.github.io/protractor/#/api?view=ElementArrayFinder

    【讨论】:

    • 要添加到这一点,您实际上可以使用量角器附带的每个方法。只需替换 then 承诺即可。
    • 啊,真的!我忘记了.each() - 比 for 循环好多了。我更新了我的答案。
    • 如果你使用by.css,那么你可以使用isSelected。只是另一种方式。
    【解决方案2】:

    您也可以 reduce 将其单独选中 - 所有复选框都选中或不选中:

    var allChecked = element.all(by.model('user.checked')).reduce(function(acc, btn) {
        return btn.getAttribute('checked')).then(function(checked) {
            return acc && (checked == 'true');
        });
    }, true);
    expect(allChecked).toBe(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多