Retry-ability helps tests wait for things to be ready before checking them. It makes tests less flaky and more reliable.
Retry-ability of commands in Cypress
cy.get(selector).should('condition')
Cypress automatically retries commands like get and assertions like should until they pass or timeout.
This means you don't need to add manual waits for most cases.
cy.get('#submit-button').should('be.visible')
cy.get('.item').should('have.length', 3)
cy.get('input').should('have.value', 'Hello')
This test visits a page, waits for a button with id 'delayed-button' to appear and be visible, clicks it, then checks the result text.
Cypress retries the get and should commands automatically until they succeed or timeout.
describe('Retry-ability example', () => { it('waits for button to appear and be clickable', () => { cy.visit('https://example.cypress.io') cy.get('#delayed-button').should('be.visible').click() cy.get('#result').should('contain', 'Success') }) })
Retry-ability works only with Cypress commands and assertions, not with plain JavaScript.
Timeouts can be customized if needed using options like { timeout: 10000 }.
Retry-ability reduces the need for cy.wait(), which is less reliable.
Retry-ability makes tests wait for elements or conditions automatically.
It helps avoid flaky tests caused by timing issues.
Use should assertions with get commands to benefit from retry-ability.
Practice
Solution
Step 1: Understand retry-ability concept
Retry-ability means Cypress waits and retries commands until the element or condition is met or timeout occurs.Step 2: Identify what retry-ability helps with
This automatic waiting avoids flaky tests caused by timing issues, ensuring tests proceed only when ready.Final Answer:
Automatically waiting for elements or conditions before proceeding -> Option AQuick Check:
Retry-ability = Automatic wait [OK]
- Confusing retry-ability with parallel test execution
- Thinking retry-ability skips tests
- Assuming retry-ability generates test data
Solution
Step 1: Identify retry-ability usage
Retry-ability works with commands likegetcombined withshouldassertions that retry until condition is true.Step 2: Check each option
cy.get('.btn').should('be.visible').click() usesgetwithshould('be.visible')which retries until the button is visible before clicking.Final Answer:
cy.get('.btn').should('be.visible').click() -> Option DQuick Check:
Use get + should for retry-ability [OK]
- Using cy.click() directly without get()
- Relying on fixed wait times instead of should()
- Missing should() causes no retry
cy.get('#status').should('contain', 'Success')
cy.get('#submit').click()What happens if the element with id
status initially does not contain 'Success' but updates after 2 seconds?Solution
Step 1: Understand retry-ability with should()
Theshould('contain', 'Success')assertion retries until the text appears or timeout.Step 2: Analyze behavior when text updates after 2 seconds
Cypress waits and retries the assertion, so it passes once 'Success' appears, then proceeds to click.Final Answer:
Cypress retries the assertion until 'Success' appears or timeout -> Option CQuick Check:
should() retries until condition met [OK]
- Assuming test fails immediately without retry
- Thinking Cypress waits fixed time automatically
- Believing test skips assertion
cy.get('.loading').should('not.exist')
cy.get('.content').click()But the test fails because
.content is not clickable yet. What is the likely cause?Solution
Step 1: Understand retry-ability of should('not.exist')
Theshould('not.exist')retries until the loading element disappears, so this part works correctly.Step 2: Analyze get('.content').click() behavior
Thegetcommand retries until the element exists but does not wait for it to be clickable or visible before clicking.Final Answer:
The get('.content') command does not wait for element to be clickable -> Option AQuick Check:
get() waits for existence, not clickability [OK]
- Assuming should('not.exist') fails to retry
- Adding unnecessary fixed waits
- Blaming wrong selector without checking
Solution
Step 1: Wait for spinner to disappear
Usingshould('not.exist')on spinner retries until spinner is gone, ensuring loading finished.Step 2: Wait for button to be visible before clicking
Usingshould('be.visible')retries until button is visible and ready for click, avoiding flaky clicks.Final Answer:
cy.get('.spinner').should('not.exist') cy.get('button#submit').should('be.visible').click() -> Option BQuick Check:
Chain should() assertions for reliable waits [OK]
- Clicking button immediately after spinner disappears without visibility check
- Using fixed wait instead of retry-ability
- Skipping spinner wait and causing flaky tests
Ready to prove it?
Pass the test and this pattern moves unknown
