Retry-ability
Learn how Cypress retries commands and assertions to avoid flakes. In addition, learn how we can use retry-ability to our advantage.
One of the core features of Cypress is its ability to retry commands several times to reduce the likelihood of flakes. As discussed previously in the “Consistent Results” lesson, Cypress takes several measures to battle the most common causes of flakes, such as:
- Half-loaded application state.
- Elements that are not available, covered, or disabled.
- Flakes caused by animations.
Retrying commands and assertions
To battle all of this, Cypress retries both commands and assertions alike. The commands are Cypress calls that execute either a user behavior or get an element:
// The following are all commandscy.get('.cta');cy.get('input').type('This is a command');cy.get('.cta').click();
On the other hand, assertions are anything that tests for a certain condition to be met, which is usually denoted by a cy.should
command:
// The following are all assertionscy.get('@cookiePolicyBanner').should('not.be.visible');cy.get('@testimonials').should('have.attr', 'data-index', '0');cy.get('@carousels').should('have.length', 2);
The reason for retries
When testing modern web applications, we will likely test asynchronous code. There are several reasons why a command might fail at first that ...