THE REPLACEQA BLOG

Test signup email verification with Playwright and Mailpit

Test signup email verification with Playwright and a local Mailpit inbox, matching the right message and checking the resulting account state.

To test signup email verification, submit a unique test address, find the message sent to that address, follow its verification link, and check that the intended account can proceed. A confirmation page saying “check your inbox” verifies only the beginning of that journey.

This tutorial uses Playwright and a local Mailpit inbox with a disposable signup fixture. It tests application behavior through captured SMTP mail. It does not measure delivery to Gmail, spam placement or the availability of a production email provider.

Define what verification should change

The fixture has a deliberately small contract. Before verification, the account page displays “Verification required.” A signup submits an example.test address and sends a link. Opening a valid link makes that account available. An expired link is rejected, and a used link shows “Link already used.”

That reuse rule is a choice for this example. Your application might instead handle reuse with an informative success page. Specify its intended behavior and test that; do not copy a rejection assertion into a product designed differently.

The fixture keeps data in memory and uses a simplified account cookie. It is a teaching application, not an authentication implementation to adopt. Real applications must test their own session, account and permission rules.

Start an isolated inbox

Download the runnable package, extract it and install the dependencies and browser:

npm ci
npx playwright install chromium
docker run -d --rm --name replaceqa-blog-mailpit -p 127.0.0.1:4325:8025 -p 127.0.0.1:4326:1025 axllent/mailpit:v1.32.0

The example application sends SMTP mail to 127.0.0.1:4326. Mailpit's UI and API are available at http://127.0.0.1:4325. These host ports differ from Mailpit's container ports so the example is less likely to collide with a local default installation.

Mailpit provides message retrieval through its API, including searching and deleting captured messages. Its integration guide describes other ways to inspect message content.

The local test configuration starts the signup fixture automatically. Run:

npx playwright test tests/signup.spec.js --grep 'signup verifies'

This creates an unrelated message as well as the target signup message. The test must find the right one to reach the expected account.

Match the recipient instead of taking the newest message

Give each test a unique address, such as signup-${randomUUID()}@example.test. Shared names and “latest message” lookups create ambiguity when another test sends mail at nearly the same time.

The package's helper polls Mailpit for the exact recipient and expected subject. This excerpt shows the matching step; the complete helper also retrieves the selected message and validates its link:

let match;
await expect.poll(async () => {
  const response = await request.get(`${inbox}/api/v1/search`, {
    params: { query: `to:${recipient}` },
  });
  expect(response.ok()).toBe(true);
  const data = await response.json();
  match = data.messages.find(message =>
    message.To.some(to => to.Address === recipient) &&
    message.Subject === 'Verify your account'
  );
  return Boolean(match);
}, {
  timeout: 5_000,
  message: `No verification message for ${recipient}`,
}).toBe(true);

This bounded wait accommodates a small delivery delay without sleeping for a fixed period before checking. An API error fails with different evidence from an inbox that remains empty. Do not turn every failed API response into “message not found.”

For a real product that sends several messages with the same subject to one address, also use a run identifier, action timestamp or another reliable correlation value. The unique recipient is sufficient for this fixture because each test creates a fresh address and expects one verification message.

Follow the intended link and inspect the account

The helper fetches the matched message by ID and extracts the plain-text verification URL. It checks the URL's origin and /verify path before navigating. This parser is intentionally specific to the fixture's message format. Adapt it to your own message template instead of following the first arbitrary link in an email.

The browser portion then verifies both the confirmation and its consequence:

message = await verificationMessage(request, recipient);
await page.goto(message.url);
await expect(page.getByRole('heading', { name: 'Email verified', exact: true })).toBeVisible();
await page.getByRole('link', { name: 'Open account' }).click();
await expect(page.getByRole('heading', { name: 'Account ready' })).toBeVisible();
await expect(page.getByText(recipient, { exact: true })).toBeVisible();
await page.goto(message.url);
await expect(page.getByRole('heading', { name: 'Link already used' })).toBeVisible();

Checking the recipient on the resulting account page is important. A test could otherwise consume another user's verification message, reach a generic success heading and incorrectly report success for the intended signup.

The complete test first checks that a fresh session cannot access the account. After verification it checks access and identity, then exercises the fixture's reuse behavior. Each piece corresponds to a different part of the contract.

Exercise expiry and missing mail deliberately

The fixture can issue an already-expired token, so the expiry test does not have to wait for wall-clock minutes to pass. It can also suppress mail delivery while keeping the signup confirmation page. In a Linux/macOS shell:

DEMO_FAULT=expired npx playwright test tests/signup.spec.js --grep 'expired link'
DEMO_FAULT=no-mail npx playwright test tests/signup.spec.js --grep 'missing message'

The expiry command should pass because that test expects rejection and continued blocked account access. The missing-mail command intentionally fails after its bounded wait. This demonstrates that a successful signup response cannot substitute for an actual message in the inbox.

Local validation with Playwright Test 1.63.0 and Mailpit 1.32.0 exercised these cases:

CaseObserved outcome
Target signup alongside an unrelated messageCorrect recipient verified and account opened
Already-used verification linkFixture's rejection state displayed
Expired verification linkRejected; account access stayed blocked
No mail sentPolling failed with a recipient-specific missing-message message

The archive's evidence.json contains the recorded attempts. These results establish the fixture's behavior under the selected conditions, not the reliability of a real email delivery service.

Clean up the data belonging to the run

The successful and expired-link tests delete captured messages by their individual IDs in cleanup. They do not empty the whole inbox, which could remove evidence needed by another test. The fixture's account records disappear when its process stops.

Stop the example inbox after finishing:

docker stop replaceqa-blog-mailpit

In a shared test environment, give account cleanup the same ownership discipline as email cleanup. Retain the identifiers needed to remove only that run's data, including when the browser fails partway through the journey.

For your broader plan, keep this controlled signup check separate from public release checks. The small-team testing strategy and production smoke checklist explain how to record that scope. ReplaceQA's current closed beta focuses on public journeys; mailbox automation and authenticated target-app testing are not features demonstrated by this standalone tutorial.

Keep checking your important customer journeys.

Explore ReplaceQA ↗