# How to test login functionality using OTP codes with Playwright

Use Humanless to give the test user a real inbox that your test can read: create an identity with the `email` channel, enter its address on the login form, submit, and call `waitForCode` to receive the parsed OTP as a string (leading zeros intact). Then fill the code and assert the logged-in state. The wait is a long-poll, so there is no sleep or polling interval to tune, and messages without a code, such as a welcome email, are skipped. If your login has a "Resend code" button, record a new `since` timestamp before clicking it so the test types the newest code, not the one it replaced.

```ts login-otp.spec.ts
const since = Date.now();
await page.getByRole("button", { name: "Email me a code" }).click();
const { code } = await hl.waitForCode({ identityId: inbox.identity_id, since, timeout: 25 });
await page.getByLabel("One-time code").fill(code);
await page.getByRole("button", { name: "Log in" }).click();
await expect(page.getByRole("heading", { name: "Dashboard" })).toBeVisible();
```

## Other options

- **A test-mode bypass**, such as a fixed OTP for one seeded test user or reading the code from your database. Fast, but it never exercises the real email.
- **Mailpit**: capture your app's SMTP output locally and read the code from its REST API.
- **Hosted inbox APIs** such as Mailosaur or MailSlurp work the same way as Humanless.

> **Use policy:** Only for apps you own or are authorized to test. See the [Terms](/terms).

More: [How to test email OTP and verification codes in Playwright](/guides/playwright-email-otp) · [All answers](/answers/)

Last updated 2026-12-04

Canonical: https://usehumanless.com/answers/how-to-test-login-functionality-using-otp-codes-with-playwright

---
More: https://usehumanless.com/llms.txt
