0
0
Cypresstesting~5 mins

Retry-ability of commands in Cypress

Choose your learning style9 modes available
Introduction

Retry-ability helps tests wait for things to be ready before checking them. It makes tests less flaky and more reliable.

When a button appears after a short delay on a webpage.
When a form field updates after some background process.
When a page element changes after clicking a link.
When waiting for data to load before checking it.
When animations or transitions delay element visibility.
Syntax
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.

Examples
Waits until the submit button is visible before continuing.
Cypress
cy.get('#submit-button').should('be.visible')
Retries until there are exactly 3 elements with class 'item'.
Cypress
cy.get('.item').should('have.length', 3)
Waits until the input field's value becomes 'Hello'.
Cypress
cy.get('input').should('have.value', 'Hello')
Sample Program

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.

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

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.

Summary

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.