1

I've seen how to check one checkbox via this link: How to validate when a checkbox is checked in AngularJS e2e tests?

However I would like to know how to do this to check if all checkboxes that I have on my view are going to be checked.

At the moment the code below only checks if one checkbox has been selected:

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

Which is what I want however when I go onto my next test of selecting all checkboxes via a button I would like to loop through all of the checkboxes to test if they have been selected. Apologies to repeat myself.

Ta

Community
  • 1
  • 1
MaxwellLynn
  • 880
  • 5
  • 23
  • 46

2 Answers2

1

Use element.all which creates an array of the specified elements and then you can either loop or call them directly i.e.

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

Or with the loop

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

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

Gunderson
  • 3,263
  • 2
  • 16
  • 34
1

You can also reduce it to a single check - all checkboxes are checked or not:

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);
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195