How to Store the Cypress Run Result into Variable

367 views Asked by At

I am using Cypress with Cucumber-preprocessor ,I need to store the result of Cypress Run that get printed in the console after execution I need to pass this data to api call

Below is the example of Cypress Run Result - test case pass, fail, time taken etc (i need to capture this data into variable to pass to api

  2 passing (34s)


  (Results)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Tests:        2                                                                                │
  │ Passing:      2                                                                                │
  │ Failing:      0                                                                                │
  │ Pending:      0                                                                                │
  │ Skipped:      0                                                                                │
  │ Screenshots:  0                                                                                │
  │ Video:        true                                                                             │
  │ Duration:     33 seconds                                                                       │
  │ Spec Ran:     Test.feature                                                               │

any way to achieve this ??

2

There are 2 answers

0
Otto On

You might consider using the Module API.

This is essentially an expanded version of npx cypress run... that gives you easy access to the results (and errors) of the run for further processing.

Note that results are not formatted for the console, but that may suit your requirement better.

Node script called e2e-run-tests.js

const cypress = require('cypress')
const nodemailer = require("nodemailer");

cypress
  .run({
    // the path is relative to the current working directory
    spec: './cypress/e2e/examples/actions.cy.js',
  })
  .then((results) => {
    console.log(results)
    const transporter = nodemailer.createTransport({
      ...

  })
  .catch((err) => {
    console.error(err)
  })
1
Yomara Mora On

You can access each test case result with this.currentTest.state

So, to achieve what you are describing I would get each test case result inside an afterEach hook (you need to make sure that your afterEach method is not written in arrow function format) and I would store those results somewhere. At the end Cypress run I would send them to the API.

Hope this helps.