playwright
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePlaywright E2E Testing
Playwright E2E测试
Generate and run Playwright end-to-end tests for web applications.
为Web应用生成并运行Playwright端到端测试。
Prerequisites
前置条件
bash
npx playwright --versionIf not installed:
bash
npm init playwright@latestbash
npx playwright --version如果未安装:
bash
npm init playwright@latestor add to existing project:
或添加到现有项目:
npm install -D @playwright/test && npx playwright install
undefinednpm install -D @playwright/test && npx playwright install
undefinedWorkflow
工作流程
1. Analyze
1. 分析
Read the source files for the page/component to test. Understand routes, interactive elements, and expected behaviors.
读取待测试页面/组件的源文件,理解路由、交互元素和预期行为。
2. Generate Test File
2. 生成测试文件
Create a test file following the project's existing patterns and directory structure.
typescript
import { test, expect } from '@playwright/test';
test.describe('Feature Name', () => {
test.beforeEach(async ({ page }) => {
// Setup: navigate, authenticate, seed data
});
test('should do expected behavior', async ({ page }) => {
// Arrange
// Act
// Assert
});
});遵循项目现有模式和目录结构创建测试文件。
typescript
import { test, expect } from '@playwright/test';
test.describe('Feature Name', () => {
test.beforeEach(async ({ page }) => {
// 准备工作:导航、认证、初始化数据
});
test('should do expected behavior', async ({ page }) => {
// 安排(Arrange)
// 执行(Act)
// 断言(Assert)
});
});3. Run Tests
3. 运行测试
bash
npx playwright test <file> # Run specific test file
npx playwright test --headed # With visible browser
npx playwright test --ui # Playwright UI mode
npx playwright test --trace on # Capture trace for debuggingbash
npx playwright test <file> # 运行指定测试文件
npx playwright test --headed # 显示浏览器窗口运行
npx playwright test --ui # Playwright UI模式
npx playwright test --trace on # 捕获跟踪信息用于调试4. Debug Failures
4. 调试失败测试
- Read error output carefully
- Use and open trace viewer:
--trace onnpx playwright show-trace trace.zip - Take screenshots on failure:
await page.screenshot({ path: 'debug.png' }) - Use for step-by-step debugging in headed mode
page.pause()
- 仔细阅读错误输出
- 使用并打开跟踪查看器:
--trace onnpx playwright show-trace trace.zip - 失败时截图:
await page.screenshot({ path: 'debug.png' }) - 在显示浏览器窗口模式下使用进行分步调试
page.pause()
Capabilities
功能特性
- Navigation: , URL assertions, redirect verification
page.goto() - Forms: ,
page.fill(),page.selectOption(), multi-step wizardspage.check() - Assertions: ,
expect(page).toHaveURL(),expect(locator).toBeVisible()toHaveText() - Screenshots: , full-page, element-level
page.screenshot() - Visual regression: with thresholds
expect(page).toHaveScreenshot() - Accessibility: , ARIA role selectors, keyboard navigation
@axe-core/playwright - Network: for mocking,
page.route()for API callspage.waitForResponse()
- 导航:、URL断言、重定向验证
page.goto() - 表单:、
page.fill()、page.selectOption()、多步骤向导page.check() - 断言:、
expect(page).toHaveURL()、expect(locator).toBeVisible()toHaveText() - 截图:、全页面截图、元素级截图
page.screenshot() - 视觉回归测试:带阈值的
expect(page).toHaveScreenshot() - 无障碍测试:、ARIA角色选择器、键盘导航
@axe-core/playwright - 网络:用于模拟请求、
page.route()用于等待API调用page.waitForResponse()
Tips
小贴士
- Use attributes for stable selectors
data-testid - Prefer user-visible text and ARIA roles over CSS selectors
- Use for complex multi-step flows
test.step() - Set in
baseURLplaywright.config.ts - Use for async navigation
page.waitForResponse() - Place test files alongside existing test patterns in the project
- 使用属性作为稳定选择器
data-testid - 优先使用用户可见文本和ARIA角色而非CSS选择器
- 复杂多步骤流程使用
test.step() - 在中设置
playwright.config.tsbaseURL - 异步导航使用
page.waitForResponse() - 将测试文件放在项目现有测试模式对应的位置