Perform custom actions on Cypress test retry

Viewed 151

I am using retries in my Cypress test. Every time a test is retried, I want to perform an action (e.g. log details about the failure in a file).

I thought the command:retry from the catalog of events might be useful, but I am not sure what does the parameter mean and how can I use it.

2 Answers

There is a cyclope plugin that may be covering what you are wanting to do with test failures. This plugin includes a utilty function savePageIfTestFailed, which you can call in an afterEach() inside your support/index.js file.

You may find command:retry is a bit too granular, try the on fail event.

Cypress.on('fail', (error, runnable) => {
  debugger
  
  // take a look at the parameters
  console.log('error', error.message)
  console.log('runnable', runnable)

  // for example
  let currentTest = runnable.ctx.currentTest
  let title = runnable.title

  throw error // throw error to have test still fail
})

About retries

The runnable object passed to the on('fail') event has a couple of properties giving useful info about the retries.

Say you set {retries: 2} on the the test, the fail event is called for every attempt that fails.

On the first & second run, runnable.final === false.

On the third and last attempt, runnable.final === true plus runnable.prevAttempts has an array of the previous two (failed) attempts.

I haven't checked out what you get if, say, the third attempt passes.

Related