你是否曾经为了测试一个简单的用户流程而写了几十行代码感到疲惫?或者当UI改变时所有测试都崩溃而感到沮丧?e2e是一个使用AI解决这些问题的下一代端到端测试框架。
在这篇文章中,我们将探索e2e如何工作,以及为什么它可能会改变你编写测试的方式。
为什么需要e2e?#
想象一下你正在测试注册流程。使用Playwright或Cypress等传统框架,你需要写:
await page.click('button[name="signup"]');
await page.fill('input[name="email"]', 'test@example.com');
await page.fill('input[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page.locator('.welcome-message')).toBeVisible();typescript如果按钮名称改变或布局变化,你的测试就会失败。你必须手动修复代码。
使用e2e,你只需告诉AI:
await agent.act('sign up with email {email}', {
params: { email: 'test@example.com' }
});
await agent.assert('the welcome screen is showing');typescriptAI会自动找出如何执行操作并检查结果。当UI改变时,AI会自动适应。
e2e是什么?#
e2e是一个用于Web和移动应用的开源端到端测试框架,旨在跟上当今软件发布速度。主要特点:
- AI驱动:使用AI模型执行操作并检查结果
- 确定性:也支持带有定位器和断言的传统API
- 混合:你可以在同一个测试中混合使用两种方法
- 跨平台:在Web(Chromium)、iOS和Android上运行测试
e2e如何工作#
e2e有三个主要组件:
目标#
每个测试在每个目标上运行一次:
- Web:通过Playwright的Chromium、Firefox、Safari
- 移动:通过agent-device的iOS模拟器和Android模拟器
- 自定义:你可以为其他平台编写自己的引擎
代理步骤#
agent.act:用自然语言完成目标agent.assert:检查屏幕上的条件agent.waitFor:等待条件变为trueagent.extract:从屏幕提取结构化数据
缓存#
已验证的act步骤会被记录并从缓存中重放,无需AI模型调用。这有助于:
- 减少token成本
- 加快测试运行速度
- 当应用未改变时保留旧操作
开始使用e2e#
快速设置#
npx e2e initbash此命令将:
- 创建
e2e.config.ts配置文件 - 创建示例测试
- 添加依赖项
- 设置你的编码代理
Web配置#
import type { E2EConfig } from 'e2e';
import { web } from '@e2e-dev/web';
import { gateway } from 'ai';
export default {
agents: { default: { model: gateway('openai/gpt-6-luna-fast') } },
targets: [{ engine: web(), app: { url: 'http://localhost:3000' } }],
} satisfies E2EConfig;typescript第一个测试#
import { test } from '@e2e-dev/web';
import { expect } from 'e2e';
test('app opens', async ({ app, browser }) => {
await app.open('/');
await expect(browser.locator('body')).toBeVisible();
});typescript运行测试:
npx e2e runbash使用AI编写测试#
代理操作#
import { test } from '@e2e-dev/web';
import { expect } from 'e2e';
test('a visitor signs up for a trial', async ({ app, agent, screen }) => {
await app.open('/');
await agent.act('sign up for a free trial as {name} with email {email}', {
params: { name: 'Ada Lovelace', email: 'ada@example.test' },
});
await agent.assert('the welcome screen greets Ada by name');
await expect(screen.getByRole('status')).toContainText('trial');
});typescript混合确定性和AI#
test('todos survive a filter round-trip', async ({ app, agent, screen }) => {
await app.open('/todos');
// AI处理复杂交互
await agent.act('add todos "Buy milk" and "Walk the dog", complete the first');
await agent.assert('only one todo remains open');
// 确定性检查精确值
await screen.getByRole('tab', 'Done').tap();
await expect(screen.getByTestId('todo')).toHaveCount(1);
});typescript结合两种方法#
e2e允许你在同一个测试中混合代理步骤和确定性API:
- 代理步骤:当你不知道需要的确切操作时
- 确定性API:当你需要检查精确值或知道定位器时
// 代理处理复杂流程
await agent.act('complete checkout with the test card');
// 确定性检查精确结果
await expect(screen.getByText('Order #12345')).toBeVisible();typescripte2e的好处#
节省时间#
编写目标而不是选择器和等待的脚本。当UI改变时,代理会适应,因此你可以花更少的时间编写和修复测试。
降低风险#
在Web和移动上覆盖更多用户依赖的流程,并在它们到达生产环境之前捕获回归。
便宜的运行#
已验证的agent.act步骤从缓存重放,无需模型调用。无需token账单或缓慢的CI即可测试更多流程。
身份验证和密钥#
e2e内置了对密钥的支持:
import { credentials } from 'e2e';
const member = credentials.user('member');
await agent.act('Sign in', {
params: { username: member.username, password: member.password },
});typescript密钥会从模型输入中编辑掉以确保安全。
移动测试#
e2e也支持iOS和Android:
import { mobile } from '@e2e-dev/mobile';
export default {
targets: [{
name: 'ios',
engine: mobile({ platform: 'ios' }),
app: { bundleId: 'com.yourapp' }
}],
workers: 1,
} satisfies E2EConfig;typescriptimport { test } from '@e2e-dev/mobile';
test('the agent opens General', async ({ agent, app, device }) => {
await app.open();
await agent.act('open {section} settings', { params: { section: 'General' } });
await agent.assert('the General settings screen is showing');
await expect(device.locator('role=NavigationBar id=General')).toBeVisible();
});typescript编码代理#
e2e设计用于与Claude、Cursor、Codex等编码代理一起工作。设置后,你可以:
- “Add a test for checkout”
- “Why did this run fail?”
- “Debug this test”
代理会自动读取技能并执行必要的操作。
结论#
e2e代表了端到端测试的重大飞跃。通过将AI与确定性API结合,它允许你:
- 更快地编写测试
- 更容易地维护测试
- 覆盖更多用户流程
- 在多个平台上运行测试
如果你正在寻找一种现代化的方法来测试你的应用程序,e2e值得一试。