DEV Community

Cover image for CAPTCHA Tried to Stop Me: How MailSlurp Saved Email Tests
Nimesha Kumarapperuma
Nimesha Kumarapperuma

Posted on

CAPTCHA Tried to Stop Me: How MailSlurp Saved Email Tests

MailSlurp 101:

Email verification is one of the most difficult tasks to automate in automated testing. OTP codes, password reset emails, sign-up links, and magic links typically lead to genuine inboxes like Gmail or Outlook, which are not conducive to automation.

Mailslurp changes the game at this point.

An email testing tool and API called MailSlurp enables testers to:

  1. Make short-term email accounts
  2. Get actual emails that the application has sent.
  3. Use API calls to read email content.
  4. Extract attachments, URLs, OTPs, and more
  5. Safely use emails in automation systems such as:
1. Playwright
2. Selenium 
3. Cypress
4. Appium
Enter fullscreen mode Exit fullscreen mode

An automation script can read emails programmatically in seconds, eliminating the need to manually check a real Gmail mailbox.

Why MailSlurp for Automation Testing?

1.Completely Automated Email Testing

Test can:

  • Establish an inbox.
  • Send an email using the app.
  • Automatically read the email.
  • Retrieve the verification link and OTP.
  • Proceed with the test flow.
  • No manual labor is required.

2.No Real Gmail / Outlook Dependency

Not required to:

  • Passwords for stores
  • Handle security blocks in your inbox.
  • Everything is test-safe and driven by API.

3.OTP, Verification Link & Magic Link Testing

Typical use cases:

  • Verification of email after registration
  • Link to reset your password
  • OTP for login
  • Email invitations
  • Email body parsing an d value extraction are made possible by MailSlurp

4.Isolated Test Data

Every test may have a separate inbox. This prevents:

  • Collisions in emails
  • Perusing incorrect emails
  • Tests that are erratic

This is ideal for parallel test execution in CI/CD workflows.

5.Works with CI/CD Pipelines

MailSlurp works well in:

1. GitHub Actions
2. GitLab CI
3. Jenkins
4. Azure DevOps
Enter fullscreen mode Exit fullscreen mode

No UI login required

Email Testing Without the Drama: How MailSlurp Fixes the Mess

Problem with Normal Email Testing How MailSlurp Fixes It
Captcha blocks automation No CAPTCHA involved
Manual OTP checking Fully automated via API
Shared test inbox issues Each test gets its own inbox
Email delays cause flaky tests Built-in waiting mechanisms
Hard to validate email content Programmatic email parsing
Security issues with Gmail creds API key-based access
Inbox polluted with test emails Auto-delete inboxes

Setting Up MailSlurp with Playwright

Prerequisites

Before starting, make sure you have:

1. Node.js installed
2. A Playwright project set up
3. A MailSlurp account
4. MailSlurp API Key
Enter fullscreen mode Exit fullscreen mode

Get an API key from the MailSlurp dashboard after signing up.

Step 1: Install MailSlurp SDK

Run this in your project root:
npm install mailslurp-client dotenv

Step 2: Store API Key Securely
Create a .env file in your project:
MAILSLURP_API_KEY=your_api_key_here

Step 3: Configure MailSlurp Client
Create a helper file: mailslurp.js

const { MailSlurp } = require('mailslurp-client');
require('dotenv').config();

const mailslurp = new MailSlurp({ apiKey: process.env.MAILSLURP_API_KEY });

async function createInbox() {
  const inbox = await mailslurp.createInbox();
  return inbox; // inbox.id and inbox.emailAddress
}

async function waitForOtp(inboxId) {
  const email = await mailslurp.waitForLatestEmail(inboxId, 60000);
  const body = email.body || email.bodyExcerpt || '';
  const otpMatch = body.match(/\b\d{6}\b/);

  if (!otpMatch) {
    throw new Error('OTP not found in email body');
  }

  return otpMatch[0];
}

async function deleteInbox(inboxId) {
  await mailslurp.deleteInbox(inboxId);
  console.log(`Inbox ${inboxId} deleted.`);
}

module.exports = { createInbox, waitForOtp, deleteInbox };

Enter fullscreen mode Exit fullscreen mode

Step 4: Create Inbox & Use It in Playwright Test

const { test, expect } = require('@playwright/test');
const { mailslurp } = require('../utils/mailslurp');

test('User signup with email verification', async ({ page }) => {
  // 1. Create a new inbox
  const inbox = await mailslurp.createInbox();
  const emailAddress = inbox.emailAddress;

  // 2. Use the email in your app signup form
  await page.goto('https://your-app-url.com/signup');
  await page.fill('#email', emailAddress);
  await page.fill('#password', 'Test@1234');
  await page.click('#signupBtn');

  // 3. Wait for verification email
  const email = await mailslurp.waitForLatestEmail(inbox.id, 30000);

  // 4. Extract verification link or OTP
  const body = email.body;
  const otpMatch = body.match(/\b\d{6}\b/); // example for 6-digit OTP
  const otp = otpMatch[0];

  // 5. Continue verification flow
  await page.fill('#otp', otp);
  await page.click('#verifyBtn');

  await expect(page).toHaveURL(/dashboard/);
});
Enter fullscreen mode Exit fullscreen mode

Step 5: Extract Links from Email
If your email contains a verification link:

const linkMatch = email.body.match(/https?:\/\/[^\s]+/);
const verifyLink = linkMatch[0];

await page.goto(verifyLink);

Enter fullscreen mode Exit fullscreen mode

How to Keep Tests Happy

  • Create a new inbox per test
  • Delete inboxes after tests
  • Use regex to extract OTPs safely
  • Add retry logic for flaky email delivery
  • Keep MailSlurp logic in helper files

Top comments (3)

Collapse
 
ganishka profile image
Ganishka

Interesting article

Collapse
 
kandege_rajika_6bc28877aa profile image
kandege rajika

Very informative

Collapse
 
duneesha_karannagoda profile image
Duneesha Karannagoda

Full of insights.