跳至主要收件區
E2E 自動化測試範本 · PLAYWRIGHT

在 Playwright 穩定測試電子郵件與 OTP 驗證,無需 SDK 綁定。

於每個 Playwright 測試前呼叫 POST /v1/mailboxes 建立獨立收件匣,透過 Fetch SSE 等待 message.ready 後擷取驗證碼。

三步驟確定性流程

1. 每個測試建立獨立信箱

呼叫 POST /v1/mailboxes 取得專屬地址與 Bearer token。

2. 訂閱 SSE 即時事件

以 Authorization: Bearer 連線 GET /v1/mailboxes/me/events。

3. 擷取驗證碼並清理

讀取訊息內容後於 teardown 呼叫 DELETE /v1/mailboxes/me。

可直接複製的 Playwright 測試範本

TypeScript
import { test, expect } from '@playwright/test';

const API = 'https://api.inboxflick.com/v1';

async function waitForOtp(token: string, timeoutMs = 30_000): Promise<string> {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), timeoutMs);
  try {
    const res = await fetch(`${API}/mailboxes/me/events`, {
      headers: { Authorization: `Bearer ${token}` },
      signal: controller.signal,
    });
    const reader = res.body!.getReader();
    const decoder = new TextDecoder();
    let buffer = '';
    while (true) {
      const { value, done } = await reader.read();
      if (done) break;
      buffer += decoder.decode(value, { stream: true });
      const match = buffer.match(/event: message\.ready\s+data: (\{[^\n]+\})/);
      if (match) {
        const { message_id } = JSON.parse(match[1]);
        const msg = await fetch(`${API}/mailboxes/me/messages/${message_id}`, {
          headers: { Authorization: `Bearer ${token}` },
        }).then((r) => r.json());
        const otp = msg.data.text_body.match(/\b(\d{6})\b/)?.[1];
        if (otp) return otp;
      }
    }
  } finally {
    clearTimeout(timer);
    controller.abort();
  }
  throw new Error('Timed out waiting for OTP email');
}

test('signup verifies 6-digit OTP email', async ({ page }) => {
  const created = await fetch(`${API}/mailboxes`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: '{}',
  }).then((r) => r.json());

  const { address } = created.data.mailbox;
  const token = created.data.access_token;

  try {
    await page.goto('/signup');
    await page.getByLabel('Email').fill(address);
    await page.getByRole('button', { name: 'Continue' }).click();

    const otp = await waitForOtp(token);
    await page.getByLabel('Verification code').fill(otp);
    await expect(page).toHaveURL(/dashboard/);
  } finally {
    await fetch(`${API}/mailboxes/me`, {
      method: 'DELETE',
      headers: { Authorization: `Bearer ${token}` },
    });
  }
});

並行 CI Worker?透過 Pro API 一次建立 1–20 個信箱

升級 Pro($1.99/月或 $8.99 永久)即可呼叫批量端點並行建立最多 20 個收件匣。

POST https://api.inboxflick.com/v1/member/mailboxes/batch
Authorization: Bearer <PRO_MEMBER_JWT>
Content-Type: application/json

{ "count": 20, "is_permanent": false }

以 $8.99 永久解鎖並行 CI 郵件測試

不限每日次數、1–20 批量 API 與 Pro 專屬網域。

cypress vitest pytest API 文件 Pro 會員

CI 郵件測試常見問題

Playwright 需要安裝額外 SDK 嗎?
不需要,原生 fetch() 或 requests 即可串接。
並行測試如何避免單 IP 限制?
使用 Pro JWT 呼叫 /v1/member/mailboxes/batch 即可。
可擷取魔術連結與 HTML 嗎?
可以,API 同時提供 text_body 與淨化後的 html_body。