E2E 邮件测试:使用 Playwright 和 Cypress — 无需 Gmail 凭证
Source: Dev.to
概览
你的 Playwright 测试点击 “Reset Password.”,随后会发送一封邮件。接下来怎么办?
大多数团队会使用模拟的 SMTP 或 MailHog 来伪造这一步。测试在本地可以通过,但在预发布环境会失败,因为邮件根本没有真正送达。还有更好的办法。
创建真实的测试收件箱
brew install nylas/nylas-cli/nylas
nylas init
nylas inbound create e2e-testnylas inbound create 会为你配置一个受管的电子邮件地址,用来接收真实邮件。无需 MX 记录,也不需要邮件服务器。
在测试中轮询消息
nylas inbound messages 用来检查新邮件:
nylas inbound messages inbox_abc123 --unread --json --limit 1解析 JSON 输出,提取重置链接、验证码或确认邮件等信息。
实时监控
nylas inbound monitor 会在邮件到达时实时流式输出:
nylas inbound monitor inbox_abc123 --tunnel cloudflared提取 OTP 码
在测试双因素认证时?nylas workflow otp list 可以提取一次性密码:
nylas workflow otp list --limit 1列出所有测试收件箱
nylas inbound list 显示你管理的地址列表:
nylas inbound list --json为什么不用 MailHog?
MailHog 只在本地拦截 SMTP——你的应用必须配置为通过它发送邮件。而 Nylas Inbound 地址是通过真实的基础设施接收邮件。如果邮件能到达这里,同样也能到达 Gmail。
CI 集成
- name: Install Nylas CLI
run: curl -fsSL https://cli.nylas.com/install.sh | bash
- name: Run E2E tests
env:
NYLAS_API_KEY: ${{ secrets.NYLAS_API_KEY }}
run: |
nylas auth config --api-key $NYLAS_API_KEY
npx playwright testWebhook 测试
nylas webhook test send 发送测试事件:
nylas webhook test send https://staging.myapp.com/hooks/emailnylas webhook server 启动本地接收器:
nylas webhook server --port 4000所有命令: Nylas CLI Command Reference
快速开始: brew install nylas/nylas-cli/nylas — 其他安装方式