rc.center Learnrc.center Learnrc.center

How to test e-mail verification in Playwright with a real inbox

Published on 7 min readOTPBox

Write a Playwright test that signs up with a fresh e-mail address, waits for the real verification mail and reads the code or link, using the OTPBox REST API.

Envelope outline with a check mark in violet light on a dark background, for a verified e-mail test

Most sign-up tests stop at the “check your inbox” screen. The form is submitted, the page says a code was sent, and the test passes. The part users actually depend on, the e-mail arriving with a code that works, is never exercised.

This guide shows a Playwright test that goes all the way: it creates a new address for each run, signs up with it, waits for the real message to arrive over SMTP, reads the one-time code (or the verification link) and finishes the flow. The inbox comes from OTPBox, which gives each test its own receive-only address and a REST endpoint that waits for the next message.

Why mocking the mail step leaves a gap

A local mail catcher or a stubbed mailer is fast and useful in unit tests. It does not tell you whether:

  • your production mail provider accepts and delivers the message,
  • the template still contains the code after the last redesign,
  • the link in the mail points at the right host for that environment,
  • the message is signed so that receivers trust it (SPF, DKIM, DMARC).

A test against a real inbox catches all four, because the mail has to cross the internet and land in a mailbox your test can read.

What you need

  • A Playwright project (the examples use the Node test runner, @playwright/test).
  • An rc.center account on the Pro plan, which includes OTPBox. The free rc.center plan can sign in but cannot create inboxes.
  • An environment of your app (staging, preview or production) that sends real e-mail through your normal provider.

Step 1: create an API key

API keys are created only in the OTPBox web app, by you. Sign in at https://otpbox.rc.center with your rc.center account, open the Inboxes page, find API keys and create one named after the pipeline that will use it, for example ci-playwright. The key is shown once.

Store it as a secret in your CI system and expose it to the test process as OTPBOX_KEY. Never commit it.

Check it works:

curl -s https://otpbox.rc.center/v1/usage -H "Authorization: Bearer $OTPBOX_KEY"

The answer lists your plan quotas: active inboxes, messages this month, messages in the last minute, storage, maximum message size and retention days.

Step 2: a small OTPBox helper for your tests

Put this in tests/support/otpbox.js. It uses the fetch built into Node 18 and later.

// tests/support/otpbox.js
// Minimal OTPBox REST client for end-to-end tests.
const BASE_URL = 'https://otpbox.rc.center/v1';

const authHeaders = () => ({
  Authorization: `Bearer ${process.env.OTPBOX_KEY}`,
  'Content-Type': 'application/json',
});

const request = async ({ method = 'GET', path, body }) => {
  const res = await fetch(`${BASE_URL}${path}`, {
    method,
    headers: authHeaders(),
    body: body ? JSON.stringify(body) : undefined,
  });
  const data = await res.json();
  if (!res.ok) {
    // Errors are JSON: {"error": "<code>", "message": "<text>"}
    throw new Error(`OTPBox ${method} ${path}: ${res.status} ${data.error} ${data.message}`);
  }
  return data;
};

// Creates a fresh inbox. prefix: 1-24 lowercase letters, digits, inner hyphens.
export const createInbox = ({ prefix }) =>
  request({ method: 'POST', path: '/inboxes', body: { prefix } });

// Long-polls for the newest matching message (timeout in seconds, max 120).
export const waitForMessage = ({ inboxId, timeout = 60, ...filters }) => {
  const query = new URLSearchParams({ timeout: String(timeout), ...filters });
  return request({ path: `/inboxes/${inboxId}/wait?${query}` });
};

export const deleteInbox = ({ inboxId }) =>
  request({ method: 'DELETE', path: `/inboxes/${inboxId}` });

waitForMessage accepts the same filters as the REST endpoint: expect_domain, from_domain, from, subject, regex and since.

Step 3: the sign-up test

// tests/signup.spec.js
import { test, expect } from '@playwright/test';
import { createInbox, waitForMessage, deleteInbox } from './support/otpbox.js';

const APP_DOMAIN = 'example.com'; // the domain your app sends mail from

test('a new user can sign up and confirm the e-mail code', async ({ page }) => {
  test.setTimeout(120_000);

  const inbox = await createInbox({ prefix: 'signup' });

  try {
    await page.goto('https://staging.example.com/signup');
    await page.getByLabel('E-mail').fill(inbox.address);
    await page.getByLabel('Password').fill('a-long-test-password');
    await page.getByRole('button', { name: 'Create account' }).click();

    const mail = await waitForMessage({
      inboxId: inbox.id,
      timeout: 60,
      expect_domain: APP_DOMAIN,
      subject: 'verification',
    });

    expect(mail.found).toBe(true);
    expect(mail.sender_auth).toBe('pass');
    expect(mail.codes.length).toBeGreaterThan(0);

    await page.getByLabel('Verification code').fill(mail.codes[0]);
    await page.getByRole('button', { name: 'Verify' }).click();
    await expect(page.getByText('Welcome')).toBeVisible();
  } finally {
    await deleteInbox({ inboxId: inbox.id });
  }
});

A few details matter here.

You can call waitForMessage right after the click. The wait endpoint also returns matching messages that arrived in the last 10 minutes, so there is no race between submitting the form and starting to wait.

expect_domain keeps the test honest. It only accepts a message whose sender domain or verification link domain is the registrable domain you pass, so a stray message cannot satisfy the test.

codes is ordered best first. OTPBox scores every candidate by how close it sits to words like “code” or “verification”, its length and whether it stands alone, so codes[0] is the one-time code in a normal template. If a template has more than one number that looks like a code, call GET /v1/messages/{id} and inspect code_candidates, which carry the surrounding text and the score.

Asserting sender_auth turns the test into a deliverability check. It summarizes the DMARC result for the message: pass, fail, or none when the domain has no DMARC record or the check could not complete. If your staging environment sends from a domain without DMARC, expect none there and pass in production.

Deleting the inbox in finally keeps you under the active inbox limit. Deleted addresses stop receiving and are never handed out again.

Here is what the wait endpoint returns for a typical message (the full message object is shortened):

{
  "found": true,
  "sender_auth": "pass",
  "from_address": "no-reply@example.com",
  "codes": ["482913"],
  "verify_link": "https://staging.example.com/verify?token=<token>",
  "verify_link_host": "staging.example.com",
  "verify_link_suspicious": false,
  "message": {
    "id": "<message-id>",
    "subject": "Your verification code",
    "from": "Example <no-reply@example.com>",
    "received_at": "2026-10-20T18:20:53Z",
    "state": "ready"
  }
}

If nothing matching arrives within the timeout, the answer is {"found": false} with status 200, so the assertion on found fails with a clear message instead of a network error.

If your app sends a confirmation link, use verify_link. OTPBox classifies the links in each message and picks the one that looks like verification, a magic login, a reset or an activation, and reports the host it really points to.

const mail = await waitForMessage({ inboxId: inbox.id, expect_domain: APP_DOMAIN });

expect(mail.found).toBe(true);
expect(mail.verify_link_host).toBe('staging.example.com');
expect(mail.verify_link_suspicious).toBe(false);

await page.goto(mail.verify_link);
await expect(page.getByText('E-mail confirmed')).toBeVisible();

Checking verify_link_host catches a common bug: a staging build that sends links to the production host because an environment variable was not set.

Running it in CI

The API key is all the test needs: no browser login and no IMAP. In GitHub Actions, for example:

- name: Run end-to-end tests
  run: npx playwright test
  env:
    OTPBOX_KEY: ${{ secrets.OTPBOX_KEY }}

Give each test its own inbox rather than sharing one across workers. Separate inboxes mean no test can read a message meant for another.

Limits to plan around

These are the rc.center Pro limits for OTPBox. Retention is fixed by the plan.

Limit Pro
Active inboxes 20
Messages per month 1,000
Messages per minute (all inboxes) 30
Maximum message size 10 MB
Storage 1 GB
Retention 30 days
Longest single wait 120 seconds

In practice:

  • With 20 active inboxes, run at most 20 e-mail tests at the same time and delete each inbox when its test ends.
  • At 30 messages per minute across the account, a large parallel suite should be spread out or split so that only the tests that need mail use OTPBox.
  • GET /v1/usage shows current usage, so a pre-flight step in CI can fail early with a readable message when the monthly quota is close.

Troubleshooting

found is false. Check that the app really sent to inbox.address (log it), relax the subject filter, and confirm that the environment sends real mail rather than to a local catcher. The address includes a random part, so a typo in a fixture is a common cause.

The code is wrong. Inspect code_candidates with GET /v1/messages/{id}. Each candidate has its surrounding text, which shows whether another number in the template scored higher.

A 409 limit_reached on inbox creation. You hit the active inbox limit. List inboxes with GET /v1/inboxes and delete the ones left behind by aborted runs.

A 403 plan_required. The account behind the key is not on a plan that includes OTPBox.