Skip to content

Instantly share code, notes, and snippets.

@Bkucera Bkucera/cypress-retries.js
Last active Mar 29, 2019

Embed
What would you like to do?
Crude implementation of retrying failed spec files in Cypress. Now with Parallelization support!
const _ = require('lodash')
const cypress = require('cypress')
const crypto = require('crypto')
/*
* Add your config args in DEFAULT_CONFIG
* Set number of retries in MAX_NUM_RUNS
*/
const MAX_NUM_RUNS = 3
const DEFAULT_CONFIG = {
// you can omit 'spec' if you just want all your tests to run
// spec: './cypress/integration/retries/**/*',
browser: "electron",
record: true,
// parallelization options
group: "main",
parallel: true,
// only necessary if recording specs from a local machine
// ciBuildId: crypto.randomBytes(6).toString('hex')
}
// id unique to the machine
const uniqueId = crypto.randomBytes(3).toString('hex')
let totalFailuresIncludingRetries = 0
const run = (num, spec, retryGroup) => {
num += 1
const config = Object.assign(_.cloneDeep(DEFAULT_CONFIG), {
env: {
numRuns: num,
},
})
if (spec) config.spec = spec
if (retryGroup) config.group = retryGroup
return cypress.run(config)
.then((results) => {
if (results.totalFailed) {
totalFailuresIncludingRetries += results.totalFailed
// rerun again with only the failed tests
const specs = _(results.runs).filter("stats.failures").map("spec.relative").value()
console.log(`Run #${num} failed.`)
// if this is the 3rd total run (2nd retry)
// and we've still got failures then just exit
if (num >= MAX_NUM_RUNS) {
console.log(`Ran a total of '${MAX_NUM_RUNS}' times but still have failures. Exiting...`)
return process.exit(totalFailuresIncludingRetries)
}
console.log(`Retrying '${specs.length}' specs...`)
console.log(specs)
// If we're using parallelization, set a new group name
let retryGroupName
if (DEFAULT_CONFIG.group) {
retryGroupName = `${DEFAULT_CONFIG.group}: retry #${num} (${specs.length} spec${specs.length===1?'':'s'} on ${uniqueId})`
}
// kick off a new suite run
return run(num, specs, retryGroupName)
}
})
}
// kick off the run with the default specs
run(0)
@thasherwin

This comment has been minimized.

Copy link

thasherwin commented Aug 30, 2018

How do you pass arguments? For example the config file or the reporter

I have the following line in my package.json:
cypress open --env configFile=testEnvConfig but changing this to node cypress-retries.js --env configFile=testEnvConfig will not use the config file. The test is executed using localhost which is not the correct host. The correct host is in the config file

@Bkucera

This comment has been minimized.

Copy link
Owner Author

Bkucera commented Aug 30, 2018

@thasherwin Lines 21-28 is where you would pass your configuration options

@germyjen

This comment has been minimized.

Copy link

germyjen commented Oct 24, 2018

If you want to use environment variables other than those saved in your cypress.json (for example, in your circle or other ci tool) this is the solution that I used:

  1. Create an e2e.sh file wherever you store your cypress.json and cypress-retries.js files
  2. chmod the file so that it can be run
  3. paste the code snippet I shared below, replacing the "export" variables with your own. Official documentation can be found here: https://docs.cypress.io/guides/guides/environment-variables.html#Option-3-CYPRESS
  4. edit your config.yml file to run your e2e.sh file. Example in snippet 2.
// e2e.sh file

#! /bin/sh
export CYPRESS_baseUrl=$E2E_BASE_URL
export CYPRESS_API_BASE_URL=$E2E_API_BASE_URL
export CYPRESS_TEST_PASSWORD=$E2E_TEST_PASSWORD
node cypress-retries.js
//config.yml snippet

steps:
      - <<: *checkout_repo
      - run: echo "Starting end-to-end tests"
      - <<: *install_dependencies
      - run:
          name: Run end-to-end tests
          command: ./e2e.sh
@VSathyamoorthy

This comment has been minimized.

Copy link

VSathyamoorthy commented Jan 11, 2019

I have added this script to my spec, could you please explain how do I execute a test suite from command line? the project using this below command, but got a respose

run command: node cypress-retries.js --record --key 07b5538b-87ec-4b24-945f-c51ca37cd837
response: We looked but did not find a cypress.json file in this folder: C:\xplanbase\apps\apex\9.99.999\cypress\integration\1_Cypress_retry

the cypress.json file is in this path C:\xplanbase\apps\apex\9.99.999

Thanks in advance your support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.