Streamlining Test Reports: A Guide to Sending Test Reports via Email with SendGrid Integration - Cypress

Priyanka Gupta
Tradeling Technology Blog

--

In my previous blog post, I demonstrated how to generate mochawesome reports. In this article, I will show you how to send these reports via email using SendGrid integration. SendGrid is a cloud-based SMTP provider that eliminates the need for maintaining email servers. I will guide you through the process of setting up a SendGrid account and integrating it with Cypress.

To add the SendGrid environment variables to a file called sendgrid.env, follow these steps:

  1. Go to the SendGrid website: https://app.sendgrid.com/login and create a new account (if you do not already have one)
  2. Under Settings in the navigation bar, click Sender Authentication, and now proceed with Single Sender Verification by selecting Get Started under Verify an Address
  3. Under Settings in the navigation bar, click Sender Authentication, and now proceed with Single Sender Verification by selecting Get Started under Verify an Address
Sender Authentication Page

3. Click on Create New Sender, Fill in all the mandatory data and click on create to Create a New Sender:

Single Sender Verification Page

4. “From Email address” needs to be verified (received from SendGrid)

Verify Single Sender Email

5. Verified the sender

Verified Sender

6. Click on Settings -> API Keys. Now, click on Create Key button and enter a name for your API Key

Create API keys

8. The key is generated, now you copy it and save it. (Key can be copied only once)

Generated API Key

Integrate SendGrid with code:

  1. Create a file named “sendgrid.env” in the cypress folder to add SendGrid environment variables and include the code below:

Sample sendgrid.env:

export SENDGRID_API_KEY='<sendgrid api key>'
export SENDGRID_API_USER='<sendgrid user email>'
export SENDGRID_EMAIL_TO='<email>'
export SENDGRID_EMAIL_CC='<email1,email2,....>'

2. Run “source cypress/sendgrid.env” to load the env variables to use this file: source <path of sendgrid.env file>

sendgrid.env file

Sending an email:

To send an email with SendGrid, we’ll need to install the SendGrid Node helper library. The @sendgrid/mail is a package provided by SendGrid for sending emails through their API. Run the below command:

yarn add @sendgrid/mail

Create a file named “sendmail-report.js” in the cypress folder and include the code below:

sendmail-report.js file

Please find the below sample code:

const cypress = require('cypress');
const sgMail = require('@sendgrid/mail');
const fs = require('fs');
const minimist = require('minimist');

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const args = minimist(process.argv.slice(2));

function sendMail (message) {
const htmlFile = fs.readFileSync("cypress/report/index.html").toString("base64");

const email = {
from: process.env.SENDGRID_API_USER,
to: process.env.SENDGRID_EMAIL_TO,
subject: 'Test-Automation-Report',
text: message,
attachments: [
{
content: htmlFile,
filename: "index.html",
type: "text/html",
disposition: "attachment"
}
]
};

sgMail.send(email)
.then(() => {
console.log('Email sent');
})
.catch((error) => {
console.log('Email failed', error);
});
};

cypress.run({
spec: args['spec'],
}).then(() => {
sendMail('Please find the attached HTML report');
});

A Closer Look at the Framework Structure:

framework structure with generated reports

Add the below script in package.json file:

node <path of sendemail-report.js file>

"email-report": "node cypress/sendmail-report.js"

Verify that all installed files are listed in the “package.json” file.

package.json

Run the yarn email-report to send the created report

Email sent

Check the email sent on the given email address in “SENDGRID_EMAIL_TO”:

Test report sent via Email

I wish you a happy learning journey. Keep learning and sharing :)

--

--

Priyanka Gupta
Tradeling Technology Blog

Over 8 years of experience as a SDET in Web, Mobile and API automation, Cypress, JS, Selenium, Java, BDD, Appium, Postman, Jmeter