Test Retries is one of the most interesting features in cypress. Once test retries are enabled, tests can be configured to have X number of retry attempts. For example, if test retries have been configured with 2 retry attempts, Cypress will retry tests up to 2 additional times (for a total of 3 attempts) before potentially being marked as a failed test. This feature will help a lot to curb the problem of flaky tests. So let’s further deep-dive by automating the below scenario:

1. Go to Wikipedia Page.
2. Assert the title. Here we will intentionally give a wrong title to force cypress to retry the test once.

Step 1: Write “retries”: 1 in your cypress.json file. The number denotes the number of retries.

cypress test retries config
 

Step 2:

1
2
3
4
5
6
7
8
9
10
describe('Test to demonstrate Test Retries in Cypress', () => {
    before(() => {
        cy.visit('https://wikipedia.org')
    })

    it('Validate Page Title', () => {
        //Intentionally making it fail to check retries
        cy.title().should('eq', 'Wikipedia1111')
    })
})

cypress test retries test scripts
 

Step 3: After execution you can see that cypress retried the test once (total attempt 2) before finally marking the test as failed.

cypress test retries attempts

This feature will only work with Cypress v5.0 and above.

Do check out 🙂

Github: https://github.com/alapanme/Cypress-Automation
All Cypress Articles: https://testersdock.com/cypress-tutorial/