Verifying Email Links Using Playwright

In modern web applications, email verification is a critical feature used for confirming user identities, resetting passwords, or activating accounts. As part of end-to-end testing, verifying email links automatically can significantly improve the reliability and efficiency of your test suite. Playwright, a popular open-source automation framework developed by Microsoft, is ideal for such tasks due to its robust browser automation capabilities and support for multiple programming languages.

This blog will guide you through how to use Playwright to verify email links, a common scenario in automated QA testing.


Why Verify Email Links?

Verifying email links ensures:

  • Users receive correct and timely emails.
  • The links inside emails direct users to the expected pages (e.g., account confirmation or password reset).
  • Expired or invalid links are handled gracefully.
  • System email delivery and content formatting work as intended.

Manually testing these links can be time-consuming and error-prone. Automating the process increases test coverage and saves time.


Tools & Setup Required

To verify email links using Playwright, you'll typically use:

We'll use Mail

  • Playwright: For browser automation.
  • MailCatcher / MailHog / MailSlurp / Mailosaur: For capturing and reading test emails.
  • Node.js or Python: Depending on your programming preference.
  • Test Email Service or SMTP Stub: Used to send test emails to a catch-all inbox.

Hog in this example as it’s a lightweight, open-source SMTP server for testing.


Step-by-Step Guide

1. Start MailHog

MailHog runs locally and provides a web UI and an API to fetch emails.

bash

docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog

  • SMTP: localhost:1025
  • Web UI: http://localhost:8025
  • API: http://localhost:8025/api/v2/messages


2. Trigger an Email via the Application

Using Playwright, simulate a user action like registration or password reset that sends an email.

javascript


await page.goto('https://your-app.com/register');

await page.fill('#email', 'test@example.com');

await page.click('#submit');

3. Fetch the Email from MailHog

Use an HTTP client like axios or requests to get the latest email.

javascript


const axios = require('axios');

const response = await axios.get('http://localhost:8025/api/v2/messages');

const messages = response.data.items;

const latestEmail = messages[0];

const htmlBody = latestEmail.Content.Body;


// Extract the link using regex

const linkRegex = /https?:\/\/[^"]+/;

const match = htmlBody.match(linkRegex);

const verificationLink = match ? match[0] : null;

4. Navigate to the Email Link with Playwright

After extracting the link, use Playwright to visit it and verify the expected behavior.


javascript

await page.goto(verificationLink);

await expect(page).toHaveText('Your email has been verified');

Best Practices

  • Use a test email inbox to avoid sending emails to real users during testing.
  • Clean up inbox before or after tests to avoid false positives.
  • Handle delays with retries or wait mechanisms, as email delivery might take a few seconds.
  • Assert correct redirection and messages to validate the full verification flow.


Benefits of Email Link Testing with Playwright

  • Ensures end-to-end flow including backend email service.
  • Increases confidence in production readiness.
  • Reduces manual testing effort for repetitive email scenarios.
  • Works well with CI/CD pipelines for continuous testing.

Conclusion

Verifying email links is a crucial part of automated testing for user workflows. With Playwright, combined with tools like MailHog, you can easily automate the process of capturing, parsing, and navigating through email links. This approach ensures that your application's email features work reliably and provide a seamless experience for users.


Learn Playwright Testing Training

Read More: Testing File Upload and Download in Playwright

Visit IHUB Talent Institute Hyderabad
Get Direction

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Using Hibernate ORM for Fullstack Java Data Management

Creating a Test Execution Report with Charts in Playwright