E2E 邮件测试:使用 Playwright 和 Cypress — 无需 Gmail 凭证

发布: (2026年4月11日 GMT+8 05:58)
3 分钟阅读
原文: Dev.to

Source: Dev.to

概览

你的 Playwright 测试点击 “Reset Password.”,随后会发送一封邮件。接下来怎么办?

大多数团队会使用模拟的 SMTP 或 MailHog 来伪造这一步。测试在本地可以通过,但在预发布环境会失败,因为邮件根本没有真正送达。还有更好的办法。

创建真实的测试收件箱

brew install nylas/nylas-cli/nylas
nylas init
nylas inbound create e2e-test

nylas 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 test

Webhook 测试

nylas webhook test send 发送测试事件:

nylas webhook test send https://staging.myapp.com/hooks/email

nylas webhook server 启动本地接收器:

nylas webhook server --port 4000

所有命令: Nylas CLI Command Reference

快速开始: brew install nylas/nylas-cli/nylas其他安装方式

0 浏览
Back to Blog

相关文章

阅读更多 »

Nylas 时区信息实用指南

工作原理 这些时区命令使用编译进二进制文件的 IANA timezone database。无需网络调用、无需 API keys、没有速率限制。它们在飞行模式下也能工作。