Cypress failed email notification

Viewed 2648

I am using cypress.io as automation tool I need to send email notification when my cypress test failed. Is there any way to do it?

2 Answers

Instead, you should use Module API. You can run cypress from a javascript file and get the results of running cypress.

Check here. https://github.com/cypress-io/cypress/issues/7441#issuecomment-632535986

Below is an example javascript.

// e2e-run-tests.js
const cypress = require('cypress')

cypress.run({ headless: true, browser: 'chrome' })
  .then(result => {
    const testResults = JSON.stringify(result, null, 2);
    // Module API uses "if (result.failures)", that didn't worked for me. That's why I check with "totalFailed > 0"
    if (result.totalFailed > 0) {
      console.log('Failure(s) in tests! Will send an e-mail')
      sendEmail(testResults);
    }
    process.exit(0);
  })
  .catch(err => {
    console.log('Cypress didn't worked due to some error')
    process.exit(0);
  });

Also, note that inside the sendEmail method you should call process.exit(0); after the email is sent.

Related