1

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)
CookieMonster
  • 241
  • 1
  • 9
  • 26

2 Answers2

2

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
Fody
  • 23,754
  • 3
  • 20
  • 37
1

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)
});
agoff
  • 5,818
  • 1
  • 7
  • 20
  • I still getting error `Timed out retrying after 4000ms: expected '' to have attribute 'checked'` it seems it is trying to find checked, but in my case I dont want any attributes to have checked. It seems the false doesn’t work as expected – CookieMonster Sep 08 '22 at 14:16
  • Ah, so the issue is probably related to how you're checking that it is checked. Can you try `.should('not.have.attr', 'checked')`? – agoff Sep 08 '22 at 14:32