playwright-expert
Jeffallan/claude-skills
適用於使用 Playwright 編寫端到端測試、設定測試基礎架構,或除錯不穩定的瀏覽器測試。適用於瀏覽器自動化、端到端測試、頁面物件模型、測試不穩定性及視覺測試。關鍵字:Playwright、端到端、瀏覽器測試、自動化、頁面物件。
...展開全部關於playwright-expert
「playwright-expert 」這項技能旨在協助使用者運用 Playwright 框架撰寫與管理端到端(E2E)測試。它能解決諸如設定測試基礎架構、除錯不穩定測試,以及確保瀏覽器自動化測試易於維護等常見挑戰。 透過運用這項技能,使用者能簡化為網頁應用程式編寫穩健、可靠且快速的測試流程,這對於 CI/CD 工作流程和持續測試至關重要。此外,它亦著重於提升測試的品質與可維護性,協助測試人員解決諸如瀏覽器行為不穩定、效能瓶頸,以及與各類網路服務的整合問題。
「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





首頁
