cy.check() and cy.uncheck() in Cypress - Build an Automation Script
describe('Checkbox check and uncheck test', () => { beforeEach(() => { cy.visit('https://example.com/checkbox-demo'); }); it('should check and uncheck the newsletter subscription checkbox', () => { cy.get('#subscribe-newsletter') .check() .should('be.checked'); cy.get('#subscribe-newsletter') .uncheck() .should('not.be.checked'); }); });
The test starts by visiting the checkbox demo page in the beforeEach hook to ensure a fresh state before each test.
We locate the checkbox using cy.get('#subscribe-newsletter'), which uses the id selector for clarity and speed.
We then use .check() to check the checkbox and immediately assert it is checked with .should('be.checked').
Next, we use .uncheck() to uncheck the checkbox and assert it is unchecked with .should('not.be.checked').
This approach uses Cypress's built-in waiting and assertion mechanisms, avoiding any manual waits or flaky code.
Now add data-driven testing to check and uncheck three different checkboxes with ids 'subscribe-newsletter', 'accept-terms', and 'receive-updates'.
Practice
cy.check() do in Cypress testing?Solution
Step 1: Understand the purpose of
cy.check()cy.check()is used to select checkboxes or radio buttons in Cypress tests.Step 2: Compare with other commands
cy.uncheck()is used to deselect checkboxes only, so it does not match the question.Final Answer:
Selects checkboxes or radio buttons -> Option CQuick Check:
cy.check() selects inputs [OK]
- Confusing cy.check() with cy.uncheck()
- Thinking cy.check() types text
- Assuming cy.check() clicks buttons
agree in Cypress?Solution
Step 1: Identify the command to deselect a checkbox
To deselect a checkbox, usecy.uncheck().Step 2: Match the syntax with the element selector
cy.get('#agree').uncheck()correctly targets the checkbox with id 'agree' and unchecks it.Final Answer:
cy.get('#agree').uncheck() -> Option AQuick Check:
uncheck() deselects checkbox [OK]
- Using check() to uncheck a box
- Using click() which may toggle unpredictably
- Trying to type 'uncheck' into checkbox
cy.get('input[type="checkbox"]').check().should('be.checked');
cy.get('input[type="checkbox"]').uncheck().should('not.be.checked');Solution
Step 1: Analyze the first command and assertion
cy.get('input[type="checkbox"]').check()selects all checkboxes, thenshould('be.checked')asserts they are checked. This should pass.Step 2: Analyze the second command and assertion
cy.get('input[type="checkbox"]').uncheck()deselects all checkboxes, thenshould('not.be.checked')asserts they are unchecked. This should also pass.Final Answer:
Both assertions pass -> Option BQuick Check:
check() then be.checked, uncheck() then not.be.checked [OK]
- Assuming uncheck() works on radio buttons
- Thinking check() does not affect checked state
- Confusing assertion syntax
cy.get('input[type="radio"]').uncheck();Why does this test fail?
Solution
Step 1: Understand
uncheck()limitationsuncheck()works only on checkboxes, not radio buttons.Step 2: Analyze the selector and usage
The selector targets radio buttons, so usinguncheck()causes failure.Final Answer:
Because uncheck() cannot be used on radio buttons -> Option DQuick Check:
uncheck() only for checkboxes [OK]
- Trying to uncheck radio buttons
- Assuming uncheck() needs parameters
- Confusing selector correctness
features. You want to check only the boxes with values fast and secure, then verify only those are checked. Which Cypress code snippet correctly does this?Solution
Step 1: Use check() with array to select multiple checkboxes
cy.get('input[name="features"]').check(['fast', 'secure'])checks the boxes with those values.Step 2: Assert that those checkboxes are checked
should('be.checked')confirms the selected checkboxes are checked.Final Answer:
cy.get('input[name="features"]').check(['fast', 'secure']).should('be.checked') -> Option AQuick Check:
check(array) selects multiple, then assert checked [OK]
- Using multiple check() calls without chaining properly
- Using should('have.value') which is incorrect for checkboxes
- Unchecking after checking without reason
Ready to prove it?
Pass the test and this pattern moves unknown
