An end-to-end test of a signup with a texted code has one awkward step: the code goes to a phone, and a browser test can't read a phone. The practical answer is to let your app send through a test key, read the code from the place your own app stored it, and keep one small real-send check on a schedule for the part a simulation can't prove.
This guide is for the browser-test angle. We've already covered mocking the SMS API in unit tests and testing without sending real texts, so here we stay on Playwright: the fixture, the test, the traps, and what each layer of the suite does and doesn't prove. We make Ready, so the SMS details use only the request shape we document.
What the browser test should prove
A browser test is slow and fragile compared to a unit test, so spend it on what only a browser can show. For a signup with a code, that's the journey: the phone field accepts the number, the code screen appears, a wrong code shows the right error, the right code lands the user on the next page, and "resend" works without breaking the form.
It shouldn't try to prove that a handset received a text. That depends on carriers, registration and quiet hours, and none of them live in your front end. Keep the claims separate:
| Layer | Proves | Doesn't prove |
|---|---|---|
| Unit test with a fake | Your code builds the request and handles each answer | That the API accepts it |
| Playwright with a test key | The whole journey, and that your server calls the API correctly | Delivery to a phone |
| Scheduled real send to a number you own | The live path, including a handset | Behavior at volume |
Point the app under test at a test key
A test key simulates POST /v1/messages: nothing is sent or charged, and each result has a test-msg- id. Create one in Settings, Developers, Environment: Test, and give it only to the environment your Playwright run talks to. Writes a test key can't simulate answer 403 test_key_not_allowed, so a test that wanders into a bulk change fails loudly instead of billing.
Because the send is simulated, nothing arrives anywhere. Your app still generated a code and stored it, and that's the thing the test needs. See the test keys docs for what a test key covers.
Read the code your app stored
You have three honest ways to get the code into the test. Pick one and keep it out of production.
- A test-only read endpoint. The app exposes
GET /test/last-otp?phone=..., mounted only when an environment flag is set. Playwright'srequestfixture calls it. - A direct database read. The test reads the verification row. It's simple, but it ties the test to your schema.
- A recording fake in front of the API. You point your app's SMS base URL at a tiny local server that records each request body, and the test reads the last one. It also checks your request shape, at the cost of one more process.
Avoid the shortcut of a fixed code such as 000000 for "test numbers". It works until the flag leaks into production, and then anyone who knows the number can sign in. If you must, tie it to a phone number allowlist and a non-production environment check, and test that the check holds.
A Playwright fixture and test
The sample below assumes a read endpoint on your own server. The route, the field names and the page labels are yours to change.
// tests/signup-otp.spec.ts
import { test, expect } from "@playwright/test";
const PHONE = "+12125550142"; // a made-up 555-01xx number, never a real one
test("signup with a texted code", async ({ page, request }) => {
await page.goto("/signup");
await page.getByLabel("Mobile number").fill(PHONE);
await page.getByRole("button", { name: "Send code" }).click();
await expect(page.getByText("Enter the 6-digit code")).toBeVisible();
// Poll until the server has stored the code the app generated.
await expect
.poll(async () => {
const res = await request.get("/test/last-otp", { params: { phone: PHONE } });
return res.ok() ? (await res.json()).code : null;
}, { timeout: 10_000 })
.toMatch(/^\d{6}$/);
const { code } = await (
await request.get("/test/last-otp", { params: { phone: PHONE } })
).json();
await page.getByLabel("Code").fill(code);
await page.getByRole("button", { name: "Verify" }).click();
await expect(page).toHaveURL(/\/welcome/);
}); Two details matter. First, expect.poll waits for the code instead of sleeping for a fixed time, so a slow send doesn't make the test flaky. Second, the number is a placeholder in the reserved 555-01xx range, which can't reach a real person even if a flag is wrong.
Test the failure paths, not only the happy one
The paths users hit most are the ones people forget:
- Wrong code. Enter a code you know is wrong and assert the error text and that the field isn't cleared in a confusing way.
- Expired code. If your codes expire, advance your server's clock through a test hook, or set a short expiry for the test environment, and assert the "request a new code" path.
- Resend. Click resend and confirm the old code stops working and the new one works.
- A refused send. Make your test server's fake or stub return an error for one number and assert that the page shows a plain message, not a spinner that never ends.
- Too many attempts. Hit the verify button past your limit and confirm it locks. Our post on SMS pumping fraud explains why resend limits matter for money too.
For the real answers an API text can get, such as opt-outs, the quiet hours 409 and others, read the error codes page and write one case per answer your UI handles. A code text is usually one segment, and the message format guide for domain-bound OTP codes covers the wording.
Keep one real send on a schedule
A simulated send can't tell you the text reached a handset. Keep a single small check outside the main suite: a scheduled job, run nightly or before a release, that signs up with a phone you own and reads the code off the screen by eye or from a device you control. Don't automate reading someone's personal inbox.
Real sends cost money. Every text is charged from the first one, at $0.0075 per segment plus a $0.0045 carrier fee per segment, so a one-segment code costs roughly a cent. A nightly check for a month is well under a dollar, and the figure leaves out screening if you switch it on. A live send also needs a card and a number of your own ($2 a month), and login codes are sent through a campaign registered with the Login and verification use case once your brand is approved. See what OTP costs and how to register it.
A STOP reply from a tester's phone opts that number out. Warn whoever owns the check phone, and check GET /v1/opt-outs/:phone before you decide the pipeline is broken.
Keep the suite from flaking
- One phone number per parallel worker. Two workers using the same number read each other's codes. Derive the number from
testInfo.workerIndex, such as+1212555010${index}. - Clean state between runs. Delete the verification rows in a
beforeEachthrough your test hook. - Retries in config, not in the test. Playwright's
retriessetting reruns a whole failed test. That's fine with a test key, since a rerun sends nothing. It is not fine against live sends. - Traces on failure. Set
trace: "on-first-retry"so a failure leaves a recording you can open. - Never share a key. The CI secret for the test key stays separate from the production key. Our key security guide covers per-environment keys.
What to take away
Let the browser test prove the journey, the test key prove your server calls the API correctly, and one real send on a schedule prove the handset. Read the code from your own app's storage, never with a production backdoor, and give each worker its own placeholder number. When the suite is green, the API quickstart is the shortest path to your first live key.
Accounts opened before November 12, 2026 keep their signup prices, shown under Billing.