Playwright: your test waits for an email, it does not sleep
A signup journey ends in a mailbox. Without a mailbox the test can read, two options are left: fake the sending, which proves nothing about the most fragile part, or loop with a fixed delay. Here is the third.
The whole test, nothing cut
This is the complete file, and it imports only Playwright.
import { expect, test } from '@playwright/test';
const INBOX = 'j3k9x2mq';
// Searches every second until the message arrives, thirty seconds at most.
async function waitForMail(sentTo: string, since: Date) {
const search = new URLSearchParams({ inbox: INBOX, sentTo, since: since.toISOString() });
for (let attempt = 0; attempt < 30; attempt++) {
const response = await fetch('https://api.facteur.eu/messages?' + search, {
headers: { authorization: 'Bearer ' + process.env.FACTEUR_API_KEY },
});
const { messages } = await response.json();
if (messages?.[0]) return messages[0];
await new Promise((resolve) => setTimeout(resolve, 1000));
}
throw new Error('Nothing arrived for ' + sentTo);
}
test('the verification link activates the account', async ({ page }) => {
const tag = 'run-' + Date.now();
// Noted before the click: only an email that arrives after it counts.
const since = new Date();
await page.goto('/signup');
await page.getByLabel('Email').fill('signup+' + tag + '@' + INBOX + '.inbox.facteur.eu');
await page.getByRole('button', { name: 'Create my account' }).click();
const message = await waitForMail('signup+' + tag + '@', since);
// The status before any field taken from the body.
expect(message.status).toBe('parsed');
await page.goto(message.links[0]);
await expect(page.getByText('Account verified')).toBeVisible();
});
- The time is noted before the click
- Only an email that arrives after it counts, even one that arrives during the click. An email sent to the same address by an earlier run stays out.
- Each test has its own address
- Everything after the "+" belongs to you and still arrives. Two tests running at the same time therefore do not steal each other's emails, with nothing to create first.
- The code is already extracted
- The message arrives with its one-time code in the otp field, and its links in links. You do not rewrite the regular expression that lingers in every test suite.
- Read status before anything else
- An accepted email exists for a moment before it is read: it then has no subject and no code. This field tells "no code in this message" from "the code is not extracted yet".
Why this test fits in thirty lines
One search a second is enough
Facteur almost always parses an email in under a second, so the loop rarely runs more than twice. Nothing to tune when the continuous integration slows down.
Extraction happens on arrival
One-time codes and links are taken out of the message body the moment it arrives. Your test reads a field instead of slicing HTML.
Addresses are unlimited
Any address ending with your inbox's domain arrives, with no API call to create it. That is what makes one address per test free.
Install the Playwright plugin from npm
playwright-facteur computes one address per test, retries its assertions the way Playwright does and attaches the message received to the report. It installs with npm i -D playwright-facteur. The test above does not use it and works as it is: the plugin adds comfort, not capabilities.
Write this test today
The free plan opens an inbox, with no credit card. By the time you create an account, you have the address to put in the test above.