Automatic retry mechanism in Cypress - Build an Automation Script
describe('Login Test with Automatic Retry', () => { beforeEach(() => { cy.visit('/login'); }); it('should login successfully and show dashboard with welcome message', () => { cy.get('#username').type('testuser'); cy.get('#password').type('Test@1234'); cy.get('#loginBtn').click(); // Cypress automatically retries assertions until they pass or timeout cy.url().should('include', '/dashboard'); cy.get('#welcomeMsg').should('contain.text', 'Welcome, testuser'); }); });
This test uses Cypress to automate the login process. The beforeEach hook ensures the login page is loaded before each test.
The test types the username and password into their respective fields and clicks the login button.
After clicking, Cypress automatically retries the cy.url().should('include', '/dashboard') assertion until the URL contains '/dashboard' or the default timeout is reached.
Similarly, the welcome message assertion retries until the expected text appears.
This automatic retry mechanism helps handle delays in page loading or rendering without needing explicit waits.
Now add data-driven testing with 3 different sets of valid credentials to verify login success for each user.
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
