How to test email verification and magic links in Playwright
By qtrl Team · Engineering
Most end-to-end suites stop at the same spot. The test fills in the sign-up form, clicks "Create account", sees "Check your inbox", and then nothing. The code is sitting in an email the test can't read. So the team seeds a verified user in a fixture, skips the step, and the one flow every new customer goes through never gets tested end to end.
There are five common ways to close that gap. They differ less in tooling than in where the email stops. Some approaches never send it, some catch it before it leaves your infrastructure, and some let it go all the way through your real provider into a real inbox. Below is how they compare, and then a full Playwright walkthrough of the approach we'd pick for most teams.
The five approaches
1. Skip the email and read the token directly
The fastest option. Your test asks the backend for the token instead of waiting for an email. That can be a query against the tokens table, a test-only endpoint, or an admin API. If you use Supabase Auth, the admin client can mint the same link the email would contain:
const { data } = await supabaseAdmin.auth.admin.generateLink({
type: 'magiclink',
email: 'new-user@example.com',
})
await page.goto(data.properties.action_link)If your app uses server-side PKCE auth, pass data.properties.hashed_token to your confirm route and call verifyOtp there instead. Either way, it's fast and free, and it's a fine choice for tests where login is just setup. But it tests your token logic, not your email. A broken template, a link pointing at the wrong domain, or a provider that silently stopped sending all pass. And a test-only endpoint is one more thing you have to keep out of production.
2. Catch mail locally with Mailpit
Mailpit is a free, MIT-licensed SMTP server with a web UI and a REST API. You point your app's SMTP settings at it (port 1025 by default, with the API on 8025, or 54324 under the Supabase CLI), and every message lands in one catch-all mailbox you can search. It replaced MailHog for most teams after MailHog's maintainer stepped back, and the Supabase CLI now uses it to capture Auth emails in local development.
const res = await fetch(
'http://localhost:8025/api/v1/search?query=' +
encodeURIComponent('to:"new-user@example.com"'),
)
const { messages } = await res.json()
const msg = await (await fetch(
'http://localhost:8025/api/v1/message/' + messages[0].ID,
)).json()
const code = msg.Text.match(/\b\d{6}\b/)?.[0]For local development and CI jobs that spin up the whole stack, Mailpit is hard to beat. The limits come from its position on the path. Your real email provider never sees the message, so a misconfigured sender domain or a template stored at the provider goes untested. There's no public address, so it can't help against a deployed staging environment. And you write the polling and the code extraction yourself.
3. Route staging mail to a hosted sandbox
Mailtrap's Email Sandbox is the hosted version of the same idea. You swap your staging SMTP credentials for the sandbox's, and everything staging sends lands in a shared web inbox with HTML checks and spam analysis. That's great for designers and QA reviewing how emails render, and for making sure staging never mails a real customer. For automated tests it has the same blind spot as Mailpit, since it stands in for your provider. The free plan is also small (50 emails a month and one email per 10 seconds per sandbox on the pricing page today), which a parallel sign-up suite runs through quickly.
4. Use a real Gmail inbox with plus addressing
Gmail delivers qa+anything@gmail.com to qa@gmail.com, so one account gives you endless addresses, and the email goes through your real provider and real delivery. You then read it with the Gmail API or IMAP.
It works, and plenty of suites have run on it for years. Where it hurts:
- You need OAuth credentials, since Google phased out password-only access for IMAP.
- Every test reads from the same mailbox, so parallel runs have to filter carefully or they grab each other's codes.
- Some sign-up forms reject the plus sign.
- Gmail's spam filtering becomes part of the system under test, whether you want it there or not.
5. Use an inbox API with an address per test
Services like Mailosaur, MailSlurp, testmail.app and receiver.email give you real, publicly routable addresses and an API that waits for a message and hands it back. Your app sends through its real provider, the message is really delivered, and each test uses its own address, so parallel runs never collide. Most of them pull the code and links out of the message for you.
The trade-offs are cost at volume and the fact that test mail sits with a third party for a while. For a suite that tests sign-up, login and password reset against a deployed environment, it's the approach that tests the most of the real path with the least code.
Comparing the inbox APIs
The four services overlap a lot, and the right pick depends on what else you need. Prices below are from each vendor's pricing page in November 2026 and change often, so check before you buy.
| Free option | Entry paid plan | Addresses | Code and link extraction | |
|---|---|---|---|---|
| Mailosaur | 14-day trial | From $20/month, billed annually | Any address on your server's domain | Built in |
| MailSlurp | 250 emails/month | $19.99/month, 2,500 emails | Created through the API | Links built in, codes by pattern match |
| testmail.app | 100 emails/month | $12/month ($9 billed annually), 10,000 emails | Any tag on your namespace | Your own regex |
| receiver.email | 500 emails/month | $19/month, 10,000 emails | Any address under your project | Built in |
Mailosaur is the broadest. It also tests SMS, renders email previews across clients and has SDKs in seven languages, which matters if your QA team works in Java or .NET. MailSlurp can also send mail, and higher plans add custom domains and phone numbers, so it fits when the test needs to reply to your app, not just receive. testmail.app is the cheapest per email and has a GraphQL API.
receiver.email is the narrowest of the four: it only receives, and there are no previews or SMS. In exchange, code and link extraction are built in and the free tier is 500 emails a month. It's what we use in the walkthrough below. The pattern carries over to the others with different method names. With testmail.app you'll also write the code extraction yourself.
Walkthrough: sign-up, magic link and password reset in Playwright
Setup
Install the SDK next to Playwright and put the API key in an environment variable. Locally that's your shell or a .env file. In CI it's a secret.
npm install --save-dev @receiverdotemail/sdk
export RECEIVER_API_KEY="recv_live_..."Next, a fixture file. One client per worker, and a brand new address for every test. Creating an address is just picking a name, nothing gets set up on a mail server, and it comes with a createdAt timestamp you'll wait from.
// tests/fixtures.ts
import { test as base, expect } from '@playwright/test'
import { ReceiverClient, type Inbox } from '@receiverdotemail/sdk'
export const test = base.extend<{ inbox: Inbox }, { receiver: ReceiverClient }>({
receiver: [
async ({}, use) => {
await use(new ReceiverClient()) // reads RECEIVER_API_KEY
},
{ scope: 'worker' },
],
inbox: async ({ receiver }, use) => {
await use(await receiver.inboxes.create({ prefix: 'pw' }))
},
})
export { expect }Waiting for an email takes longer than Playwright's default 30 second test timeout allows for comfortably, so raise it. You want the email wait to fail first, with a message that says no email arrived, rather than a generic test timeout.
// playwright.config.ts
export default defineConfig({
timeout: 90_000,
use: { baseURL: process.env.BASE_URL },
})Sign-up with a verification code
import { test, expect } from './fixtures'
test('new user can verify their email with a code', async ({ page, inbox, receiver }) => {
await page.goto('/signup')
await page.getByLabel('Email').fill(inbox.address)
await page.getByRole('button', { name: 'Create account' }).click()
const otp = await receiver.emails.waitForOtp({
recipient: inbox.address,
since: inbox.createdAt,
codeLength: 6,
timeout: 60,
})
await page.getByLabel('Verification code').fill(otp.code)
await page.getByRole('button', { name: 'Verify' }).click()
await expect(page).toHaveURL(/\/onboarding/)
})Two details save you a debugging session later. since makes sure the test only accepts mail that arrived after it started, so a stale email from an earlier retry can't pass the test. codeLength keeps the extractor from grabbing a year or an order number in the footer. If your codes aren't six digits, set it explicitly.
Magic link login
For links, wait for the whole message and pick the link by path. Emails are full of links (logo, unsubscribe, help center), and links[0] is rarely the one you want.
test('magic link signs the user in', async ({ page, inbox, receiver }) => {
await page.goto('/login')
await page.getByLabel('Email').fill(inbox.address)
await page.getByRole('button', { name: 'Email me a link' }).click()
const email = await receiver.emails.waitFor({
recipient: inbox.address,
since: inbox.createdAt,
subject: 'sign in',
timeout: 60,
})
const link = email.links.find((l) => new URL(l).pathname.startsWith('/auth/callback'))
expect(link, 'magic link in the email').toBeDefined()
await page.goto(link!)
await expect(page.getByRole('button', { name: 'Account' })).toBeVisible()
})Open the link in the same browser context that requested it. If your auth flow uses PKCE (Supabase's server-side auth does by default), the code verifier sits in the browser that started the login, so a link opened anywhere else fails by design.
Password reset, including the resend button
Resend flows are where shared-inbox setups usually go wrong, because the test has to tell the first email from the second. Waiting from the first email's receivedAt handles it:
test('resent reset link works and the first one does not', async ({ page, inbox, receiver }) => {
// Precondition: create an account for inbox.address via your API before this step
await page.goto('/forgot-password')
await page.getByLabel('Email').fill(inbox.address)
await page.getByRole('button', { name: 'Send reset link' }).click()
const first = await receiver.emails.waitFor({
recipient: inbox.address,
since: inbox.createdAt,
timeout: 60,
})
await page.getByRole('button', { name: 'Resend' }).click()
const second = await receiver.emails.waitFor({
recipient: inbox.address,
since: first.receivedAt,
timeout: 60,
})
const resetLink = (e: typeof first) =>
e.links.find((l) => new URL(l).pathname.startsWith('/reset-password'))!
await page.goto(resetLink(first))
await expect(page.getByText('This link has expired')).toBeVisible()
await page.goto(resetLink(second))
await page.getByLabel('New password').fill('a-long-test-password-1')
await page.getByRole('button', { name: 'Save' }).click()
await expect(page.getByText('Password updated')).toBeVisible()
})Whether an older link should stop working is a product decision, so check what your spec says before you assert on it. Either way, testing both links is the point: invalidating old tokens is the kind of rule that breaks quietly in a refactor.
Running it in CI
Add RECEIVER_API_KEY as a repository secret and pass it to the test step. Because every test has its own address, fullyParallel and sharding work without changes. Use a separate project (and key) per environment, so a staging run and a production smoke test never read each other's mail. If you already keep test data isolated per run, this is the same idea applied to inboxes.
When the tester is an AI agent
Everything above assumes a script that knows where the code goes. AI agents that test your app, or that sign up for services on someone's behalf, hit the same wall from a different side: they can fill in the form, but they have no inbox to check.
MailSlurp, testmail.app, Mailtrap and receiver.email all offer MCP servers now, which give an agent tools to read mail. With receiver.email, the tool from the walkthrough, the agent gets a create_inbox tool and a wait_for_email tool that can also pull out the code. Adding it to Claude Code is one command:
claude mcp add receiver \
-e RECEIVER_API_KEY=recv_live_... \
-- npx -y @receiverdotemail/mcpA nice side effect: an agent with a real inbox tests the flow the way a user experiences it. If the email takes a while, it waits. If it never comes, the agent reports that the sign-up is stuck, which is the bug report you want. Keep the same rule as for scripts, though: one project per agent or environment, and only automate sign-ups on systems you own or that allow it.
So which one should you use?
Most teams end up with two. Skip the email wherever login is only setup, and keep an inbox API for the handful of tests that cover sign-up, login, password reset and email change against a deployed environment. Those flows are where a broken email costs you real users, and they're the only tests that prove the whole path works. Gmail with plus addressing still works if you're already set up on it. We just wouldn't start a new suite on it. And whichever approach you use, keep the email tests few and focused. The locator advice and flakiness fixes for the rest of your suite apply here too.
In qtrl you can keep these flows as plain-language test cases next to the rest of your suite, with test accounts, URLs and credentials in per-environment variables and secrets instead of the case itself. An AI agent runs the steps in a real browser and keeps a screenshot of each one. It doesn't read inboxes today, so when a case reaches the email step it reports the run as blocked rather than guessing, and the Playwright tests above cover that part. Put your sign-up flow under test.
Have more questions about AI testing and QA? Check out our FAQ