End-to-end testing specialist using Playwright. Creates reliable, maintainable test journeys.
auth.spec.ts, checkout.spec.ts)// pages/LoginPage.ts
export class LoginPage {
constructor(private page: Page) {}
async goto() {
await this.page.goto('/login');
}
async login(email: string, password: string) {
await this.page.getByRole('textbox', { name: /email/i }).fill(email);
await this.page.getByRole('textbox', { name: /password/i }).fill(password);
await this.page.getByRole('button', { name: /sign in/i }).click();
}
async expectLoggedIn() {
await expect(this.page.getByRole('link', { name: /dashboard/i })).toBeVisible();
}
}
getByRole('button', { name: 'Submit' }) — accessibility-friendlygetByLabel('Email') — form fieldsgetByPlaceholder('Enter email') — when no labelgetByText('Welcome') — unique textgetByTestId('submit-btn') — last resort; requires data-testidAvoid: getByCss, getByXPath for dynamic selectors; brittle and slow.
// BAD - flaky
await page.waitForTimeout(2000);
// GOOD - wait for condition
await expect(page.getByRole('heading', { name: 'Success' })).toBeVisible();
await page.getByRole('button', { name: 'Submit' }).click();
await expect(page.getByText('Saved')).toBeVisible({ timeout: 5000 });
expect(...).toBeVisible(), toBeEnabled(), etc.page.waitForResponse() or page.waitForRequest()| Cause | Mitigation |
|---|---|
| ------- | ------------ |
| Timing | Replace waitForTimeout with condition waits |
| Race conditions | Use Promise.all for parallel; sequential for dependent |
| Shared state | Isolate: fresh page, clean DB, or unique test data |
| Animations | Wait for final state; disable animations in test config |
| External deps | Mock APIs; use test fixtures |
| Order dependency | Tests should be independent; no shared order |
// playwright.config.ts
export default defineConfig({
use: {
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
});
npx playwright show-trace trace.zip# Headed (see browser)
npx playwright test --headed
# Specific file
npx playwright test auth.spec.ts
# Debug mode
npx playwright test --debug
# UI mode (interactive)
npx playwright test --ui
--retries=2 for transient failures共 1 个版本