端-到-端测试
Drive 你的整个 app 如真实用户到 验证关键 journeys 工作 in production.
你将学到
- 什么端-到-端测试是 and confidence 它们 provide
- 如何 tools 如 Playwright and Cypress automate 浏览器
- 选择关键用户 journeys 到 cover
- Fighting flakiness 使用 retries, waits, and isolated 状态
概念讲解
Testing From the Outside
An end-to-end (E2E) test interacts with the running application exactly as a user would: it loads a real URL, clicks buttons, types into fields, and reads the resulting page. Because it exercises the entire stack — browser, frontend, API, database — a passing E2E test gives the strongest possible signal that the feature truly works for users.
Tools of the Trade
Playwright and Cypress are the dominant browser automation tools. Playwright (from Microsoft) supports Chromium, Firefox, and WebKit with a single API and runs tests in parallel across browsers. Cypress runs in the browser itself and offers a time-traveling debugger. Both let you write tests in JavaScript or TypeScript and handle the hard parts of browser timing for you.
Choose Critical Journeys
E2E tests are slow and brittle, so you cannot cover every path. Pick the journeys that matter most to the business: sign-up and checkout for an e-commerce site, creating and sharing a document for a collaboration tool. Three to ten well-chosen E2E tests often give more value than a hundred fragile ones. Document what each journey covers so the suite stays intentional.
Beating Flakiness
E2E tests are notorious for failing randomly because of timing. Fight this with auto-waiting (let the tool wait for elements to be ready instead of sleeping), retries for genuinely flaky network conditions, and isolated test data so tests never collide. Run E2E tests on every pull request but keep them out of the tight inner loop — a 30-second E2E suite on every save will kill productivity.
In Practice
When authoring E2E tests, prefer 数据-testid attributes or accessible roles for selectors so tests survive CSS and copy changes. Create a fresh user account or reset the database at the start of each test so journeys never interfere with one another. If a test fails intermittently, do not reach for retries as a first fix — investigate the race condition, because retrying hides real bugs. Run the E2E suite in a headless browser in CI for speed, but occasionally run it headed locally to watch the automation and catch visual issues the assertions miss. Keep a small library of page-object helpers that wrap common interactions (login, navigate to a record, fill a form) so each journey test reads as a short, high-level script. This keeps the suite maintainable as the application grows.
示例代码
// Playwright E2E test for a login journey
import { test, expect } from "@playwright/test";
test("user can log in and see the dashboard", async ({ page }) => {
await page.goto("https://app.example.com/login");
await page.fill('[data-testid="email"]', "[email protected]");
await page.fill('[data-testid="password"]', "correct-horse");
await page.click('[data-testid="submit"]');
// Playwright auto-waits for this URL to appear
await expect(page).toHaveURL(/\/dashboard/);
await expect(page.locator("h1")).toHaveText("Welcome, Ada");
});
测试 drives 真实浏览器通过登录 flow and asserts 用户 lands on dashboard. 数据-testid attributes 制作 selectors resilient 到样式更改.
动手试一试
- HTTP 请求 Tester
Replay API calls 你的 E2E journey 触发后面 scenes.
- HTTP 状态 Codes
确认响应 codes 你的 E2E flow expects.
常见错误
错误写法
Using fixed sleeps 相反的 waiting for 条件
正确做法
Use auto-waiting assertions (expect(locator).toBeVisible()) 相反的 page.waitForTimeout. Fixed sleeps 创建 races on 慢 machines.
错误写法
编写 E2E 测试 for 每个 minor UI variation
正确做法
Cover 关键 journeys 一次. 推送 detailed UI 检查下 到组件测试, 哪个是 远 faster and 更多 stable.
练习 端-到-端测试
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。