Playwright Skill
Purpose
Write effective end-to-end tests using Playwright for web applications.
Core Concepts
Page Object Model
Encapsulate page interactions in reusable classes.
class LoginPage {
constructor(private page: Page) {}
async login(email: string, password: string) {
await this.page.fill('[data-testid="email"]', email);
await this.page.fill('[data-testid="password"]', password);
await this.page.click('[data-testid="submit"]');
}
}
Locator Strategies (Priority Order)
data-testidattributes (most stable)- Accessible roles and names
- Text content
- CSS selectors (least preferred)
Waiting Strategies
- Auto-wait for elements (built-in)
waitForSelectorfor dynamic contentwaitForResponsefor API callswaitForLoadStatefor navigation
Best Practices
- Use Test IDs - Add
data-testidfor test stability - Isolate Tests - Each test should be independent
- Mock Strategically - Mock external services, not internal logic
- Screenshot on Failure - Capture state for debugging
- Parallel Execution - Use workers for speed
Common Patterns
// Wait for API response
await page.waitForResponse(resp =>
resp.url().includes('/api/users') && resp.status() === 200
);
// Screenshot on failure
test.afterEach(async ({ page }, testInfo) => {
if (testInfo.status !== testInfo.expectedStatus) {
await page.screenshot({ path: `failure-${testInfo.title}.png` });
}
});
Converted and distributed by TomeVault — claim your Tome and manage your conversions.