A Practical Guide to Mastering Cypress Email Testing

This guide delves into the practicalities of using Cypress, an advanced web application testing framework that can be integrated with various email testing tools. You’ll gain insights into executing crucial test cases, including ensuring the reliability of email services for functions like password reset and other essential workflows. We’ll cover how to validate email delivery, content accuracy, and cross-platform rendering.

If you’re familiar with Cypress’s functionalities, jump directly to the how-to section.

Why Use Cypress for Email Testing?

If you’re new to Cypress or considering it for your email testing needs, here’s a quick overview of why it stands out:

  • Rapid Test Execution: Cypress’s speed aligns with agile development needs, ensuring quick feedback loops in your test runs.
  • User-Friendly Syntax: Cypress offers straightforward and easy-to-understand syntax.
  • Consistent Performance: It delivers reliable results consistently, a crucial factor for trustworthy testing.
  • Dynamic Content Handling: Cypress is adept at managing dynamic content, vital for testing emails with varying user interactions and personalization.
  • Integration with Plugins and Services: It seamlessly integrates with various email testing services and plugins, creating an efficient ecosystem for email testing in controlled environments.

These features make Cypress a robust choice for email testing, ensuring thorough and efficient verification of email functionalities.

Before Testing: Setting Up Cypress

Before diving into the ins and outs of email testing with Cypress, you’ll need a few prerequisites, including certain dependencies, to ensure a smooth setup.

  • You must install Node.js (version 14 or higher) on your system, as Cypress is a Node.js application.
  • npm (Node Package Manager) should be available to manage the packages Cypress requires.
  • A text editor or Integrated Development Environment (IDE) is necessary to write and manage your test scripts.

Installing Cypress is a straightforward process once you have the prerequisites, including setting up your environment variable and config.

Open your terminal or command prompt, navigate to your project directory, and install Cypress using the npm command:

npm install cypress

After the installation is complete, you can verify it by launching the Cypress Test Runner with the following command:

./node_modules/.bin/cypress open

The next step is to create a new file for your tests. In the cypress/e2e/ directory, create a file named test.cy.js. This file will house your email testing scripts.

How to run different types of email tests with Cypress

Cypress can perform various email tests, each designed to validate different aspects of email functionality. Here’s a categorized list:

  • Checking for Broken Links: Ensures all hyperlinks in an email lead to the intended destinations.
  • Validating Email Content: Verifies the accuracy of text, images, and other content within the email.
  • Testing Email Rendering: Checks how emails are displayed on various devices and screen sizes.

Checking for Broken Links

Correct and secure email links are key to your business’s success. Broken links mean trouble – they frustrate users, hurt conversions, and can even cost you customers and revenue. 
To check for broken links using Cypress, you can place the following code in cypress/e2e/test.cy.js:

describe('Email broken links', () => {
it('Find all broken links', () => {

// Visit the email HTML file
cy.visit('emailcontent.html')


// Get all the links in the email
cy.get('a').each((link) => {
// Check if the link has an href attribute
if (link.prop('href')) {
// Make a request to the link
cy.request({
// Set the URL of the request to the link's href attribute
url: link.prop('href'),
// Do not fail the test if the response status code is not 200
failOnStatusCode: false
}).then((response) => {
// Check if the response status code is not 200
if (response.status !== 200) {
// Log the broken link to the Cypress console
cy.log(`Broken link: ${link.prop('href')}`);
}
});
}
});

});
});

This script analyzes the email’s HTML file, iterates over each link, and makes a request to the URL specified in the href attribute. If the response status code is not 200, it logs the broken link.

Validating Email Content

It’s crucial for any email sent from your app to its users to have clear and accurate content.
After all, no one wants to receive emails with confusing “tech junk” text, which can frustrate users and increase your unsubscribe rate.

To validate the content of such emails, you can use the following Cypress script:

describe('Validating Email', () => {
it('Validating Email Content', () => {

cy.visit(emailcontent.html')
// Get the email body
cy.get('.email-body').then((emailBody) => {
// Validate that the specific text is present in the email body
expect(emailBody.text()).to.contain('specific text');
});

});
});

This code checks that the email body contains specific text, ensuring the email’s content is as expected. Additionally, Cypress commands can be used to automate the verification of email contents, such as checking for a specific verification code in a confirmation email.

Aligning email content and delivery practices with best practices is key to maintaining a strong domain reputation, which is crucial for reliable email deliverability and engagement.

...

Thank you for choosing this article! To find out other types of Cypress email testing and verification, read full version of the article in Maitrap blog!