An end-to-end test requests an email code, finds six digits in a message, enters them, and sees a green checkmark. It proves that one code can complete one path. It says nothing about whether an expired code still works, a resend leaves two valid codes behind, or the same code can be replayed.
That gap matters because email OTP verification is a small state machine. Tests should cover the transitions and the boundaries around them, not just the happy path.
Start with a behavior table
Before writing selectors, write down what the product promises. A useful minimum looks like this:
| Situation | Expected result |
|---|---|
| Correct, unexpired code | Verification succeeds for the address that requested it |
| Incorrect code | Verification stays incomplete; a clear error appears |
| Expired code | Verification is rejected |
| Code submitted twice | The second submission is rejected if codes are single-use |
| Resend requested | The newest message arrives; behavior of the older code matches the product's policy |
| Delivery delayed or missing | The test fails with a useful timeout, not a fixed sleep or a false pass |
The last detail in the resend row is a product decision. Some systems invalidate the previous code on resend; others allow both until their individual expiry. Make the contract explicit, then assert that contract.
Give every run its own recipient
Parallel tests that share an email address can read one another's messages. A unique recipient per test run gives the test a reliable way to isolate delivery. Use a new address for retries too, so a delayed email from a failed attempt cannot be mistaken for the retry's code.
Create the inbox before triggering the send. Then wait for a message with the sender or subject you expect. A real wait for a matching message is better than waitForTimeout(5000): a sleep is sometimes too short and otherwise makes every run slower.
Here is the delivery portion using Playwright's request client and an InboxRhino inbox fixture. The fixture should provide a ready inbox and delete it after each test. Keep the API key in the test runner's environment or CI secrets.
import { expect, test } from '@playwright/test'; test('verifies an address using the code delivered to this run', async ({ page, request, inbox }) => { const apiKey = process.env.INBOXRHINO_API_KEY; if (!apiKey) throw new Error('INBOXRHINO_API_KEY is required'); await page.goto('https://staging.example.com/verify-email'); await page.getByLabel('Email').fill(inbox.address); await page.getByRole('button', { name: 'Send code' }).click(); const query = new URLSearchParams({ wait_seconds: '120', limit: '1', include: 'content', sender: 'accounts@example.com', subject: 'Your verification code', }); const response = await request.get( 'https://api.inboxrhino.in/v1/inboxes/' + encodeURIComponent(inbox.id) + '/messages?' + query, { headers: { Authorization: 'Bearer ' + apiKey }, timeout: 130_000, }, ); // A wait timeout is HTTP 204 with no response body. if (response.status() === 204) { throw new Error('No verification message arrived for ' + inbox.address); } expect(response.status()).toBe(200); const { data } = await response.json(); expect(data).toHaveLength(1); expect(data[0].to.toLowerCase()).toBe(inbox.address.toLowerCase()); // Match the code using the actual, stable template surrounding it. const body = String(data[0].text ?? '') + '\n' + String(data[0].html ?? ''); const match = body.match(/Your verification code is:\s*(\d{6})/i); expect(match, 'message should contain a code in the expected template').not.toBeNull(); await page.getByLabel('Verification code').fill(match![1]); await page.getByRole('button', { name: 'Verify' }).click(); await expect(page.getByTestId('verified-email')).toHaveText(inbox.address); }); The subject, sender, message template, page selectors, and fixture are examples; replace them with your application's contract. Avoid extracting the first six digits from any message. It might be a support number, an order ID, or a code from a different notification. Assert the recipient too, and make the UI show which address became verified.
Test expiry without relying on the wall clock
Waiting around for a production-length expiry makes a slow and flaky test. If the application has an injectable clock or a test-only expiry setting, issue a code, advance time past its expiry, and submit it. Assert that verification is rejected and the account remains unverified.
Keep that test seam restricted to the test environment. If the app has no controllable clock, test the expired-code branch at the service boundary with a fixed clock, then keep one shorter end-to-end test to prove the actual email and UI path work together.
Treat resend as a separate transition
Resend behavior catches subtle defects: the second email never arrives, the interface reports success when sending failed, or an older code remains valid against a policy that says it should be revoked.
Use the same inbox for a resend test only when the inbox query can distinguish the messages reliably. Capture message identifiers or a delivery timestamp, request the resend, and wait for the next matching message. Then assert the old and new code behavior according to the documented policy. Do not assume that a later received_at proves which UI action caused the message; a delayed message from the earlier request can arrive later.
Check the account state, not just the response text
“Code accepted” is a weak final assertion if the verification page can accept a code for the wrong account. Assert the email address displayed as verified, or check persisted account state through a test-only API. For single-use codes, submit the same code again and assert it is rejected. For wrong-code handling, check that the account remains unverified as well as checking the visible error.
Keep OTPs and full email bodies out of CI logs. Record safer diagnostics such as the recipient, message ID, test-run ID, and whether the verification state changed. Never put a real user's mailbox or production account into an end-to-end test.
The goal is not to test every combination of digits. It is to test the small number of state transitions that define the security and user experience of the flow. Once those expectations are written down, the inbox becomes one step in a precise test, rather than a six-digit string that happens to make the suite green.
Disclosure: I build InboxRhino, a receive-only inbox API for email tests. The state-machine checks here apply with any test mailbox your runner can read.
