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?".
Command timeout vs assertion timeout in 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.
cy.get('#submit-button', { timeout: 10000 }).click()
cy.get('.message').should('be.visible', { timeout: 5000 })
cy.contains('Loading...', { timeout: 8000 })
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.
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 }) }) })
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.
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.
Practice
command timeout and assertion timeout in Cypress?Solution
Step 1: Understand command timeout purpose
Command timeoutis the time Cypress waits to find or interact with elements before failing.Step 2: Understand assertion timeout purpose
Assertion timeoutis the time Cypress waits for conditions like visibility or text to become true in assertions.Final Answer:
Command timeoutwaits for actions like finding elements;assertion timeoutwaits for checks like visibility. -> Option CQuick Check:
Command timeout vs assertion timeout =Command timeoutwaits for actions like finding elements;assertion timeoutwaits for checks like visibility. [OK]
- Confusing command timeout with page load timeout
- Thinking assertion timeout waits for commands
- Mixing up API calls with UI element waits
Solution
Step 1: Recall Cypress command timeout syntax
Timeout for commands likecy.get()is set by passing an options object withtimeoutin milliseconds.Step 2: Check each option for correct syntax
Onlycy.get('button', { timeout: 10000 })uses the correct syntax. The others use invalid methods.Final Answer:
cy.get('button', { timeout: 10000 }) -> Option AQuick Check:
Command timeout syntax = cy.get('button', { timeout: 10000 }) [OK]
- Using non-existent methods like .timeout()
- Passing timeout as a separate method
- Confusing assertion timeout syntax with command timeout
cy.get('#submit', { timeout: 5000 }).click();
cy.get('#message').should('be.visible', { timeout: 10000 });What happens if the
#submit button appears after 6 seconds and the #message appears after 8 seconds?Solution
Step 1: Analyze command timeout for
The command timeout is 5000 ms (5 seconds). The button appears after 6 seconds, which is too late, so the click command fails.#submitStep 2: Analyze assertion timeout for
The assertion timeout is 10000 ms (10 seconds). The message appears after 8 seconds, which is within timeout, so the assertion passes.#messageFinal Answer:
Test fails at clicking#submitbut passes the visibility check for#message. -> Option AQuick Check:
Command timeout too short, assertion timeout sufficient = Test fails at clicking#submitbut passes the visibility check for#message. [OK]
- Assuming both timeouts behave the same
- Ignoring timeout values in options
- Thinking assertion timeout affects commands
cy.get('.loading', { timeout: 3000 }).should('not.exist');But the test fails because the element disappears after 5 seconds. What is the best fix?
Solution
Step 1: Identify what is being waited on
The test waits for the element to disappear, which is an assertion (should('not.exist')).Step 2: Adjust the correct timeout
The command timeout controls finding the element, but the assertion timeout controls waiting for the condition. Since the element disappears after 5 seconds, increase assertion timeout beyond 5000 ms.Final Answer:
Increase the assertion timeout to more than 5000 ms. -> Option DQuick Check:
Assertion timeout controls condition wait = Increase the assertion timeout to more than 5000 ms. [OK]
- Increasing command timeout instead of assertion timeout
- Using fixed wait instead of timeout adjustment
- Changing assertion logic incorrectly
Solution
Step 1: Set command timeout for button appearance
The button may appear after up to 4 seconds, so set command timeout slightly higher (5000 ms) to allow finding it.Step 2: Set assertion timeout for modal visibility
The modal may take up to 7 seconds to appear, so set assertion timeout higher (8000 ms) to wait for visibility.Final Answer:
Usecy.get('button', { timeout: 5000 }).click()andcy.get('.modal').should('be.visible', { timeout: 8000 }). -> Option BQuick Check:
Command timeout for action, assertion timeout for condition = Usecy.get('button', { timeout: 5000 }).click()andcy.get('.modal').should('be.visible', { timeout: 8000 }). [OK]
- Swapping timeout values between command and assertion
- Setting timeouts too low causing flaky tests
- Not setting timeouts causing premature failures
Ready to prove it?
Pass the test and this pattern moves unknown
