Automatic retry helps tests try again if something is not ready yet. This makes tests more stable and less likely to fail because of small delays.
Automatic retry mechanism in Cypress
cy.get('selector').should('be.visible')
Cypress automatically retries commands like cy.get() and assertions like should() until they pass or timeout.
You do not need to write loops for retry; Cypress handles it for you.
cy.get('#submit-button').should('be.enabled')
cy.contains('Loading...').should('not.exist')
cy.get('.alert').should('have.text', 'Success')
This test visits a page, waits automatically for a delayed button to appear and be enabled, clicks it, and then checks the result text.
describe('Automatic retry example', () => { it('waits for button to be clickable', () => { cy.visit('https://example.cypress.io') cy.get('#delayed-button').should('be.visible').and('be.enabled') cy.get('#delayed-button').click() cy.get('#result').should('contain', 'Clicked') }) })
Cypress retries commands and assertions up to the default timeout (usually 4 seconds).
You can change the timeout with options like { timeout: 10000 } to wait longer if needed.
Automatic retry reduces flaky tests caused by slow loading or animations.
Automatic retry makes tests wait and try again until conditions are met.
Cypress handles retry for commands and assertions without extra code.
This helps tests be more reliable and less flaky.
Practice
Solution
Step 1: Understand Cypress retry behavior
Cypress automatically retries commands and assertions until they pass or the timeout is reached.Step 2: Compare options with this behavior
Only the option stating "It keeps trying commands and assertions until they pass or timeout." correctly describes this automatic retry mechanism without manual intervention.Final Answer:
It keeps trying commands and assertions until they pass or timeout. -> Option CQuick Check:
Automatic retry = keeps trying until pass [OK]
- Thinking retries happen only on user command
- Believing Cypress skips failed tests without retry
- Assuming manual loops are needed for retries
submit-btn is visible, leveraging Cypress's automatic retry?Solution
Step 1: Identify correct Cypress command and assertion
The correct command to select an element iscy.get()and the assertion to check visibility isshould('be.visible').Step 2: Validate syntax correctness
cy.get('#submit-btn').should('be.visible') uses the correct syntax and leverages automatic retry on the assertion. Other options use invalid commands or incorrect assertion syntax.Final Answer:
cy.get('#submit-btn').should('be.visible') -> Option BQuick Check:
Correct syntax = cy.get + should('be.visible') [OK]
- Using non-existent commands like assertVisible()
- Incorrect assertion strings like 'visible' instead of 'be.visible'
- Using cy.find() instead of cy.get()
cy.get('.loading').should('not.exist')
cy.get('#data').should('contain', 'Success')What will happen if the element with class
loading disappears after 3 seconds but the default timeout is 4 seconds?Solution
Step 1: Understand retry on should('not.exist')
Cypress automatically retries assertions likeshould('not.exist')until the element disappears or timeout.Step 2: Analyze timing and retry behavior
The element disappears after 3 seconds, which is before the 4-second timeout, so the assertion will eventually pass.Final Answer:
The test will pass because Cypress retries until the element disappears within timeout. -> Option DQuick Check:
Retry waits for condition until timeout [OK]
- Assuming immediate failure if element exists initially
- Believing manual waits are always needed
- Thinking Cypress does not retry negative assertions
cy.get('#submit').click()
cy.get('#result').should('contain', 'Done')But the test fails because
#result text updates slowly. Which fix uses Cypress's automatic retry correctly?Solution
Step 1: Identify problem with slow update
The element#resultupdates slowly, so default timeout may be too short.Step 2: Use automatic retry with increased timeout
Usecy.get('#result', { timeout: 7000 }).should('contain', 'Done')to increase the timeout, letting Cypress retry the assertion longer until it passes.Final Answer:
Use cy.get('#result', { timeout: 7000 }).should('contain', 'Done') -> Option AQuick Check:
Increase timeout to let retry succeed [OK]
- Using fixed waits instead of timeout options
- Not changing anything and expecting success
- Replacing should with then, which disables retry
submit-btn. Which Cypress code best uses automatic retry to wait for the button to be enabled before clicking it?Solution
Step 1: Understand button enabling timing
The button becomes enabled after an API call, so it may be disabled initially.Step 2: Use automatic retry on enabled state
cy.get('#submit-btn').should('be.enabled').click() usesshould('be.enabled')which retries until the button is enabled, then clicks it.Final Answer:
cy.get('#submit-btn').should('be.enabled').click() -> Option AQuick Check:
Use should('be.enabled') to wait before click [OK]
- Clicking immediately without waiting
- Using fixed wait instead of retry
- Using then() which disables retry and may fail
Ready to prove it?
Pass the test and this pattern moves unknown
