Tools: CAPTCHA Tried to Stop Me: How MailSlurp Saved Email Tests

Tools: CAPTCHA Tried to Stop Me: How MailSlurp Saved Email Tests

Source: Dev.to

MailSlurp 101: ## Why MailSlurp for Automation Testing? ## Email Testing Without the Drama: How MailSlurp Fixes the Mess ## Setting Up MailSlurp with Playwright ## Prerequisites ## How to Keep Tests Happy 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: An automation script can read emails programmatically in seconds, eliminating the need to manually check a real Gmail mailbox. 1.Completely Automated Email Testing 2.No Real Gmail / Outlook Dependency 3.OTP, Verification Link & Magic Link Testing Every test may have a separate inbox. This prevents: This is ideal for parallel test execution in CI/CD workflows. 5.Works with CI/CD Pipelines MailSlurp works well in: Before starting, make sure you have: 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 Step 4: Create Inbox & Use It in Playwright Test Step 5: Extract Links from Email If your email contains a verification link: Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK: 1. Playwright 2. Selenium 3. Cypress 4. Appium Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: 1. Playwright 2. Selenium 3. Cypress 4. Appium CODE_BLOCK: 1. Playwright 2. Selenium 3. Cypress 4. Appium CODE_BLOCK: 1. GitHub Actions 2. GitLab CI 3. Jenkins 4. Azure DevOps Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: 1. GitHub Actions 2. GitLab CI 3. Jenkins 4. Azure DevOps CODE_BLOCK: 1. GitHub Actions 2. GitLab CI 3. Jenkins 4. Azure DevOps CODE_BLOCK: 1. Node.js installed 2. A Playwright project set up 3. A MailSlurp account 4. MailSlurp API Key Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: 1. Node.js installed 2. A Playwright project set up 3. A MailSlurp account 4. MailSlurp API Key CODE_BLOCK: 1. Node.js installed 2. A Playwright project set up 3. A MailSlurp account 4. MailSlurp API Key CODE_BLOCK: 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 CODE_BLOCK: 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 }; CODE_BLOCK: 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 }; COMMAND_BLOCK: 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 COMMAND_BLOCK: 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/); }); COMMAND_BLOCK: 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/); }); CODE_BLOCK: const linkMatch = email.body.match(/https?:\/\/[^\s]+/); const verifyLink = linkMatch[0]; await page.goto(verifyLink); Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: const linkMatch = email.body.match(/https?:\/\/[^\s]+/); const verifyLink = linkMatch[0]; await page.goto(verifyLink); CODE_BLOCK: const linkMatch = email.body.match(/https?:\/\/[^\s]+/); const verifyLink = linkMatch[0]; await page.goto(verifyLink); - Make short-term email accounts - Get actual emails that the application has sent. - Use API calls to read email content. - Extract attachments, URLs, OTPs, and more - Safely use emails in automation systems such as: - 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. - Passwords for stores - Handle security blocks in your inbox. - Everything is test-safe and driven by API. - 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 - Collisions in emails - Perusing incorrect emails - Tests that are erratic - 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