Email verification in Playwright
Your app’s sign-up sends a confirmation link. This test signs up with a fresh address, waits for the email, checks that the link goes to your app and opens it. Each run gets its own inbox and deletes it at the end, so an email from an earlier run never passes for this one.
1. A key for CI
Section titled “1. A key for CI”In the dashboard, open API keys and create a key for one project with only these permissions: inbox_create,
inbox_delete and message_read. The key can then make and delete inboxes in that project and read their mail,
nothing else (API keys, OAuth and permissions). Put it in your CI’s
secrets as YOAGENT_KEY.
2. The test
Section titled “2. The test”Change /signup, the form’s selectors, the email’s subject and the confirmation text to your app’s.
import { expect, test } from "@playwright/test";
const yo = async (path: string, init?: RequestInit) => { const response = await fetch(`https://api.yoagent.dev/v0${path}`, { ...init, headers: { Authorization: `Bearer ${process.env.YOAGENT_KEY}`, "Content-Type": "application/json", }, }); if (!response.ok) throw new Error(`${path}: ${response.status}`); return response.status === 204 ? null : response.json();};
test("sign-up sends a working link", async ({ page, baseURL }) => { test.setTimeout(90_000); // the wait below takes up to 60 s const inbox = await yo("/inboxes", { method: "POST", body: "{}" }); try { await page.goto("/signup"); await page.fill("#email", inbox.address); await page.click("text=Create account");
const query = new URLSearchParams({ to: inbox.address, after: inbox.created_at, subject: "Confirm your email", // part of your email's subject timeout: "60", }); const { message } = await yo(`/messages/wait?${query}`); expect(message, "no email within 60 s").toBeTruthy(); const app = new URL(baseURL!).origin; const verify = message.links.find( (link) => link.kind === "verify" && new URL(link.url).origin === app, ); expect(verify, "no verify link to the app in the email").toBeTruthy(); await page.goto(verify.url); await expect(page.getByText("Email confirmed")).toBeVisible(); expect(new URL(page.url()).origin, "confirmed off the app").toBe(app); } finally { await yo(`/inboxes/${inbox.inbox_id}`, { method: "DELETE" }).catch( console.warn, ); }});What each part does:
- An inbox per run.
POST /v0/inboxeswith an empty body makes an inbox with a random address in the key’s project. All its mail belongs to this run. - The wait.
GET /v0/messages/waitanswers as soon as a message matching the query arrives, or withmessage: nullwhentimeoutruns out (at most 2 minutes).afteris the inbox’s creation time by the API’s clock, so a slow clock on the CI machine changes nothing;subjectmatches part of the subject, so a welcome email that arrives first does not count. More in Receive and read email. - The link. Every message comes with the links found in it, each with a
kind(verifyfor confirmation links). The test takes the one that leads to your app, opens it, and checks the page it lands on is still your app.kindis guessed from the link’s words and address (verify, confirm, activate, sign in and the like); if your email’s link has none of them, find it by its URL instead. - Clean up.
finallydeletes the inbox even when the test fails; a failed delete only logs a warning, so it never hides the test’s own error.
The app’s address comes from the config:
import { defineConfig } from "@playwright/test";
export default defineConfig({ use: { baseURL: process.env.APP_URL ?? "http://localhost:3000" },});3. Run it
Section titled “3. Run it”APP_URL=http://localhost:3000 npx playwright test signupAPP_URL is where your app runs; YOAGENT_KEY comes from your CI’s secrets (locally, export it first). When no email
comes within 60 seconds, the test fails with “no email within 60 s”.