How to test email verification, invitation, and password reset links in Playwright and Cypress
What you want to catch
Invitations, password resets, and email verification all complete only when someone opens a link they received by email. UI tests alone miss bugs like these:
- The email is not sent, or it goes to the wrong person
- The link points at the wrong host (a production URL in a staging email, for example)
- Opening the link does not lead to the expected page
- A used or expired link still works
This post uses the MailBrew API to create an address per test, extract the link from the email that arrives, and open it in the browser, in both Playwright and Cypress. For flows where the user types a code (a one-time password) instead, see the Playwright post and the Cypress post.
The flow
- Create a test address
- In the app under test, send an invitation or reset to that address
- Wait for the email
- Extract the link from it
- Open the link in the browser and check the page
Step 3 uses server-side long polling. The server checks for new mail every second and responds as soon as the email arrives. Unlike sleeping for a fixed number of seconds, the test moves on as soon as the email is there.
The link extraction endpoint
Links come from this endpoint. In the SDK, extractLinks() returns its links array as is.
GET /api/mailbrew/addresses/{address}/emails/{emailId}/links?pattern=...
{
"data": {
"links": ["https://staging.example.com/invitations/42?signature=..."],
"email_id": 1234,
"source": "text_body"
}
}
A few rules are worth knowing:
| Rule | Details |
|---|---|
| Where it looks | If the email has a plain-text body, only the text body is searched. For HTML-only emails, it reads href values and URLs in the body text |
| What counts | Only URLs starting with http:// or https:// |
| Order | In order of appearance for text bodies. For HTML-only emails, href values come first, then URLs in the body text. Duplicates are merged |
pattern |
A regular expression; only matching URLs are returned. It cannot contain / (you get a 422) and is limited to 200 characters |
Emails also contain links to your logo, support page, unsubscribe page, and so on. Use pattern to narrow the result down to the link you are testing. Since / is not allowed, match on a word in the path, such as invitations or reset-password.
Extracted URLs come back ready to open in a browser. An & in an HTML href is turned back into &, and punctuation that directly follows a URL in a text body (a sentence-ending period, or Japanese punctuation such as 「。」) is not included.
Playwright: join a team from an invitation email
mailbox.captureLinks() in @mailbrew/playwright creates an address and bundles waiting and extracting into one call. Addresses it creates are deleted automatically when the test finishes.
npm install -D @mailbrew/playwright
import { test, expect } from '@mailbrew/playwright';
test('can join the team from the invitation email', async ({ page, browser, mailbox }) => {
// 1. Create the invitee's address (only keep invitation links)
const { address, waitAndExtract } = await mailbox.captureLinks({ pattern: 'invitations' });
// 2. Send the invitation as an admin (assumes you are signed in)
await page.goto('https://staging.example.com/team/members');
await page.getByLabel('Email').fill(address);
await page.getByRole('button', { name: 'Send invitation' }).click();
// 3. Wait for the email and extract the link
const { links, email } = await waitAndExtract();
expect(email.subject).toContain('invited');
expect(links).toHaveLength(1);
const inviteUrl = links[0];
expect(inviteUrl).toMatch(/^https:\/\/staging\.example\.com\//);
// 4. Open it as the invitee, in a separate browser context
const invitee = await browser.newContext();
const invitedPage = await invitee.newPage();
await invitedPage.goto(inviteUrl);
await expect(invitedPage.getByRole('heading', { name: 'Join the team' })).toBeVisible();
await invitee.close();
});
If you open the invitation in the browser where the admin is still signed in, you are not testing what the invitee sees. browser.newContext() creates a separate context that does not share cookies.
The host check (toMatch) catches staging emails that contain production or localhost URLs, which usually means the app URL setting is missing.
Playwright: check that a password reset link works only once
Password resets need an existing user. If the test signs up first and then requests a reset, the same address receives two emails: the signup confirmation and the reset email.
Waiting for a single email can then hand you the signup email that arrived first. Pass count: 2 to waitForEmails() to wait until both are there, and pick the reset email by subject.
import { test, expect } from '@mailbrew/playwright';
test('a password reset link works only once', async ({ page, mailbox }) => {
test.setTimeout(60000);
const addr = await mailbox.createAddress({ ttl: 600 });
// 1. Sign up (email 1: signup confirmation)
await page.goto('https://staging.example.com/register');
await page.getByLabel('Email').fill(addr.address);
await page.getByLabel('Password').fill('first-password-123');
await page.getByRole('button', { name: 'Sign up' }).click();
// 2. Request a password reset (email 2)
await page.goto('https://staging.example.com/forgot-password');
await page.getByLabel('Email').fill(addr.address);
await page.getByRole('button', { name: 'Send reset link' }).click();
// 3. Wait for both emails and pick the reset one by subject
const emails = await mailbox.waitForEmails(addr.address, { count: 2, timeout: 30000 });
expect(emails).toHaveLength(2);
const resetMail = emails.find((e) => e.subject.includes('Reset'));
const [link] = await mailbox.extractLinks(addr.address, resetMail.id, { pattern: 'reset-password' });
const resetUrl = link;
// 4. Set a new password
await page.goto(resetUrl);
await page.getByLabel('New password').fill('second-password-456');
await page.getByRole('button', { name: 'Update password' }).click();
await expect(page.getByText('Your password has been updated')).toBeVisible();
// 5. Open the same link again and check that it no longer works
await page.goto(resetUrl);
await expect(page.getByText('This link is invalid')).toBeVisible();
});
If fewer emails than count arrive in time, waitForEmails() does not throw; it returns whatever arrived. That is why the example checks the length with expect before going on. The newest email comes first.
Cypress: open the verification link
With @mailbrew/cypress, you chain the custom commands. Setup and passing the API key work the same way as in the Cypress post.
describe('Email verification', () => {
it('marks the email as verified when the link is opened', () => {
cy.mailbrewCreateAddress().then((addr) => {
// 1. Sign up with the new address
cy.visit('/register');
cy.get('#email').type(addr.address);
cy.get('#password').type('first-password-123');
cy.get('#submit').click();
// 2. Wait for the email and extract the verification link
cy.mailbrewWaitForEmail(addr.address).then((email) => {
cy.mailbrewExtractLinks(addr.address, email.id, { pattern: 'verify' }).then((links) => {
expect(links).to.have.length(1);
const url = links[0];
expect(url).to.match(/^https:\/\/staging\.example\.com\//);
// 3. Open the link and check the verified page
cy.visit(url);
cy.contains('Your email address has been verified');
});
});
});
});
});
In Cypress, moving to a different origin in the middle of a test requires cy.origin(). If the link's host differs from the page you opened first, the commands after cy.visit() time out and the cause is hard to see. The example checks the host before opening the link, so the test fails with a clear reason.
To wait for a second email, use cy.mailbrewWaitForEmails(addr.address, { count: 2 }).
Testing link reuse and expiry
Whether a link is safe depends on your app, not on MailBrew. These are the checks worth having in your E2E suite:
| Check | How |
|---|---|
| A used link no longer works | Open the same link again, as in the example above |
| Requesting a second reset invalidates the first link | Request twice, wait with count: 2, and open the older link |
| An expired link does not work | Do not actually wait for expiry. Shorten the lifetime in the test environment only (in Laravel, passwords.users.expire in config/auth.php), or shift the app's clock |
| One user's link cannot be used by another | Invite two addresses and open one link while signed in as the other user |
Tests that really wait for a link to expire are slow and run into CI time limits. Keep the shortened lifetime in test environments only, separate from your production settings.
Running in CI
- An owner creates the API key on the "API Keys" page of the dashboard. These tests need
addresses:write(create addresses),addresses:delete(clean up), andemails:read(fetch, wait for, and extract links from emails). - If your organization has IP restrictions turned on, they also apply to API key calls. See Access control for test inboxes.
- To keep all staging email away from real recipients, see Keep staging emails away from real recipients.
Summary
| Task | Playwright | Cypress |
|---|---|---|
| Create an address | mailbox.createAddress() / mailbox.captureLinks() |
cy.mailbrewCreateAddress() |
| Wait for one email | mailbox.waitForEmail() |
cy.mailbrewWaitForEmail() |
| Wait for several emails | mailbox.waitForEmails(address, { count }) |
cy.mailbrewWaitForEmails(address, { count }) |
| Extract links | mailbox.extractLinks(address, id, { pattern }) |
cy.mailbrewExtractLinks(address, id, { pattern }) |
Full API details are in the API documentation.