0
0
Cypresstesting~5 mins

Multiple assertions chaining in Cypress

Choose your learning style9 modes available
Introduction

Multiple assertions chaining helps check many things about a web element in one go. It makes tests shorter and easier to read.

When you want to check that a button is visible, enabled, and has the right text.
When you need to verify that a form input has the correct value and is not disabled.
When testing that a link has the correct URL and is visible on the page.
When you want to confirm multiple styles or attributes of an element at once.
Syntax
Cypress
cy.get('selector')
  .should('assertion1')
  .and('assertion2')
  .and('assertion3')

Use .should() for the first assertion and .and() for chaining more assertions.

Each assertion checks a different property or state of the same element.

Examples
Check that the submit button is visible, enabled, and has the text 'Submit'.
Cypress
cy.get('button#submit')
  .should('be.visible')
  .and('be.enabled')
  .and('contain.text', 'Submit')
Verify the email input has the correct value and is not disabled.
Cypress
cy.get('input[name="email"]')
  .should('have.value', 'user@example.com')
  .and('not.be.disabled')
Confirm the home link points to '/home' and is visible.
Cypress
cy.get('a#home-link')
  .should('have.attr', 'href', '/home')
  .and('be.visible')
Sample Program

This test visits a page and checks the submit button is visible, enabled, and has the text 'Submit' using chained assertions.

Cypress
describe('Multiple assertions chaining example', () => {
  it('checks button properties', () => {
    cy.visit('https://example.cypress.io')
    cy.get('button[data-test-id="submit-button"]')
      .should('be.visible')
      .and('be.enabled')
      .and('contain.text', 'Submit')
  })
})
OutputSuccess
Important Notes

Chaining assertions keeps tests clean and easy to understand.

If one assertion fails, Cypress stops and shows the error immediately.

Use descriptive selectors to avoid flaky tests.

Summary

Multiple assertions chaining lets you check many things about one element in one statement.

Start with .should() and add more checks with .and().

This makes tests shorter, clearer, and easier to maintain.