Cypress: How to retry failed test cases and log the console error on command line?

Reading Time: 3 minutes

In our previous blog “How to load chrome extension in cypress.io” we explained

  • How to get an unpacked version of an extension from the chrome web store?
  • How to load chrome extension in our application under test?
  • What are the related configuration changes needed to load an extension in cypress?

Here we will discuss:

  • How to add retry ability in our test scripts?
  • How to log console output on the command line to make the debugging easier?

Before moving on let’s discuss why we need to add retry ability? As we know in Front End automation flaky tests are one of the major challenges and adding retry ability can be a possible solution for the same which helps to reduce false failures because of random issues. 

How to Add retry ability?

As of now, cypress does not provide a direct way to retry failed tests but we can add this ability by integrating a plugin in our test scripts. This plugin will execute the failed test again as per the provided retry counter at every level of test suite like test level, hook level, etc.

So here it is:

Install the plugin:

  • Add the plugin to devDependencies
 $ npm install -D cypress-plugin-retries
  • At the top of cypress/support/index.js:
 require('cypress-plugin-retries')
  •  At the top of cypress/plugin/index.js:
module.exports = (on, config) => {
require('cypress-plugin-retries/lib/plugin')(on)
}
// This enable to log the retry counter on the command line.

Usage

  • Use the environment variable CYPRESS_RETRIES to set the retry number for all spec files:

 CYPRESS_RETRIES=2 npm run cypress

  • Or Set the “env” key in your cypress.json configuration file to set the retry number for all spec files:
{
    "env":
     {
        "RETRIES": 2
     }
 }
  • Or On a per-test or per-hook basis, set the retry number:
  it('test', () => {
    Cypress.currentTest.retries(2)
 })

This is all about adding retry ability in our test cases to prevent the flaky test cases.

How to log console output on the command line?

Now the question is, to execute the test cases with CI/CD pipeline and if any failure occurs how to know the root cause of failure? As in CI/CD pipeline test executes in headless mode and there is no UI exist so how to know the console error? Same as retry ability cypress does not provide any configuration to print the console error on the command line then another plugin comes in picture.

Installation

  • Add the plugin to devDependencies
 $ npm install --save-dev cypress-log-to-output

Usage

  • At the top of cypress/plugin/index.js:
module.exports = (on, config) => {
require('cypress-plugin-retries/lib/plugin')(on)
}

During the test execution we will see the logs in command line as follows:

I hope you have enjoyed this post, follow up here to see what’s next.

References:
https://docs.cypress.io/plugins/index.html#content

Written by 

Gaurav is a Module Lead QA Consultant having experience of more than 4.5 years. He is well familiar with core QA concepts and well versed in designing automation frameworks in a microservice environment. He has good hands-on experience in automation tools like ReadyAPI, SoapUI, postman.io, rest-assured.io, Cypress.io, gatling.io, Selenium (Java & Scala) and others. He actively promotes shifting left in the development cycle and understands the project domain to work closely with the development team. He has a good understanding of different domains like Airline, health Care and Shipping and cruising domain.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading