# Playwright guide

> Give each Playwright test its own disposable inbox, phone number or authenticator, then wait for the parsed code with one call. This replaces fixed sleeps, shared test inboxes and webmail scraping with a push-based wait that returns as soon as the message arrives.

## Setup

1. Create an API key at [api.usehumanless.com/app](https://api.usehumanless.com/app). Set it as the `HUMANLESS_API_KEY` secret in CI.
2. Download the [TypeScript SDK](https://usehumanless.com/sdk/humanless.ts) into your project as `tests/humanless.ts` (see [SDK docs](https://usehumanless.com/docs/sdk)).

```ts
// tests/humanless-client.ts
import { Humanless } from "./humanless";
export const hl = new Humanless({ apiKey: process.env.HUMANLESS_API_KEY! });
```

## Before and after

```ts
// Before: a real inbox, a fixed sleep, then scraping a webmail UI and hoping the first 6 digits are the code.
await page.click("text=Send code");
await page.waitForTimeout(15_000);
await mail.goto("https://webmail.example.com");
const code = (await mail.textContent(".msg"))!.match(/\d{6}/)![0];
```

```ts
// After: a disposable inbox per test, an event-driven wait, and a parsed code.
const { code } = await hl.waitForCode({ identityId: identity_id });
```

## Email OTP sign-up

```ts
import { expect, test } from "@playwright/test";
import { withIdentity } from "./humanless";
import { hl } from "./humanless-client";

test("sign up with an email code", async ({ page }) => {
  await withIdentity(hl, { channels: ["email"] }, async ({ identity_id, email }) => {
    await page.goto("http://localhost:3100/signup");
    await page.getByLabel("Email").fill(email!);
    await page.getByRole("button", { name: "Send verification code" }).click();
    const { code } = await hl.waitForCode({ identityId: identity_id });
    await page.getByLabel("Verification code").fill(code);
    await page.getByRole("button", { name: "Verify" }).click();
    await expect(page.getByTestId("welcome")).toBeVisible();
  });
});
```

`withIdentity` deletes the identity even if the test fails.

## Magic link login

Take a timestamp before you submit, so the wait can't miss a fast email:

```ts
test("log in with a magic link", async ({ page }) => {
  await withIdentity(hl, { channels: ["email"] }, async ({ identity_id, email }) => {
    await page.goto("http://localhost:3100/login");
    await page.getByLabel("Email").fill(email!);
    const since = Date.now();
    await page.getByRole("button", { name: "Email me a link" }).click();
    const { url } = await hl.waitForLink({ identityId: identity_id, since });
    await page.goto(url);
    await expect(page.getByTestId("dashboard")).toBeVisible();
  });
});
```

## SMS OTP

SMS identities need a paid plan or prepaid credits. `phone` is in E.164 format (`+1...`). If your form has a separate country-code dropdown, type the number without `+1`.

```ts
test("verify a phone number", async ({ page }) => {
  await withIdentity(hl, { channels: ["sms"] }, async ({ identity_id, phone }) => {
    await page.goto("http://localhost:3100/settings/phone");
    await page.getByLabel("Phone").fill(phone!);
    await page.getByRole("button", { name: "Text me a code" }).click();
    const { code } = await hl.waitForCode({ identityId: identity_id, channel: "sms" });
    await page.getByLabel("Code").fill(code);
  });
});
```

## TOTP two-factor authentication

Enroll the identity's `totp_secret` where your app offers "enter the key manually". Then read codes whenever the app asks for one:

```ts
test("enable and use 2FA", async ({ page }) => {
  await withIdentity(hl, { channels: ["email", "totp"] }, async ({ identity_id, totp_secret }) => {
    // ...sign up with the email first...
    await page.getByRole("button", { name: "Can't scan the QR code?" }).click();
    await page.getByLabel("Setup key").fill(totp_secret!);
    let t = await hl.getTotp(identity_id);
    if (t.seconds_remaining <= 3) {
      await page.waitForTimeout((t.seconds_remaining + 1) * 1000);
      t = await hl.getTotp(identity_id);
    }
    await page.getByLabel("Authentication code").fill(t.code);
  });
});
```

## Tips

- **Never sleep before waiting.** Call `waitForCode` right after the click. It returns as soon as the message lands.
- **Parallel workers:** create one identity per test. Email identities aren't capped per run, but identity creation is rate-limited per plan (30, 300 or 1,000 per hour; see [rate limits](https://usehumanless.com/docs/api#rate-limits)). SMS identities each hold one of your dedicated numbers (1 on Builder, 5 on Pro) until they are deleted, so run at most that many SMS tests at once.
- **Resend buttons:** each resend sends a new code. Wait again and use the newest one.
- **Timeouts:** the SDK's default overall wait is 60 s. Set Playwright's test timeout higher than that.
- **Your own domain:** custom email domains are set up on request (email hello@usehumanless.com). Once one is verified for your account, pass it as `domain` when you create an identity; identity addresses are generated on it.

---
Canonical: https://usehumanless.com/docs/playwright · Index of all docs: https://usehumanless.com/llms.txt
