Elevate Your Email Testing with MailSlurp & Cypress

Elevate Your Email Testing with MailSlurp & Cypress

Introduction

Email is a vital element in the majority of today’s applications. It serves as a user identifier, enabling functionalities such as sign-ups, logins, password resets, and account confirmations. Despite its significance, the end-to-end testing of processes that rely on email can often be a challenging task. In this article, we’ll demonstrate how to automate email flows using MailSlurp and Cypress. While we’re using Cypress in this example, the same process can be implemented with any test framework or automation tool, regardless of the programming language.

Tools Used

Our tech stack for this tutorial includes Cypress for UI automation and the MailSlurp API.

Setting Up MailSlurp

First, let’s walk through the process of setting up a MailSlurp account. You can create a free account here. Upon account creation, an API key is generated, which will be used for integration purposes.

You can then create an inbox for sending/receiving emails. The generated email will follow the format of inbox_id@mailslurp.com. Remember to keep this Inbox ID safe for further integration.

Integrating MailSlurp into Our Tests

The package required for this integration is mailslurp-client. You can install this package using either npm or yarn:

npm install mailslurp-client
# OR
yarn add mailslurp-client

Next, import the package and instantiate the client:

import { MailSlurp } from 'mailslurp-client';

const apiKey = `your_api_key`;
const mailslurp = new MailSlurp({ apiKey: `${apiKey}` });

You can then wait for the latest email to reach your inbox:

const inboxId = `your_inbox_id`;
const email = await mailslurp.waitForLatestEmail(inboxId);

Alternatively, you can use the waitForMatchingEmails method to wait for a specific email that matches your defined conditions:

const email = await mailslurp.waitForMatchingEmails({
  inboxId,
  count,
  matchOptions: [
    {
      field: "SUBJECT",
      should: "CONTAIN",
      value: "Please reset your My Account password",
    },
  ],
});

This email variable will contain your entire email data. You can log the email details like so:

console.log(`email.to -> ${email.to}`);
console.log(`email.from -> ${email.from}`);
console.log(`email.body -> ${email.body}`);

Parsing the Email Body

To fetch the password reset link, we need to parse the email body. The email.body data is a string that represents the entire email in HTML format. To parse this email body, we need to use another npm package called node-html-parser.

npm install node-html-parser
# OR
yarn add node-html-parser

Import the parse method from the node-html-parser package and use it to parse the HTML:

import { parse } from 'node-html-parser';

const root = parse(email.body);
const resetPasswordUrl = root.querySelector('body a').rawText;

Navigating to the Reset Password URL

Finally, navigate to the URL fetched in the previous step. You can use the visit method of Cypress:

cy.visit(resetPasswordUrl);

By following these steps, you can efficiently automate email flows by calling the APIs, thereby simplifying the process of validating email-related functionalities in your tests

Flexibility with MailSlurp

While we’ve used Cypress and JavaScript in this example, it’s important to note that MailSlurp is not limited to JavaScript-based automation or UI automation tools. MailSlurp provides SDKs for C#, Golang, Java, Node.js/JavaScript/TypeScript, PHP, Python, and Ruby. This means you can use MailSlurp with any test framework like Jest, JUnit, Pytest, etc., or any UI or API automation tool like Supertest, Playwright, etc. If your requirement is to implement this in any other language or framework, you can hit the MailSlurp APIs directly. This flexibility makes MailSlurp a powerful tool for automating email flows in a wide range of scenarios.

You can find these multiple integration details here.

Data Security Considerations

When using MailSlurp, it’s crucial to remember that the test emails will go out to the MailSlurp server. This means that test data is going outside your organisation. Therefore, you may need to obtain necessary approvals from your Security Team if there are any company policies regarding data security. Also, be careful with Personally Identifiable Information (PII) data. Always ensure that you are compliant with your organisation’s data security policies and regulations.

Final Notes

In this tutorial, we’ve demonstrated how to automate email flows using MailSlurp and Cypress. We’ve walked through the process of setting up a MailSlurp account, integrating it into our tests, parsing the email body, and navigating to a reset password URL. While the example in this blog uses the MailSlurp JavaScript SDK with Cypress, the same process can be implemented in other programming languages as well. If an SDK is available for your language of choice, you can use it for a smoother experience. Alternatively, you can make simple API calls in any programming language-based automation tool to achieve the same results.

Automating email flows is a crucial aspect of end-to-end testing for many applications. By leveraging tools like MailSlurp and Cypress, we can efficiently validate email-related functionalities in our tests. This not only improves the reliability of our applications but also enhances the overall user experience.

Remember, the key to successful automation lies in comprehending the tools you have at hand and utilising them to their full potential. By adopting the correct strategies, you can optimise your testing procedures and guarantee the smooth operation of your applications. Keep exploring, keep learning, and keep innovating!

prateek.chauhan@shinesolutions.com
No Comments

Leave a Reply