playwright-expert
Jeffallan/claude-skills
适用于使用 Playwright 编写端到端测试、搭建测试基础设施或调试不稳定的浏览器测试。可用于浏览器自动化、端到端测试、页面对象模型、测试不稳定性以及视觉测试。关键词:Playwright、端到端、浏览器测试、自动化、页面对象。
...展开全部简介playwright-expert
“playwright-expert ”技能旨在协助用户使用 Playwright 框架编写和管理端到端(E2E)测试。它解决了测试基础设施搭建、不稳定测试的调试以及确保浏览器自动化测试可维护性等常见难题。 通过运用此技能,用户可以简化为 Web 应用程序编写健壮、可靠且高效的测试的过程,这对 CI/CD 工作流和持续测试至关重要。它还致力于提高测试的质量和可维护性,帮助测试人员解决浏览器行为不稳定、性能瓶颈以及与各种 Web 服务的集成问题。
“playwright-expert ”技能的主要特点包括精通页面对象模型(POM)模式、API 模拟、视觉回归测试以及 Playwright 的测试配置。该技能帮助用户实施最佳实践,例如利用自动等待机制避免任意超时、编写独立测试,以及使用基于角色的选择器来确保可靠的测试执行。 此外,该技能还提供了关于调试处理的建议,支持启用跟踪和截图功能以快速分析测试结果,并支持并行执行测试以优化性能。
该技能主要面向资深 QA 自动化工程师,尤其是那些在浏览器测试和 Playwright 方面拥有丰富经验的人员。对于负责构建测试基础设施、调试不稳定测试、实现 POM 模式以及执行 API 模拟和视觉回归测试的用户而言,该技能极具价值。 从事 Web 应用程序端到端测试的开发人员和测试人员,特别是在 CI/CD 环境中工作的,将发现本技能极具价值。
常见问题
如何为端到端测试配置 Playwright?
您可以按照“references/configuration.md”文档中的配置指南来设置 Playwright,其中包括配置 playwright.config.ts 以确保测试正确执行。
什么是页面对象模型(POM),它如何帮助测试?
页面对象模型(POM)是一种用于创建代表网页的测试类的设计模式。它通过为页面交互提供一个集中位置,有助于组织和维护测试,从而使测试更易于阅读和维护。
我可以将这项技能集成到我的 CI/CD 管道中吗?
可以,本技能在设计时就考虑了与 CI/CD 的集成。使用 Playwright 编写的测试可以轻松集成到您的 CI/CD 管道中,从而确保持续测试并加快反馈周期。
使用 Playwright 时需要注意哪些性能问题?
建议并行运行测试以优化测试性能。Playwright 还支持自动等待功能,可减少不必要的超时,从而提高整体测试效率。
该技能对测试选择器有何限制?
是的,建议避免依赖 CSS 类名等不稳定的选择器。相反,应尽可能使用基于角色的选择器,以确保测试的稳定性和可维护性。
Playwright Expert
E2E testing specialist with deep expertise in Playwright for robust, maintainable browser automation.
Core Workflow
- Analyze requirements - Identify user flows to test
- Setup - Configure Playwright with proper settings
- Write tests - Use POM pattern, proper selectors, auto-waiting
- Debug - Run test → check trace → identify issue → fix → verify fix
- Integrate - Add to CI/CD pipeline
Reference Guide
Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| Selectors | references/selectors-locators.md | Writing selectors, locator priority |
| Page Objects | references/page-object-model.md | POM patterns, fixtures |
| API Mocking | references/api-mocking.md | Route interception, mocking |
| Configuration | references/configuration.md | playwright.config.ts setup |
| Debugging | references/debugging-flaky.md | Flaky tests, trace viewer |
Constraints
MUST DO
- Use role-based selectors when possible
- Leverage auto-waiting (don't add arbitrary timeouts)
- Keep tests independent (no shared state)
- Use Page Object Model for maintainability
- Enable traces/screenshots for debugging
- Run tests in parallel
MUST NOT DO
- Use
waitForTimeout()(use proper waits) - Rely on CSS class selectors (brittle)
- Share state between tests
- Ignore flaky tests
- Use
first(),nth()without good reason
Code Examples
Selector: Role-based (correct) vs CSS class (brittle)
// ✅ Role-based selector — resilient to styling changesawait page.getByRole('button', { name: 'Submit' }).click();await page.getByLabel('Email address').fill('[email protected]');// ❌ CSS class selector — breaks on refactorawait page.locator('.btn-primary.submit-btn').click();await page.locator('.email-input').fill('[email protected]');
Page Object Model + Test File
// pages/LoginPage.tsimport { type Page, type Locator } from '@playwright/test';export class LoginPage { readonly page: Page; readonly emailInput: Locator; readonly passwordInput: Locator; readonly submitButton: Locator; readonly errorMessage: Locator; constructor(page: Page) { this.page = page; this.emailInput = page.getByLabel('Email address'); this.passwordInput = page.getByLabel('Password'); this.submitButton = page.getByRole('button', { name: 'Sign in' }); this.errorMessage = page.getByRole('alert'); } async goto() { await this.page.goto('/login'); } async login(email: string, password: string) { await this.emailInput.fill(email); await this.passwordInput.fill(password); await this.submitButton.click(); }}
// tests/login.spec.tsimport { test, expect } from '@playwright/test';import { LoginPage } from '../pages/LoginPage';test.describe('Login', () => { let loginPage: LoginPage; test.beforeEach(async ({ page }) => { loginPage = new LoginPage(page); await loginPage.goto(); }); test('successful login redirects to dashboard', async ({ page }) => { await loginPage.login('[email protected]', 'correct-password'); await expect(page).toHaveURL('/dashboard'); }); test('invalid credentials shows error', async () => { await loginPage.login('[email protected]', 'wrong-password'); await expect(loginPage.errorMessage).toBeVisible(); await expect(loginPage.errorMessage).toContainText('Invalid credentials'); });});
Debugging Workflow for Flaky Tests
// 1. Run failing test with trace enabled// playwright.config.tsuse: { trace: 'on-first-retry', screenshot: 'only-on-failure',}// 2. Re-run with retries to capture trace// npx playwright test --retries=2// 3. Open trace viewer to inspect timeline// npx playwright show-trace test-results/.../trace.zip// 4. Common fix — replace arbitrary timeout with proper wait// ❌ Flakyawait page.waitForTimeout(2000);await page.getByRole('button', { name: 'Save' }).click();// ✅ Reliable — waits for element stateawait page.getByRole('button', { name: 'Save' }).waitFor({ state: 'visible' });await page.getByRole('button', { name: 'Save' }).click();// 5. Verify fix — run test 10x to confirm stability// npx playwright test --repeat-each=10
Output Templates
When implementing Playwright tests, provide:
- Page Object classes
- Test files with proper assertions
- Fixture setup if needed
- Configuration recommendations
Knowledge Reference
Playwright, Page Object Model, auto-waiting, locators, fixtures, API mocking, trace viewer, visual comparisons, parallel execution, CI/CD integration
Documentation





首页
