How to Send Email Reports When Automation Fails: A Step-by-Step Guide
Introduction
Ensuring a smooth automation testing workflow is essential for maintaining code quality and catching bugs early. However, what happens when an automation suite fails? As teams work in different time zones or rely on asynchronous updates, automating failure notifications becomes a necessity.
In this guide, we'll walk through the process of sending email reports when your automation suite encounters a failure, using AWS Simple Email Service (SES) and Node.js. This will help you alert key stakeholders in real-time and provide direct access to reports that detail the issue.
Why Automate Failure Notifications?
Automated emails ensure that stakeholders and developers are notified instantly about automation failures. This approach:
Saves time by eliminating the need for manual report generation.
Increases visibility into test failures, helping teams resolve issues faster.
Standardizes communication with structured emails that can include direct links to failure reports.
You can also check this Blog
Step-by-Step Guide to Sendi****ng Emails on Automation Failures
Step 1: Set Up AWS SES
AWS Simple Email Service (SES) is a powerful and cost-effective solution to send emails from your automation framework.
Create an AWS SES Account: If you haven’t already, sign up for AWS and navigate to the SES dashboard.
Verify Your Domain: To send emails from your domain, you need to verify your email address or domain. Follow these steps from AWS to verify it here.
Configure Sending Limits: By default, SES restricts email sends to ensure no abuse. You can request an increase in sending limits if necessary.
Step 2: Install AWS SDK in Node.js
We’ll use Node.js for our automation framework. To begin sending emails using AWS SES, install the required SDK.
npm install @aws-sdk/client-ses
npm install aws-sdk-js-codemod
// In Package.json for Reference
"dependencies": {
"@aws-sdk/client-ses": "^3.666.0",
"aws-sdk-js-codemod": "^2.3.2"
}
This SDK will allow us to interact with AWS SES from our Node.js script.
Step 3: Create the Email Script
Here’s a basic script that sends an email when the automation suite fails. It’s built using AWS SES and JavaScript.
//email-script.js
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses';
import { EMAIL_RECIPIENTS, EMAIL_SENDER } from './configs/emailConfig.js';
const buildStatusFailed = process.argv[2] == 0;
const buildName = process.argv[3];
const baseUrl = process.argv[4] || '';
const reportUrl = process.argv[5];
const sesClient = new SESClient({ region: 'eu-asia-1' });
const params = {
Source: EMAIL_SENDER,
Destination: {
ToAddresses: EMAIL_RECIPIENTS,
},
Message: {
Subject: {
Data: `🚨 Automation Failure Report: ${buildName} 🚨`,
Charset: 'UTF-8',
},
Body: {
Html: {
Data: `<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
background-color: #f4f4f4;
padding: 0 20px;
}
.email-container {
background-color: #ffffff;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
max-width: 600px;
margin: auto;
}
.header {
font-size: 18px;
font-weight: bold;
color: #333333;
margin-bottom: 20px;
text-align: center;
}
.info {
margin-bottom: 20px;
}
.info span {
font-weight: bold;
}
.footer {
margin-top: 20px;
color: #888888;
font-size: 12px;
}
.button {
display: block;
width: fit-content;
margin: 20px auto;
background-color: #007BFF;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
}
.button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="email-container">
<div class="header">🚨 Automation Failure Report for Build [${buildName}] 🚨</div>
<p>Dear QA Team & Manager,</p>
<p>We encountered a failure during the Automation Schduler run for the following build:</p>
<div class="info">
<ul>
<li><span>Build Name:</span> ${buildName}</li>
<li><span>Base URL:</span> ${baseUrl || 'N/A'}</li>
<li><span>Failure Timestamp:</span> ${new Date().toLocaleString()}</li>
<li><span>Failure Reason:</span> Please review the below report for details</li>
</ul>
</div>
<p>Please review the full Automation Report and take necessary action by clicking the button below:</p>
<a href="${reportUrl}" class="button">View Report</a>
<p>We are actively investigating the issue and will provide an update as soon as possible. If you have any questions or need further clarification, feel free to reach out to the Automation Team.</p>
<div class="footer">
<p>Best Regards,</p>
<p>The Automation Team</p>
<p>Email: <a href="mailto:${EMAIL_SENDER}">${EMAIL_SENDER}</a></p>
<p><small>Please note: This is an automated email. Do not reply directly to this message.</small></p>
</div>
</div>
</body>
</html>`
}
}
}
};
const sendEmail = async () => {
try {
const data = await sesClient.send(new SendEmailCommand(params));
console.log('Email sent successfully', data);
} catch (err) {
console.error('Error sending email', err);
}
};
/* If you want to run For specific Suite only */
//if (buildStatusFailed && 'E2E-Test-Scheduler'.includes('E2E-Test-Scheduler')) {
// await sendEmail();
//}
if (buildStatusFailed) {
sendEmail();
}
This script is designed to:
Retrieve information from the failed build (build name, report URL, base URL).
Send an email with detailed failure information, such as the timestamp and a link to the full report.
Step 4: Customize the Email Content
To ensure clear communication, customize your email content:
Email Subject: Clear and action-oriented subject lines are essential.
- Example:
🚨 Automation Failure: Build [${buildName}] - Immediate Attention Required
- Example:
Email Body: Provide essential details (build name, error message, failure time) and a link to the full report.
Call to Action: Ensure there’s a clear button or link to view the report.
Step 5: Execute the Script When the Automation Fails
Ensure that your script is triggered when the automation suite encounters a failure. This can be done by integrating it with your CI/CD pipeline or automation scheduler. For example, you can pass the build name, report URL, and other parameters via command line arguments to this Node.js script.
Step 6: Notify Stakeholders
List the stakeholders and ensure they are added to the email configuration file:
// emailConfig.js
const EMAIL_RECIPIENTS = [
'manager@example.com',
'qa-team@example.com',
];
const EMAIL_SENDER = 'automation@yourdomain.com';
export { EMAIL_RECIPIENTS, EMAIL_SENDER };
With this configuration, the email will be sent to the specified recipients each time the automation suite fails.
How to Run or Trigger the Script?
To test the script, you need to run the command below.
node email-script.js
For the framework inclusion of the script run, you have to place it in the YAML file as shown below,
version: 0.2
env:
variables:
BROWSERSTACK_USER: Userkey
BROWSERSTACK_KEY : PassKey
phases:
install:
runtime-versions:
nodejs: 20
java: corretto17
pre_build:
commands:
# Move to Automation directory if req
- cd wdio-automation
# Installation of all required packages.
- npm install
build:
commands:
- npm run test
post_build:
commands:
- echo Build completed on `date`
- ls -lrt src/test/
- aws s3 sync test-report/allure-report s3://cucumbertesting/wdio-automation/$CODEBUILD_BUILD_ID/report --only-show-errors
- REPORT_URL=https://uniquekey.cloudfront.net/wdio-automation/$CODEBUILD_BUILD_ID/report/index.html
- echo Report URL $REPORT_URL
- node email-script.js $CODEBUILD_BUILD_SUCCEEDING $BUILD_NAME $URL_SET $REPORT_URL
You can also check this Blog
Conclusion
Automation failure notifications are vital for maintaining the integrity of your testing process. By automating email reports, you can ensure that stakeholders are informed of issues as soon as they arise, speeding up the process of investigation and resolution. With AWS SES and Node.js, you can efficiently handle failure notifications, reducing downtime and increasing team collaboration.
Subscribe to my newsletter
Read articles from Hardik Chotaliya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Hardik Chotaliya
Hardik Chotaliya
👩💻 Automation QA Engineer || SDET ||✍🏼 Tech Blogger || WebdriverIO, JavaScript, TypeScript || based in 🇮🇳 || 🤓 Life-long learner ||Love ☕ & 🏏