generate
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGenerate Playwright Tests
生成Playwright测试
Generate production-ready Playwright tests from a user story, URL, component name, or feature description.
根据用户故事、URL、组件名称或功能描述生成可用于生产环境的Playwright测试。
Input
输入
$ARGUMENTS"user can log in with email and password""the checkout flow""src/components/UserProfile.tsx""the search page with filters"
$ARGUMENTS"用户可以使用邮箱和密码登录""结账流程""src/components/UserProfile.tsx""带筛选器的搜索页面"
Steps
步骤
1. Understand the Target
1. 明确测试目标
Parse to determine:
$ARGUMENTS- User story: Extract the behavior to verify
- Component path: Read the component source code
- Page/URL: Identify the route and its elements
- Feature name: Map to relevant app areas
解析 以确定:
$ARGUMENTS- 用户故事:提取需要验证的行为
- 组件路径:读取组件源代码
- 页面/URL:识别路由及其元素
- 功能名称:映射到应用的相关区域
2. Explore the Codebase
2. 探索代码库
Use the subagent to gather context:
Explore- Read for
playwright.config.ts,testDir,baseURLprojects - Check existing tests in for patterns, fixtures, and conventions
testDir - If a component path is given, read the component to understand its props, states, and interactions
- Check for existing page objects in
pages/ - Check for existing fixtures in
fixtures/ - Check for auth setup (or
auth.setup.tsconfig)storageState
使用 子代理收集上下文:
Explore- 读取 获取
playwright.config.ts、testDir、baseURL配置projects - 检查 中的现有测试,了解测试模式、fixture和约定
testDir - 如果提供了组件路径,读取组件以理解其props、状态和交互逻辑
- 检查 目录下的现有页面对象
pages/ - 检查 目录下的现有fixture
fixtures/ - 检查认证设置(或
auth.setup.ts配置)storageState
3. Select Templates
3. 选择模板
Check in this plugin for matching patterns:
templates/| If testing... | Load template from |
|---|---|
| Login/auth flow | |
| CRUD operations | |
| Checkout/payment | |
| Search/filter UI | |
| Form submission | |
| Dashboard/data | |
| Settings page | |
| Onboarding flow | |
| API endpoints | |
| Accessibility | |
Adapt the template to the specific app — replace with actual selectors, URLs, and data.
{{placeholders}}查看此插件的 目录,匹配对应模式:
templates/| 测试场景... | 加载模板路径 |
|---|---|
| 登录/认证流程 | |
| CRUD操作 | |
| 结账/支付流程 | |
| 搜索/筛选UI | |
| 表单提交 | |
| 仪表盘/数据页面 | |
| 设置页面 | |
| 新用户引导流程 | |
| API接口 | |
| 可访问性 | |
根据具体应用调整模板——将 替换为实际的选择器、URL和数据。
{{placeholders}}4. Generate the Test
4. 生成测试用例
Follow these rules:
Structure:
typescript
import { test, expect } from '@playwright/test';
// Import custom fixtures if the project uses them
test.describe('Feature Name', () => {
// Group related behaviors
test('should <expected behavior>', async ({ page }) => {
// Arrange: navigate, set up state
// Act: perform user action
// Assert: verify outcome
});
});Locator priority (use the first that works):
- — buttons, links, headings, form elements
getByRole() - — form fields with labels
getByLabel() - — non-interactive text content
getByText() - — inputs with placeholder text
getByPlaceholder() - — when semantic options aren't available
getByTestId()
Assertions — always web-first:
typescript
// GOOD — auto-retries
await expect(page.getByRole('heading')).toBeVisible();
await expect(page.getByRole('alert')).toHaveText('Success');
// BAD — no retry
const text = await page.textContent('.msg');
expect(text).toBe('Success');Never use:
page.waitForTimeout()- or
page.$(selector)page.$$(selector) - Bare CSS selectors unless absolutely necessary
- for things locators can do
page.evaluate()
Always include:
- Descriptive test names that explain the behavior
- Error/edge case tests alongside happy path
- Proper on every Playwright call
await - -relative navigation (
baseURLnotpage.goto('/'))page.goto('http://...')
遵循以下规则:
结构:
typescript
import { test, expect } from '@playwright/test';
// 如果项目使用自定义fixture,导入它们
test.describe('功能名称', () => {
// 分组相关行为
test('应该<预期行为>', async ({ page }) => {
// 准备:导航到页面,设置状态
// 执行:执行用户操作
// 断言:验证结果
});
});定位器优先级(优先使用第一个可用的):
- —— 按钮、链接、标题、表单元素
getByRole() - —— 带标签的表单字段
getByLabel() - —— 非交互式文本内容
getByText() - —— 带占位符的输入框
getByPlaceholder() - —— 当语义化选项不可用时使用
getByTestId()
断言 —— 始终使用Web-first断言:
typescript
// 推荐——自动重试
await expect(page.getByRole('heading')).toBeVisible();
await expect(page.getByRole('alert')).toHaveText('Success');
// 不推荐——无重试机制
const text = await page.textContent('.msg');
expect(text).toBe('Success');禁止使用:
page.waitForTimeout()- 或
page.$(selector)page.$$(selector) - 除非绝对必要,否则不要使用纯CSS选择器
- 对于定位器可以完成的操作,不要使用
page.evaluate()
必须包含:
- 描述性的测试名称,解释测试的行为
- 除了正常流程测试外,还要包含错误/边界情况测试
- 每个Playwright调用都要正确使用
await - 相对于 的导航(使用
baseURL而非page.goto('/'))page.goto('http://...')
5. Match Project Conventions
5. 匹配项目约定
- If project uses TypeScript → generate
.spec.ts - If project uses JavaScript → generate with
.spec.jsimportsrequire() - If project has page objects → use them instead of inline locators
- If project has custom fixtures → import and use them
- If project has a test data directory → create test data files there
- 如果项目使用TypeScript → 生成 文件
.spec.ts - 如果项目使用JavaScript → 生成 文件,使用
.spec.js导入require() - 如果项目使用页面对象 → 使用页面对象而非内联定位器
- 如果项目有自定义fixture → 导入并使用它们
- 如果项目有测试数据目录 → 在该目录下创建测试数据文件
6. Generate Supporting Files (If Needed)
6. 生成支持文件(如有需要)
- Page object: If the test touches 5+ unique locators on one page, create a page object
- Fixture: If the test needs shared setup (auth, data), create or extend a fixture
- Test data: If the test uses structured data, create a JSON file in
test-data/
- 页面对象:如果测试在一个页面上涉及5个以上唯一定位器,创建页面对象
- Fixture:如果测试需要共享设置(认证、数据),创建或扩展fixture
- 测试数据:如果测试使用结构化数据,在 目录下创建JSON文件
test-data/
7. Verify
7. 验证
Run the generated test:
bash
npx playwright test <generated-file> --reporter=listIf it fails:
- Read the error
- Fix the test (not the app)
- Run again
- If it's an app issue, report it to the user
运行生成的测试:
bash
npx playwright test <generated-file> --reporter=list如果测试失败:
- 读取错误信息
- 修复测试(而非修改应用)
- 重新运行
- 如果是应用问题,向用户报告
Output
输出
- Generated test file(s) with path
- Any supporting files created (page objects, fixtures, data)
- Test run result
- Coverage note: what behaviors are now tested
- 生成的测试文件及路径
- 创建的所有支持文件(页面对象、fixture、数据文件)
- 测试运行结果
- 覆盖说明:现在已测试哪些行为