0
0
Cypresstesting~10 mins

cy.check() and cy.uncheck() in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test verifies that a checkbox can be checked and unchecked using Cypress commands cy.check() and cy.uncheck(). It confirms the checkbox state changes correctly.

Test Code - Cypress
Cypress
describe('Checkbox check and uncheck test', () => {
  it('should check and uncheck the checkbox', () => {
    cy.visit('https://example.com/form')
    cy.get('#subscribe-newsletter').check().should('be.checked')
    cy.get('#subscribe-newsletter').uncheck().should('not.be.checked')
  })
})
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, no browser opened yet-PASS
2Browser opens and navigates to 'https://example.com/form'Browser shows the form page with a checkbox having id 'subscribe-newsletter'-PASS
3Find checkbox element with id 'subscribe-newsletter' using cy.get('#subscribe-newsletter')Checkbox element is located on the page-PASS
4Check the checkbox using cy.check()Checkbox is now checked (ticked)Verify checkbox is checked with .should('be.checked')PASS
5Find the same checkbox element againCheckbox element is located and currently checked-PASS
6Uncheck the checkbox using cy.uncheck()Checkbox is now unchecked (unticked)Verify checkbox is not checked with .should('not.be.checked')PASS
Failure Scenario
Failing Condition: Checkbox element with id 'subscribe-newsletter' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does cy.check() do in this test?
AIt ticks the checkbox to mark it as checked
BIt unticks the checkbox to mark it as unchecked
CIt clicks a button on the page
DIt reloads the page
Key Result
Always verify that the element selector matches an existing element on the page before using commands like cy.check() or cy.uncheck() to avoid test failures.