Skip to main content

How to Use MailSlurp with Playwright and Why DevAssure is Your Go-To Solution

· 7 min read
Divya Manohar
Co-Founder and CEO, DevAssure

In today's fast paced development landscape, it's crucial to ensure the email functionality within your application works seamlessly. Emails are included in various flows of the application to enhance user experience - confirmation emails to validate the email id during sign ups, email OTP for logins, password resets, notifications to end users, or transactional emails. Bugs in emails can prevent new users from signing up or existing users to churn away. In this blog post, we'll explore how you can integrate MailSlurp with Playwright for robust email testing and introduce DevAssure—a next-generation platform that takes email testing to a whole new level.

Playwright MailSlurp Logos

What is Playwright?

Playwright is an open source framework for testing web applications. It's used to automate browser interactions and testing your web applications across multiple browsers. You can build robust automation frameworks using Playwright.

What is MailSlurp?

MailSlurp is an email testing service created for Developers and QA engineers to generate real email addresses, send and receive emails, and automate the testing of email flows.

Key Benefits of Using MailSlurp with Playwright

  • Real Email Addresses: Generate unique email addresses for each test run.
  • Automated Email Validation: Verify that emails are sent, received, and rendered correctly.
  • Seamless Integration: Easily integrate with Playwright to combine UI and email testing.
  • Enhanced Test Coverage: Ensure your application's email functionalities work under real-world conditions.

How to integrate MailSlurp API into your Playwright Automation Script?

Lets consider a sample use case where a user signs up on an application using an email address. As a first step to complete the sign up process, the user is sent a confirmation email which contains a link to verify the email address.

In this case, we need to perform 2 validations -

  1. Validate the email gets triggered to the user inbox
  2. Validate the link in the email is clickable and once clicked completes the email verification process.

Now lets automate the same using Playwright and MailSlurp. We will be using typescript.

Playwright set up instructions

If you haven't installed Playwright yet, do so with the following command. This will set up a new playwright project.

npm init playwright@latest

For Playwright code generator use

npx playwright codegen

This will open a browser window and a code editor.

Learn more about playwright codegen.

MailSlurp Set up

  1. Create an account with MailSlurp.
  2. Once logged in, Under Developer Settings you will be able to find the API key - this API key will be used to send API requests to MailSlurp from Playwright.
  3. Next install mailslurp-client in your playwright project.
npm install axios mailslurp-client

Test Automation Implementation

import { test, expect } from '@playwright/test';
import MailSlurp from 'mailslurp-client';


test('Automate Signup with Email Verification', async ({ page }) => {

const apiKey = "Add API Key here.";
const mailslurp = new MailSlurp({ apiKey });

const inbox = await mailslurp.createInbox();
console.log(`Generated Email: ${inbox.emailAddress} ${inbox.id}`);

await page.goto('Your Application URL');
await page.getByRole('link', { name: 'Join free today' }).click();
await page.getByPlaceholder('Enter your email').click();
await page.getByPlaceholder('Enter your email').fill(inbox.emailAddress);
await page.getByPlaceholder('Enter your name').click();
await page.getByPlaceholder('Enter your name').fill('This is a test - Please ignore');
await page.getByPlaceholder('Enter your password', { exact: true }).click();
await page.getByPlaceholder('Enter your password', { exact: true }).fill(password');
await page.getByPlaceholder('Enter your password again').click();
await page.getByPlaceholder('Enter your password again').fill('password');
await page.getByRole('button', { name: 'Sign Up' }).click();

const email = await mailslurp.waitForLatestEmail(inbox.id, 30000);
console.log(`Received Email: ${email.subject}`);

if (email.body) {
console.log(email.body)
const linkMatch = email.body.match(/href="([^"]+)"/);
const confirmationLink = linkMatch ? linkMatch[1] : null;
console.log(`Confirmation Link: ${confirmationLink}`);
if (confirmationLink) {
await page.goto(confirmationLink);
await expect(page).toHaveTitle('Your email has been verified');
} else {
throw new Error('No confirmation link found in the email.');
}
}
});

Explanation

const apiKey = "Add API Key here.";
const mailslurp = new MailSlurp({ apiKey });
const inbox = await mailslurp.createInbox();
console.log(`Generated Email: ${inbox.emailAddress} ${inbox.id}`);

The above code will help create the email account dynamically using MailSlurp.

const email = await mailslurp.waitForLatestEmail(inbox.id, 30000);

The above code waits for the email to be received in the email inbox.

const linkMatch = email.body.match(/href="([^"]+)"/);

The email content is read and using regex the link is extracted from the email.

Analysis

The above code cannot be used directly within your test automation scripts, for the following reasons

  • Mailslurp API key should be centralized in the test automation repo - so that it can be used across multiple tests
  • Limitations with the number of email inboxes that can be created with MailSlurp - This would not be effective and efficient if you have thousands of tests running in parallel across multiple builds and environments. Additionally, the API rate limits may be restrictive if you're running high volumes of tests or need to generate large numbers of emails.
  • Page Object Model is not followed by Playwright's codegen - This is a huge maintenance overhead. Every test generated needs to modularised
  • Dependency on 3rd party tools and library maintenance when building tests using Playwright.

How can DevAssure be used for Email Testing?

DevAssure is a no code test automation tool that can be used to automate web app, mobile app and API tests. In addition to functional testing, DevAssure also supports testing of non functional use cases for UX validations, Accessibility and Email Testing.

Unlike traditional setups that require juggling multiple tools, DevAssure provides a centralized, streamlined platform for email testing. There is no need to worry about including any libraries or 3rd party dependencies to do Email testing with DevAssure.

How to perform email testing with DevAssure

Here's a detailed documentation on email testing with DevAssure.

Step 1 - Enable Email Testing Feature

  • Navigate to the "Test Lab" section in the DevAssure web application.
  • Under "Test Lab", locate "Email Testing" and enable the feature.

Step 2 - Configure your domain

  • The default domain for email testing is datest.com, and you can configure a custom subdomain under this main domain.
  • DevAssure utilizes subdomains (e.g., *.datest.com) to generate temporary email IDs during testing.
  • You can customize your subdomain with 4 to 6 characters during setup.

DevAssure Email Configuration for testing

Step 3 - Automate using DevAssure

Automate using DevAssure

  • Create new mail ID: Used for generating anonymous email addresses for testing.
  • Wait for email for (mail ID) within (duration) milliseconds: Waits for a new email to arrive in the specified mailbox within the given duration.
  • Get email text content (email): Retrieves the text content/body of the specified email.
  • Open email in browser email: This action opens the specified email in a web browser to visually verify its content or layout.

Playwright with MailSlurp vs DevAssure for Email Testing

Playwright with MailSlurpDevAssure
Requires codingNo coding skills required
Requires additional libraries and account to be set up with MailslurpWorks with a click, no additional libraries or set up required
Upper limit on number of emails that can be created across different plansNo upper limit - enables the creation of static and dynamic emails at run time.
API rate limiting causes issues during parallel execution of thousands of tests.No rate limiting.
Cannot open the email in browser for testing all actions within the email.Ability to open the email in browser to test various actions.


DevAssure


To experience how DevAssure simplifies test automation for email testing, sign up for a free trial.


To explore how the DevAssure test automation platform can enhance your organization's testing capabilities, click the button below to request a personalized demo session with our team of experts.