What if your tests could patiently wait and never fail just because the app is a little slow?
Why Retry-ability of commands in Cypress? - Purpose & Use Cases
Imagine you are testing a website manually. You click a button and wait for a message to appear. Sometimes it takes a moment, so you keep checking again and again, hoping it shows up. This waiting and checking is tiring and easy to miss.
Manually waiting and checking is slow and boring. You might check too soon and think the feature is broken, or miss the message if you look away. This causes mistakes and wastes time, making testing frustrating and unreliable.
Retry-ability means the test automatically tries again until the expected result appears or a timeout happens. This way, tests wait patiently and only fail if the feature truly does not work, making tests more reliable and faster to write.
cy.get('#submit').click().then(() => { expect(Cypress.$('#message').length).to.be.gt(0); }); // fails if message not immediately present
cy.get('#submit').click(); cy.get('#message').should('be.visible'); // retries until visible or timeout
It enables tests to handle real-world delays smoothly, making automated testing trustworthy and less flaky.
When submitting a form, the confirmation message might take a second to appear. Retry-ability lets the test wait and catch that message without failing too early.
Manual checking is slow and error-prone.
Retry-ability automates waiting and rechecking.
This makes tests more reliable and easier to maintain.
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
