Playwright Email Verification: Test Sign-up Codes and Magic Links

Published

To test email verification in Playwright, give every test its own temporary inbox through a fixture, sign up with that address in the browser, then read the code or the link from the email in the same test. The tempmailo-client package for Node.js covers the inbox side with three calls: create, waitForMessage and delete. Below are the config, the fixtures, two tests you can copy (a sign-up code and a magic link) and the CI setup.

The addresses are ordinary temp mail addresses of TempMailo, made through the API instead of the website. They receive mail and can't send it, which is all a sign-up test needs.

What you need

  • Node.js 22 or newer and @playwright/test. The client has no dependencies and uses the fetch built into Node.
  • A free TempMailo account and an API key. Sign up, confirm the email, then create a key on the API page of your account.
  • An app that sends its emails to any address a test gives it: a staging server, a preview deploy or a local build with real mail sending.

The free limits are on the API page: 100 new addresses a day, 60 requests a minute, and each address lives 10 or 60 minutes. With one fresh address per test, that is up to 100 email tests a day.

Install the package and set the key

npm install --save-dev @playwright/test tempmailo-client
npx playwright install chromium
export TEMPMAILO_API_KEY=tmk_your_key_here

The client reads TEMPMAILO_API_KEY by itself. Keep the key in an environment variable or a CI secret, never in the repository. The calls run in the test code on Node, not inside the page you test: the key must never reach the browser, and the API sends no CORS headers, so a web page can't call it anyway.

Config: give the email time to arrive

import { defineConfig } from '@playwright/test';

export default defineConfig({
  testDir: './e2e',
  timeout: 120_000, // a test may wait up to 90 seconds for an email
  fullyParallel: true, // tests of one file run in parallel too: each has its own inbox
  workers: process.env.CI ? 4 : undefined,
  use: { baseURL: process.env.APP_URL ?? 'https://staging.your-app.example' },
});

Playwright gives each test 30 seconds by default, and that time includes creating the inbox in the fixture. A test that waits for an email needs more, so 120 seconds leaves 90 for the email and the rest for the browser steps. By default Playwright runs the tests of one file one after another; fullyParallel: true lets them run at the same time, which is safe here because every test has its own inbox. APP_URL points the tests at your staging server, so page.goto('/signup') opens the right app.

Fixtures: a new inbox for every test

Put this in e2e/fixtures.js. The tests import test and expect from this file instead of @playwright/test.

import { test as base, expect } from '@playwright/test';
import { TempMailo } from 'tempmailo-client';

export const test = base.extend({
  // one client per worker; it reads TEMPMAILO_API_KEY
  mail: [async ({}, use) => use(new TempMailo()), { scope: 'worker' }],

  // a fresh address for every test, deleted afterwards
  inbox: async ({ mail }, use) => {
    const box = await mail.create({ lifetime: 600, client: 'playwright-e2e 1.0' });
    await use(box);
    await mail.delete(box.token);
  },
});

export { expect };

mail is a worker fixture: one client per worker process. inbox is a test fixture: every test that asks for it gets a new address, and the line after use deletes the address when the test ends. Playwright runs that teardown after a failed test too, so failed runs don't leave addresses behind.

Test 1: sign up and enter the code from the email

import { test, expect } from './fixtures.js';

test.skip(!process.env.TEMPMAILO_API_KEY, 'set TEMPMAILO_API_KEY to run the email tests');

test('sign-up: the emailed code confirms the account', async ({ page, mail, inbox }) => {
  await page.goto('/signup');
  await page.getByLabel('Email').fill(inbox.email);
  await page.getByLabel('Password').fill('a-long-test-password');
  await page.getByRole('button', { name: 'Sign up' }).click();

  const letter = await mail.waitForMessage(inbox.token, {
    timeout: 90_000,
    match: (m) => /code/i.test(m.subject),
  });
  expect(letter.from).toBe('no-reply@your-app.example');

  const code = (letter.plain || letter.html).match(/\b\d{6}\b/)?.[0];
  expect(code, 'a 6-digit code in the email').toBeTruthy();

  await page.getByLabel('Code').fill(code);
  await page.getByRole('button', { name: 'Confirm' }).click();
  await expect(page.getByText('Welcome')).toBeVisible();
});

The test.skip line at the top skips the email tests when no key is set, so anyone without a key can still run the rest of the suite. Swap the labels and button names for the ones in your sign-up form.

waitForMessage asks for new mail every 5 seconds, skips messages that don't pass match and returns the whole message: subject, sender in from, plain text, HTML and attachments. The pattern \b\d{6}\b finds a six-digit code. If your codes look like "418 507" or contain letters, change the pattern to match. The letter.plain || letter.html part covers emails that have no plain-text version, and the from check catches a staging server that still sends from a default address.

Test 2: open the magic link from the email

test('magic link: the emailed link signs the user in', async ({ page, mail, inbox }) => {
  await page.goto('/login');
  await page.getByLabel('Email').fill(inbox.email);
  await page.getByRole('button', { name: 'Email me a link' }).click();

  const letter = await mail.waitForMessage(inbox.token, { timeout: 90_000 });
  const href = letter.html.match(/href="([^"]*\/login\/verify[^"]*)"/)?.[1];
  expect(href, 'a sign-in link in the email').toBeTruthy();

  await page.goto(href.replaceAll('&', '&'));
  await expect(page.getByText('Welcome back')).toBeVisible();
});

In HTML, the & between link parameters is written as &, so the test turns it back before page.goto. A button in an email is still a link with an href, so the same pattern finds it. To wait for a second email in the same test, such as a password reset, pass after: letter.id to the next waitForMessage, and it skips the email you already have.

Run the email tests in CI

Add the key as a repository secret named TEMPMAILO_API_KEY, then run Playwright as usual. A GitHub Actions example:

# .github/workflows/e2e.yml
name: e2e
on: [push, pull_request]

jobs:
  playwright:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: 24
      - run: npm ci
      - run: npx playwright install --with-deps chromium
      - run: npx playwright test
        env:
          TEMPMAILO_API_KEY: ${{ secrets.TEMPMAILO_API_KEY }}
          APP_URL: https://staging.your-app.example

With workers: 4 and fullyParallel: true from the config, CI runs up to four tests at the same time, and each one has its own inbox, so they never read each other's mail. A test that waits for mail makes about 12 requests a minute (one every 5 seconds), so up to four tests can wait at the same time and stay within the limit of 60 requests a minute. When a request hits that limit and the API asks to wait 60 seconds or less, the client waits and retries by itself.

Why a test fails, and what to check

  • TempMailoError with code "timeout". No matching email arrived within 90 seconds. Look at your app's mail log, check that match fits the real subject, and make sure the form sent the email to inbox.email and not to a fixed test address.
  • Test timeout of 120000ms exceeded. The browser steps and the wait together took longer than the config allows. Raise timeout in the config or lower the wait.
  • RateLimitError. A limit is used up. error.scope says which one, error.retryAfter how many seconds to wait.
  • ExpiredError. The address has lived its 10 or 60 minutes. Create inboxes per test with the fixture, not once for the whole run.
  • AuthError. The key is wrong or was deleted in your account.

When a real mailbox is the better choice

TempMailo addresses only receive. A test that replies to an email, checks whether a message lands in spam at Gmail or Outlook, or keeps an account alive for weeks needs a mailbox you own. The same goes for a shared QA account whose address has to stay valid after the test. To check a sign-up email by hand, without any code, see how to test sign-up emails.

FAQ

Does it work with TypeScript?

Yes. The package ships its own type definitions, so import { TempMailo } from 'tempmailo-client' works in .spec.ts files with no extra packages.

Can I use it with Cypress?

Yes, through cy.task. Cypress runs test code in the browser, where the key would be visible, so create the inbox and wait for mail in tasks defined in setupNodeEvents, then call those tasks from the test.

How do I check the email headers?

await mail.source(inbox.token, letter.id) returns the raw message as text, with headers such as DKIM-Signature and List-Unsubscribe.

Can tests get mail without polling?

Yes, with a webhook: TempMailo sends every new email to your HTTPS endpoint as a signed request, and verifyWebhook checks the signature. For most test suites polling is simpler, since a test runner usually has no public URL.

Is there a Python version?

Yes, tempmailo on PyPI, with the same methods. The pytest guide shows it with fixtures and four tests.

Sources

Read next

All articles of the blog

We use cookies to keep your inbox open in this browser, to show ads and to count visits. Cookie policy