0
0
Cypresstesting~5 mins

Automatic retry mechanism in Cypress

Choose your learning style9 modes available
Introduction

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.

When a web page element takes time to appear after an action.
When waiting for a server response that might be slow.
When testing animations or transitions that need time to complete.
When a test depends on data loading asynchronously.
When network speed or server load can cause temporary delays.
Syntax
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.

Examples
This waits and retries until the submit button is enabled before continuing.
Cypress
cy.get('#submit-button').should('be.enabled')
This retries until the loading text disappears from the page.
Cypress
cy.contains('Loading...').should('not.exist')
This retries until the alert shows the text 'Success'.
Cypress
cy.get('.alert').should('have.text', 'Success')
Sample Program

This test visits a page, waits automatically for a delayed button to appear and be enabled, clicks it, and then checks the result text.

Cypress
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')
  })
})
OutputSuccess
Important Notes

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.

Summary

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.