Playwright
The Node library @waitformail/sdk makes up addresses, waits for messages and cleans up. It has
no dependencies and works in any Node 18+ test runner.
npm install --save-dev @waitformail/sdkThe package is on its way to npm. Until it is there, call the API with
fetchas in the quick start: the requests are the same.
A sign-up test
import { expect, test } from '@playwright/test';
import { Waitformail } from '@waitformail/sdk';
const mail = new Waitformail({
apiKey: process.env.WAITFORMAIL_API_KEY!,
projectId: process.env.WAITFORMAIL_PROJECT!,
});
test('sign up and confirm the email address', async ({ page }) => {
// Room for the app to send: the wait below may take up to 30 s of the test's time.
test.setTimeout(60_000);
// A new address nobody used before: signup-<random>@<your project domain>.
const email = await mail.address('signup');
await page.goto('/signup');
await page.getByLabel('Email').fill(email);
await page.getByRole('button', { name: 'Create account' }).click();
// Resolves as soon as the message is stored.
const message = await mail.waitForMessage({ to: email, timeout: 30_000 });
expect(message.subject).toBe('Confirm your email address');
const link = message.links.find((l) => l.text === 'Confirm your email address');
await page.goto(link!.url);
await expect(page.getByRole('heading', { name: 'Email confirmed' })).toBeVisible();
});Codes instead of links
For a one-time code, read codes: every run of 4 to 8 digits in the message, in order.
const message = await mail.waitForMessage({ to: email, subject: 'Your login code' });
await page.getByLabel('Code').fill(message.codes[0]);What the library does
| Method | What it does |
|---|---|
address(prefix?) |
A new address <prefix>-<random>@<project domain>; nothing to create first |
waitForMessage({ to, from, subject, receivedAfter, timeout }) |
The earliest matching message, at once or as soon as it arrives; rejects with code: 'message_timeout' |
listMessages(filter) |
Messages, newest first, with nextCursor for more |
getMessage(id) |
One message with text, HTML, links, codes and headers |
getRaw(id), getAttachment(id, index) |
The original .eml, an attachment, as bytes |
deleteMessage(id), deleteMessages({ to }) |
Delete one message, all of one address or all of the project |
Errors are thrown as WaitformailError with code and status (see
errors). While waiting for a message, the library also
waits out a rate limit within your timeout.
Tips
- Make up a new address in every test. Tests then never read each other's mail and can run in parallel.
- Keep
timeoutabove the time your application needs to send, and below the test's own timeout. - Read the API key from the environment; in CI, from a secret (see GitHub Actions).