YoAgent

Recipe

Email verification in a Playwright test

Your sign-up sends a confirmation link. This test signs up with a fresh address, waits for the email, checks the link goes to your app and opens it. Each run gets its own inbox and deletes it at the end.

1. A key for CI

In the dashboard, create an API key for one project with these permissions:inbox_create, inbox_delete, message_read. Put it in your CI's secrets as YOAGENT_KEY.

2. The test

Change /signup, the form's selectors, the email's subject and the confirmation text to your app's. The inbox is new on every run, so an older email never passes for this one.

tests/signup.spec.ts

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);
  }
});

The app's address comes from the config:

playwright.config.ts

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

export default defineConfig({
  use: { baseURL: process.env.APP_URL ?? "http://localhost:3000" },
});

3. Run it

Command

APP_URL=http://localhost:3000 npx playwright test signup

APP_URL is where your app runs;YOAGENT_KEY comes from your CI's secrets (locally, export it first). The wait takes up to 60 seconds; when no email comes, the test fails with "no email within 60 s".