2

I have a checkbox which I want to check if it is not checked and want to uncheck it if it is checked in Cypress. How can I do that?

1
  • Welcome to the community. What have you tried so far? Can you add your test code of you've tried so far?
    – Lee Jensen
    Commented Nov 22, 2021 at 16:13

1 Answer 1

4

Conditional testing is not recommended by Cypress, but it's possible to deal with this situation like this:

html:

<div>
  <input type="checkbox" id="mytestcheckbox" />
  <label for="mytestcheckbox">Toggle me!</label>
</div>

and in Cypress:

cy
  .get('#mytestcheckbox')
  .as('checkbox')
  .invoke('is', ':checked')
  .then(checked => {
    if (checked) {
      cy
        .get('@checkbox')
        .uncheck();
    } else {
      cy
        .get('@checkbox')
        .check();
    }
  });
1
  • Thanks a lot , that did resolved my issue
    – Rao Thota
    Commented Nov 22, 2021 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.