ThisCalculator

Everyday

Playwright Email Verification: Reliable Graph Mail Test Flow?

Updated · Standard formula, shown below

Max attempts 12
Time to wait (minutes)
2 min
You entered
Polling interval (seconds)
10 sec
You entered

Yes, Playwright email verification can be a reliable way to test Graph Mail flows, provided you use a dedicated test mailbox and handle the API calls correctly. This article explains how to set up a robust email verification test flow using Playwright with Microsoft Graph API, including background, code examples, common pitfalls, and answers to frequently asked questions.

Background: Why Email Verification in Playwright?

Playwright is a browser automation library that excels at testing web applications. However, many applications require email verification—for example, when a user signs up or resets a password. Testing this flow end-to-end means you need to read the email that your application sends. Microsoft Graph API provides a programmatic way to access emails in a Microsoft 365 or Outlook.com mailbox, making it possible to automate the entire verification process.

Using a real mailbox (rather than mocking the email service) gives you higher confidence that the email is actually sent, formatted correctly, and contains the right verification link. This is especially important for flows where the email content is dynamic or depends on user data.

How the Graph Mail Test Flow Works

The flow involves three main steps:

  1. Trigger the email – In your Playwright test, perform the action that sends the email (e.g., submit a signup form).
  2. Fetch the email via Graph API – Use the Microsoft Graph API to retrieve the latest message from the test mailbox. You’ll need to authenticate with an access token (usually via client credentials or a delegated token).
  3. Extract the verification link and click it – Parse the email body to find the verification URL, then navigate to it in the browser to complete the flow.

Here’s a simplified code example (using Node.js and the @playwright/test runner):

const { test, expect } = require('@playwright/test');
const { Client } = require('@microsoft/microsoft-graph-client');
const { ClientSecretCredential } = require('@azure/identity');

test('signup with email verification', async ({ page }) => {
  // 1. Trigger the email
  await page.goto('https://example.com/signup');
  await page.fill('#email', 'testuser@yourdomain.com');
  await page.fill('#password', 'S3cureP@ss!');
  await page.click('#submit');

  // 2. Fetch the email via Graph API
  const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
  const client = Client.initWithMiddleware({ authProvider: { getAccessToken: async () => (await credential.getToken('https://graph.microsoft.com/.default')).token } });

  const messages = await client.api('/me/messages').filter("subject eq 'Verify your email'").top(1).get();
  const email = messages.value[0];
  const body = email.body.content; // this is HTML

  // 3. Extract the verification link (adjust regex to match your email)
  const linkMatch = body.match(/href="(https:\/\/example\.com\/verify\?token=[^"]+)"/);
  expect(linkMatch).toBeTruthy();
  await page.goto(linkMatch[1]);

  // Assert that verification succeeded
  await expect(page.locator('text=Email verified')).toBeVisible();
});

Choosing the Right Mailbox and Authentication

For reliable tests, you should use a dedicated test mailbox that is not used by humans. This prevents interference from other emails and avoids accidentally reading a personal message. You also need to decide between two common authentication methods:

  • Client credentials – The app authenticates as itself (no user context). This works if the app has the Mail.Read application permission and admin consent. It’s ideal for CI/CD because it doesn’t require user interaction.
  • Delegated permissions – The app acts on behalf of a signed-in user. This is more complex in automated tests because you need to obtain a token for a real user, but it can be useful if you want to test user-specific mailboxes.

For most test automation, client credentials are the simplest and most secure choice. Ensure your test mailbox is a cloud-only account (or a shared mailbox) that you can access with the same credentials.

Common Problems and How to Avoid Them

Even with a solid setup, you may encounter issues. Here are the most frequent pitfalls and their solutions:

  • Email not arriving in time – Email delivery can take a few seconds. Use Playwright’s expect.poll to wait for the email to appear in the mailbox, with a timeout that matches your application’s expected delivery time (e.g., 30 seconds).
  • Multiple emails in the mailbox – If your test sends multiple emails (e.g., signup + welcome), you need to filter by subject or recipient. Always filter by a unique subject line that your app uses for the verification email.
  • HTML vs. plain text – The email body may be HTML, but sometimes it’s plain text. Use the body.contentType property to decide how to parse. For HTML, you can use a regex or a DOM parser to find the link.
  • Rate limiting – Graph API has throttling limits. If you run many tests in parallel, you might hit those limits. Implement exponential backoff or increase polling intervals.
  • Token expiration – Access tokens expire after a short time (usually 1 hour). In long test suites, you may need to refresh the token or acquire a new one before each test.

Comparing Graph API with Other Email Testing Approaches

There are several ways to handle email verification in tests:

r>

Method Pros Cons
Microsoft Graph API Works with real Office 365/Outlook mailboxes; no extra services; supports HTML and attachments. Requires Azure app registration and permissions; can be slower than mock services.
Email testing services (e.g., Mailosaur, Mailtrap) Fast, simple API; often include built-in wait/polling; no Azure setup. Third-party dependency; may cost money; emails are not truly sent to a real inbox.
Mocking the email service Fastest; no external dependency. Does not test actual email sending; may miss content errors.

Graph API is the best choice if your organization already uses Microsoft 365 and you want to test the entire email pipeline without introducing a third-party service. It also allows you to test features like attachments or inline images that some mock services don’t support.

Costs and Limitations

Using Graph API to read emails is free as long as you stay within Microsoft’s API limits. However, you need an Azure Active Directory tenant, which is free for basic use. The main costs are development time and the need to maintain an app registration. There is also a limitation: Graph API can only access mailboxes that are in the same tenant as your app (unless you have cross-tenant permissions, which are complex). For personal Outlook.com accounts, you must use a different authentication flow (delegated with a personal Microsoft account).

Another limitation is that Graph API does not allow you to send emails directly (you’d need to use the sendMail action), but for testing you only need to read, which is fine.

Security and Best Practices

When automating email verification, follow these security practices:

  • Never hard-code secrets (client secret, tenant ID) in your test code. Use environment variables or a secret manager.
  • Use a dedicated test mailbox with a strong password and enable multi-factor authentication only if you can automate it (usually not).
  • Limit the permissions of your Azure app to only Mail.Read (not Mail.ReadWrite) to reduce risk.
  • Rotate client secrets regularly and avoid using the same secret across environments.
  • If you run tests in a CI pipeline, ensure that the test mailbox is not used for any other purpose and that emails are cleaned up regularly to avoid clutter.

Frequently Asked Questions

Can I use Graph API to test emails sent to Gmail or other providers?

No, Graph API only works with Microsoft 365 or Outlook.com mailboxes. For Gmail, you would need to use Gmail API or a third-party email testing service.

How long should I wait for the email to arrive?

It depends on your email service. Typically, emails arrive within 5-10 seconds, but to avoid flakiness, set a timeout of 30-60 seconds. Use Playwright’s expect.poll to wait for the email to appear.

What if the email contains a link that is not in the body but in an attachment?

You can download the attachment via Graph API and parse it. Use the message.attachments endpoint to get the attachment content. This is more complex but doable.

Do I need to set up an Azure app registration for this to work?

Yes, you need to register an application in Azure AD and grant it the Mail.Read permission. This is a one-time setup and takes about 10 minutes.

Can I run tests in parallel with the same mailbox?

Yes, but you must ensure that each test uses a unique recipient (e.g., testuser+timestamp@domain.com) or filter by a unique subject. Otherwise, tests may pick up the wrong email.

Is it possible to test email verification without a real mailbox?

Yes, you can mock the email service at the API level (e.g., using Playwright’s route interception) or use a service like Mailtrap. However, this will not test the actual email sending process.

Related

Formula used
in t | Time to wait (minutes) | min | 2 | 0
in p | Polling interval (seconds) | sec | 10 | 1
out attempts | Max attempts | - | ceil(t * 60 / p) | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.