Email verification in Playwright tests
Sign-up flows that send a one-time code or a magic link are awkward to test: the email lands somewhere your test cannot read. This guide shows a pattern that works in Playwright Test (Node, plain JavaScript): every test gets its own address, waits for the email through one HTTP call, and then either types the code or follows the link. The same approach works for password reset and email change flows.
How it works
AssertInbox receives mail for any address shaped like <namespace>.<tag>@assertinbox.com. Your key owns the namespace, and the tag is whatever you choose, so a unique tag per test is a private inbox. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON, including otp (the code) and magic_link (the verification link), both already extracted. Messages are deleted after 24 hours.
Get an API key
Get one in a click at /start, or with the request below. One request creates a key and your private namespace. No email address or card is needed. The key is shown once, so store it in your CI secrets.
curl -s -X POST https://assertinbox.com/v1/keys
# {"api_key":"ai_...","namespace":"k3x9a1bz","address_template":"k3x9a1bz.<tag>@assertinbox.com"}
Export both values for the tests: ASSERTINBOX_KEY is the api_key and ASSERTINBOX_NAMESPACE is the namespace. If the call returns 403 signups_closed, the beta is not open for new keys. Use inboxes only for services you own or control; see the terms.
The helper and two tests
Save this as tests/email-verification.spec.js. It needs Node 18 or newer for the built-in fetch. Replace the URLs and selectors with those of your app.
const { test, expect } = require("@playwright/test");
const API = "https://assertinbox.com";
const KEY = process.env.ASSERTINBOX_KEY;
const NAMESPACE = process.env.ASSERTINBOX_NAMESPACE;
const APP = process.env.APP_URL || "http://localhost:3000";
function newAddress(testInfo) {
const random = Math.random().toString(36).slice(2, 8);
const tag = ("pw-" + testInfo.workerIndex + "-" + Date.now() + "-" + random).toLowerCase();
return { tag, email: NAMESPACE + "." + tag + "@assertinbox.com" };
}
async function waitForEmail(tag) {
for (let attempt = 0; attempt < 3; attempt++) {
const res = await fetch(API + "/v1/wait?tag=" + encodeURIComponent(tag) + "&timeout=25", {
headers: { Authorization: "Bearer " + KEY },
});
if (res.status === 200) return res.json();
if (res.status !== 408) throw new Error("AssertInbox returned HTTP " + res.status);
}
throw new Error("No email arrived for tag " + tag);
}
async function deleteEmails(tag) {
await fetch(API + "/v1/messages?tag=" + encodeURIComponent(tag), {
method: "DELETE",
headers: { Authorization: "Bearer " + KEY },
});
}
test.describe("email verification", () => {
test.setTimeout(120000);
let inbox;
test.beforeEach(async ({}, testInfo) => {
inbox = newAddress(testInfo);
});
test.afterEach(async () => {
await deleteEmails(inbox.tag);
});
test("verifies the account by magic link", async ({ page }) => {
await page.goto(APP + "/signup");
await page.fill("input[name=email]", inbox.email);
await page.click("button[type=submit]");
const mail = await waitForEmail(inbox.tag);
expect(mail.magic_link).toBeTruthy();
await page.goto(mail.magic_link);
await expect(page.getByText("Email verified")).toBeVisible();
});
test("verifies the account by one-time code", async ({ page }) => {
await page.goto(APP + "/signup");
await page.fill("input[name=email]", inbox.email);
await page.click("button[type=submit]");
const mail = await waitForEmail(inbox.tag);
expect(mail.otp).toMatch(/^[0-9]{4,8}$/);
await page.fill("input[name=code]", mail.otp);
await page.click("button[type=submit]");
await expect(page.getByText("Email verified")).toBeVisible();
});
});
Run it with ASSERTINBOX_KEY=... ASSERTINBOX_NAMESPACE=... npx playwright test.
Why a unique tag per test
If two tests used the same address, one could read the other's email. The tag above combines the worker index, a timestamp and a random suffix, so retries and parallel workers never collide. Tags are case-insensitive, may contain letters, digits, dots, dashes and underscores, and are at most 64 characters long.
Troubleshooting
The wait times out
A 408 timeout means no message arrived within the wait. First check that your app really sent the email and that it used the full address, including the namespace. Many apps send from a queue, so allow the three 25-second attempts in the helper before failing. If your staging environment has an allow-list of recipient domains, add assertinbox.com.
The wrong code is returned
The extractor picks the first number near a label such as code or verification. If your email also contains other numbers in the same sentence, read mail.text and parse it yourself. The full message is always in the response. The docs list the extraction rules.
Parallel workers
Each worker process runs the helper independently, so nothing needs to be shared. The only shared resource is your key's daily budget: 2,000 API requests per UTC day, and 200 received messages per namespace per day. A wait that times out three times costs three requests. GET /v1/me shows what you have used.
Tests leave mail behind
The afterEach hook deletes the tag's messages. Even if a run is killed, everything is removed after 24 hours, so there is no cleanup job to maintain.
Using the code or the link
Prefer magic_link when your product offers a link and otp when it offers a code. When the field is null, the full list of links is in links, and the raw body is in text and html. Never log the key in CI output.
Client files instead of a helper
If you would rather not keep your own helper, the client files for JavaScript and Python do the same job: they build addresses, wait with retries on 408, and accept the from, subject and after filters. They are single files you download with curl. The daily allowance, the retention period and every error code are listed on the limits page.
For waits that must ignore an earlier message, such as a resend button, use the after parameter described in the docs: pass the id of the email you already read.
Related guides: Cypress OTP login, pytest with Playwright for Python and email for AI coding agents.