7

How to record videos only for the failed test cases in CI pipeline using cypress, what configuration shall I make to achieve this? My cypress.json looks like below:

{
    "defaultCommandTimeout": 30000,
    "pageLoadTimeout": 120000,
    "requestTimeout": 30000,
    "responseTimeout": 30000,
    "execTimeout": 30000,
    "taskTimeout": 30000,
    "screenshotsFolder": "cypress/screenshots",
    "trashAssetsBeforeRuns":true,
    "testFiles": "**/*Smoke.spec.ts",
    "video": false,
    "videosFolder": "build/cypress/videos",
    "viewportWidth": 1300,
    "viewportHeight": 1000,
    "animationDistanceThreshold": 50,
    "reporter": "junit",
    "reporterOptions": {
      "mochaFile": "build/cypress/test-output-[hash].xml",
      "toConsole": true,
      "attachments": true
    }
}
1
  • Cypress will automatically capture screenshots when a failure happens during cypress run. Screenshots on failure are not automatically taken during cypress open. - This is what their website says, so probably you are running with cypress open, try cypress run instead.
    – 109149
    Feb 19, 2021 at 12:33

3 Answers 3

5

As far as I know, Cypress doesn't have the option to only keep videos for a failed test over a passing one - but... recently-released Cypress 6.2.0 includes new events that trigger at interesting points around a spec execution or the whole test run. Cypress developers suggest on Github using these for the exact purpose of deleting video files when the tests are successful. With this technique, once Cypress finishes running the tests, the directory containing the videos (videosFolder in your cypress.json) will contain video files for the failed tests only. It's then up to you to have your CI tool archive this directory. Good luck!

3

Cypress confirms there's no way to do this right now during cypress run, since the tests are recorded before knowing if they pass or fail.

Besides the plugin for deleting videos that Cycy refers to, Cypress also suggests setting "videoCompression": false if you want to save on compression time.

2

In addition to Cycy's answer, there is an official solution, which is also mentioned as the final answer in the related and closed github issue.

Two things should be noted:

  1. Depending on which version of Cypress you are using, it either must be included in plugin.js (<10) or cypress.config.js (>=10), directly in the file or as an import. But this is the case for every code which extends the test behaviour.
  2. It may happen that the video file is not found and the script returns with an error Error: ENOENT: no such file or directory, unlink (...), which then breaks the ci pipeline. For this case I added fs.existsSync(results.video). I'm not sure yet if cypress already deletes videos from successful runs, that would be an explanation. Nevertheless, the adapted script looks like this:
on("after:spec", (spec, results) => {
  if (results && results.video) {
    // Do we have failures for any retry attempts?
    const failures = results.tests.some((test) => {
      return test.attempts.some((attempt) => attempt.state === "failed");
    });
    if (!failures && fs.existsSync(results.video)) {
      // delete the video if the spec passed and no tests retried
      return fs.unlinkSync(results.video);
    }
  }
});
1
  • Did you understand why videos were not found? Could it be that they are stored in different nodes/file systems when running Cypress in parallel?
    – Davi Lima
    Nov 22 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.