0
0
Cypresstesting~5 mins

Command timeout vs assertion timeout in Cypress

Choose your learning style9 modes available
Introduction

We use timeouts to tell Cypress how long to wait for something before giving up. Command timeout waits for actions like clicking or typing. Assertion timeout waits for checks like "is this text visible?".

When clicking a button that might take time to appear on the page.
When checking if a message shows up after submitting a form.
When waiting for a page element to load before interacting with it.
When verifying that a value changes within a certain time.
When debugging slow responses or animations in your app.
Syntax
Cypress
cy.get(selector, { timeout: milliseconds })
cy.contains(text, { timeout: milliseconds })
cy.get(selector).should('have.text', expectedText, { timeout: milliseconds })

Timeout values are in milliseconds (1000 ms = 1 second).

Command timeout applies to commands like get, click, type.

Examples
Waits up to 10 seconds to find the button before clicking.
Cypress
cy.get('#submit-button', { timeout: 10000 }).click()
Waits up to 5 seconds for the message to become visible.
Cypress
cy.get('.message').should('be.visible', { timeout: 5000 })
Waits up to 8 seconds to find the text 'Loading...'.
Cypress
cy.contains('Loading...', { timeout: 8000 })
Sample Program

This test tries to click a button that does not exist, waiting 7 seconds before failing. Then it checks if a message contains 'Success', waiting 5 seconds for it to appear.

Cypress
describe('Timeouts example', () => {
  it('waits for button and checks message', () => {
    cy.visit('https://example.cypress.io')
    // Command timeout: wait up to 7 seconds for button
    cy.get('#non-existent-button', { timeout: 7000 }).click()
    // Assertion timeout: wait up to 5 seconds for message
    cy.get('#message').should('contain.text', 'Success', { timeout: 5000 })
  })
})
OutputSuccess
Important Notes

Command timeout controls how long Cypress waits for commands to complete.

Assertion timeout controls how long Cypress retries assertions before failing.

You can set default timeouts globally in cypress.config.js or override per command.

Summary

Command timeout waits for actions like finding or clicking elements.

Assertion timeout waits for checks like visibility or text content.

Use timeouts to handle slow loading or dynamic content in tests.