check if all checkbox are unchecked on cypress

Viewed 33

I have dynamic checkboxes (anything from 1-20 checkboxes). I want to check if all checkboxes are unchecked.

I have tried serval ways without any success.

//is true even if it is checked
cy.get('[type="checkbox"]').not("checked");

cy.get('[type="checkbox"]').should('have.attr', 'checked', false)
2 Answers

The checked attribute does not have a true/false value. It's either present or absent.

This should work:

cy.get('[type="checkbox"]').should('not.be.checked')        // checks multiple

I believe the issue is coming from the fact that your .get() should yield multiple checkboxes, but you're then using a singular assert on it. Switching to using .each() to iterate through each yielded checkbox should work better.

cy.get('[type="checkbox"]').each(($el) => {
  cy.wrap($el).should('have.attr', 'checked', false)
});
Related