04 / 10

How do you perform assertions in Cypress?

In Cypress, assertions are how you check that something is true during your test—like checking if a button exists, a value is correct, or a page has loaded properly. Cypress uses Chai, Sinon, and jQuery assertions under the hood, and it integrates them with commands like should() and expect().​

Using .should() (Most Common), This is chained directly onto Cypress commands.
Using expect(): This is used when working with variables or custom logic.
Common Assertions Examples:
  1. 1

    Text exists : cy.contains('Login Successful')

  2. 2

    Element is visible: cy.get('.alert').should('be.visible')

  3. 3

    Class or attribute check: cy.get('button').should('have.class', 'primary')

  4. 4

    URL or title check: cy.url().should('include', '/home')

  5. 5

    Value in input field: cy.get('#name').should('have.value', 'John')

  6. 6

    Checkbox is checked: cy.get('#subscribe').should('be.checked')

Common Assertions Examples:
  1. 1

    Assertions automatically wait for the condition to be true (up to default timeout).

  2. 2

    You can chain multiple assertions