playwright-testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePlaywright E2E Testing Skill
Playwright 端到端测试技能
For end-to-end testing of web applications with Playwright - cross-browser, fast, reliable.
Setup
配置
Installation
安装
bash
undefinedbash
undefinedNew project
新项目
npm init playwright@latest
npm init playwright@latest
Existing project
现有项目
npm install -D @playwright/test
npx playwright install
undefinednpm install -D @playwright/test
npx playwright install
undefinedConfiguration
配置文件
typescript
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './e2e',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: [
['html'],
['list'],
process.env.CI ? ['github'] : ['line'],
],
use: {
baseURL: process.env.BASE_URL || 'http://localhost:3000',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
projects: [
// Auth setup - runs once before all tests
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
dependencies: ['setup'],
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
dependencies: ['setup'],
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
dependencies: ['setup'],
},
// Mobile viewports
{
name: 'mobile-chrome',
use: { ...devices['Pixel 5'] },
dependencies: ['setup'],
},
{
name: 'mobile-safari',
use: { ...devices['iPhone 12'] },
dependencies: ['setup'],
},
],
// Start dev server before tests
webServer: {
command: 'npm run dev',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
timeout: 120 * 1000,
},
});typescript
// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './e2e',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: [
['html'],
['list'],
process.env.CI ? ['github'] : ['line'],
],
use: {
baseURL: process.env.BASE_URL || 'http://localhost:3000',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
projects: [
// 身份验证设置 - 在所有测试前运行一次
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
dependencies: ['setup'],
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
dependencies: ['setup'],
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
dependencies: ['setup'],
},
// 移动端视口
{
name: 'mobile-chrome',
use: { ...devices['Pixel 5'] },
dependencies: ['setup'],
},
{
name: 'mobile-safari',
use: { ...devices['iPhone 12'] },
dependencies: ['setup'],
},
],
// 测试前启动开发服务器
webServer: {
command: 'npm run dev',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
timeout: 120 * 1000,
},
});Project Structure
项目结构
project/
├── e2e/
│ ├── fixtures/
│ │ ├── auth.fixture.ts # Auth fixtures
│ │ └── test.fixture.ts # Extended test with fixtures
│ ├── pages/
│ │ ├── base.page.ts # Base page object
│ │ ├── login.page.ts # Login page object
│ │ ├── dashboard.page.ts # Dashboard page object
│ │ └── index.ts # Export all pages
│ ├── tests/
│ │ ├── auth.spec.ts # Auth tests
│ │ ├── dashboard.spec.ts # Dashboard tests
│ │ └── checkout.spec.ts # Checkout flow tests
│ ├── utils/
│ │ ├── helpers.ts # Test helpers
│ │ └── test-data.ts # Test data factories
│ └── auth.setup.ts # Global auth setup
├── playwright.config.ts
└── .auth/ # Stored auth state (gitignored)project/
├── e2e/
│ ├── fixtures/
│ │ ├── auth.fixture.ts # 身份验证夹具
│ │ └── test.fixture.ts # 扩展测试夹具
│ ├── pages/
│ │ ├── base.page.ts # 基础页面对象
│ │ ├── login.page.ts # 登录页面对象
│ │ ├── dashboard.page.ts # 控制台页面对象
│ │ └── index.ts # 导出所有页面
│ ├── tests/
│ │ ├── auth.spec.ts # 身份验证测试
│ │ ├── dashboard.spec.ts # 控制台测试
│ │ └── checkout.spec.ts # 结账流程测试
│ ├── utils/
│ │ ├── helpers.ts # 测试辅助函数
│ │ └── test-data.ts # 测试数据工厂
│ └── auth.setup.ts # 全局身份验证设置
├── playwright.config.ts
└── .auth/ # 存储的身份验证状态(已加入git忽略)Locator Strategy (Priority Order)
定位器策略(优先级顺序)
Use locators that mirror how users interact with the page:
typescript
// ✅ BEST: Role-based (accessible, resilient)
page.getByRole('button', { name: 'Submit' })
page.getByRole('textbox', { name: 'Email' })
page.getByRole('link', { name: 'Sign up' })
page.getByRole('heading', { name: 'Welcome' })
// ✅ GOOD: User-facing text
page.getByLabel('Email address')
page.getByPlaceholder('Enter your email')
page.getByText('Welcome back')
page.getByTitle('Profile settings')
// ✅ GOOD: Test IDs (stable, explicit)
page.getByTestId('submit-button')
page.getByTestId('user-avatar')
// ⚠️ AVOID: CSS selectors (brittle)
page.locator('.btn-primary')
page.locator('#submit')
// ❌ NEVER: XPath (extremely brittle)
page.locator('//div[@class="container"]/button[1]')使用与用户和页面交互方式一致的定位器:
typescript
// ✅ 最佳:基于角色(可访问、稳定)
page.getByRole('button', { name: 'Submit' })
page.getByRole('textbox', { name: 'Email' })
page.getByRole('link', { name: 'Sign up' })
page.getByRole('heading', { name: 'Welcome' })
// ✅ 良好:用户可见文本
page.getByLabel('Email address')
page.getByPlaceholder('Enter your email')
page.getByText('Welcome back')
page.getByTitle('Profile settings')
// ✅ 良好:测试ID(稳定、明确)
page.getByTestId('submit-button')
page.getByTestId('user-avatar')
// ⚠️ 避免:CSS选择器(脆弱)
page.locator('.btn-primary')
page.locator('#submit')
// ❌ 禁止:XPath(极度脆弱)
page.locator('//div[@class="container"]/button[1]')Chaining Locators
链式定位器
typescript
// Narrow down to specific section
const form = page.getByRole('form', { name: 'Login' });
await form.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
await form.getByRole('button', { name: 'Submit' }).click();
// Filter within a list
const productCard = page.getByTestId('product-card')
.filter({ hasText: 'Pro Plan' });
await productCard.getByRole('button', { name: 'Buy' }).click();typescript
// 缩小到特定区域
const form = page.getByRole('form', { name: 'Login' });
await form.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
await form.getByRole('button', { name: 'Submit' }).click();
// 在列表中筛选
const productCard = page.getByTestId('product-card')
.filter({ hasText: 'Pro Plan' });
await productCard.getByRole('button', { name: 'Buy' }).click();Page Object Model
页面对象模型
Base Page
基础页面
typescript
// e2e/pages/base.page.ts
import { Page, Locator } from '@playwright/test';
export abstract class BasePage {
constructor(protected page: Page) {}
async navigate(path: string = '/') {
await this.page.goto(path);
}
async waitForPageLoad() {
await this.page.waitForLoadState('networkidle');
}
// Common elements
get header() {
return this.page.getByRole('banner');
}
get footer() {
return this.page.getByRole('contentinfo');
}
// Common actions
async clickNavLink(name: string) {
await this.header.getByRole('link', { name }).click();
}
}typescript
// e2e/pages/base.page.ts
import { Page, Locator } from '@playwright/test';
export abstract class BasePage {
constructor(protected page: Page) {}
async navigate(path: string = '/') {
await this.page.goto(path);
}
async waitForPageLoad() {
await this.page.waitForLoadState('networkidle');
}
// 通用元素
get header() {
return this.page.getByRole('banner');
}
get footer() {
return this.page.getByRole('contentinfo');
}
// 通用操作
async clickNavLink(name: string) {
await this.header.getByRole('link', { name }).click();
}
}Page Implementation
页面实现
typescript
// e2e/pages/login.page.ts
import { Page, expect } from '@playwright/test';
import { BasePage } from './base.page';
export class LoginPage extends BasePage {
readonly emailInput: Locator;
readonly passwordInput: Locator;
readonly submitButton: Locator;
readonly errorMessage: Locator;
constructor(page: Page) {
super(page);
this.emailInput = page.getByLabel('Email');
this.passwordInput = page.getByLabel('Password');
this.submitButton = page.getByRole('button', { name: 'Sign in' });
this.errorMessage = page.getByRole('alert');
}
async goto() {
await this.navigate('/login');
}
async login(email: string, password: string) {
await this.emailInput.fill(email);
await this.passwordInput.fill(password);
await this.submitButton.click();
}
async expectError(message: string) {
await expect(this.errorMessage).toContainText(message);
}
async expectLoggedIn() {
await expect(this.page).toHaveURL(/.*dashboard/);
}
}typescript
// e2e/pages/dashboard.page.ts
import { Page, Locator, expect } from '@playwright/test';
import { BasePage } from './base.page';
export class DashboardPage extends BasePage {
readonly welcomeHeading: Locator;
readonly userMenu: Locator;
readonly logoutButton: Locator;
constructor(page: Page) {
super(page);
this.welcomeHeading = page.getByRole('heading', { name: /welcome/i });
this.userMenu = page.getByTestId('user-menu');
this.logoutButton = page.getByRole('button', { name: 'Logout' });
}
async goto() {
await this.navigate('/dashboard');
}
async logout() {
await this.userMenu.click();
await this.logoutButton.click();
}
async expectWelcome(name: string) {
await expect(this.welcomeHeading).toContainText(name);
}
}typescript
// e2e/pages/login.page.ts
import { Page, expect } from '@playwright/test';
import { BasePage } from './base.page';
export class LoginPage extends BasePage {
readonly emailInput: Locator;
readonly passwordInput: Locator;
readonly submitButton: Locator;
readonly errorMessage: Locator;
constructor(page: Page) {
super(page);
this.emailInput = page.getByLabel('Email');
this.passwordInput = page.getByLabel('Password');
this.submitButton = page.getByRole('button', { name: 'Sign in' });
this.errorMessage = page.getByRole('alert');
}
async goto() {
await this.navigate('/login');
}
async login(email: string, password: string) {
await this.emailInput.fill(email);
await this.passwordInput.fill(password);
await this.submitButton.click();
}
async expectError(message: string) {
await expect(this.errorMessage).toContainText(message);
}
async expectLoggedIn() {
await expect(this.page).toHaveURL(/.*dashboard/);
}
}typescript
// e2e/pages/dashboard.page.ts
import { Page, Locator, expect } from '@playwright/test';
import { BasePage } from './base.page';
export class DashboardPage extends BasePage {
readonly welcomeHeading: Locator;
readonly userMenu: Locator;
readonly logoutButton: Locator;
constructor(page: Page) {
super(page);
this.welcomeHeading = page.getByRole('heading', { name: /welcome/i });
this.userMenu = page.getByTestId('user-menu');
this.logoutButton = page.getByRole('button', { name: 'Logout' });
}
async goto() {
await this.navigate('/dashboard');
}
async logout() {
await this.userMenu.click();
await this.logoutButton.click();
}
async expectWelcome(name: string) {
await expect(this.welcomeHeading).toContainText(name);
}
}Export All Pages
导出所有页面
typescript
// e2e/pages/index.ts
export { BasePage } from './base.page';
export { LoginPage } from './login.page';
export { DashboardPage } from './dashboard.page';typescript
// e2e/pages/index.ts
export { BasePage } from './base.page';
export { LoginPage } from './login.page';
export { DashboardPage } from './dashboard.page';Authentication
身份验证
Global Auth Setup
全局身份验证设置
typescript
// e2e/auth.setup.ts
import { test as setup, expect } from '@playwright/test';
import path from 'path';
const authFile = path.join(__dirname, '../.auth/user.json');
setup('authenticate', async ({ page }) => {
// Go to login page
await page.goto('/login');
// Login with test credentials
await page.getByLabel('Email').fill(process.env.TEST_USER_EMAIL!);
await page.getByLabel('Password').fill(process.env.TEST_USER_PASSWORD!);
await page.getByRole('button', { name: 'Sign in' }).click();
// Wait for auth to complete
await expect(page).toHaveURL(/.*dashboard/);
// Save auth state for reuse
await page.context().storageState({ path: authFile });
});typescript
// e2e/auth.setup.ts
import { test as setup, expect } from '@playwright/test';
import path from 'path';
const authFile = path.join(__dirname, '../.auth/user.json');
setup('authenticate', async ({ page }) => {
// 前往登录页
await page.goto('/login');
// 使用测试凭证登录
await page.getByLabel('Email').fill(process.env.TEST_USER_EMAIL!);
await page.getByLabel('Password').fill(process.env.TEST_USER_PASSWORD!);
await page.getByRole('button', { name: 'Sign in' }).click();
// 等待身份验证完成
await expect(page).toHaveURL(/.*dashboard/);
// 保存身份验证状态以供复用
await page.context().storageState({ path: authFile });
});Using Auth in Tests
在测试中使用身份验证
typescript
// playwright.config.ts
export default defineConfig({
projects: [
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
storageState: '.auth/user.json',
},
dependencies: ['setup'],
},
],
});typescript
// playwright.config.ts
export default defineConfig({
projects: [
{ name: 'setup', testMatch: /.*\.setup\.ts/ },
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
storageState: '.auth/user.json',
},
dependencies: ['setup'],
},
],
});Tests Without Auth
无需身份验证的测试
typescript
// e2e/tests/public.spec.ts
import { test } from '@playwright/test';
// Override to skip auth
test.use({ storageState: { cookies: [], origins: [] } });
test('homepage loads for anonymous users', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('heading', { name: 'Welcome' })).toBeVisible();
});typescript
// e2e/tests/public.spec.ts
import { test } from '@playwright/test';
// 覆盖设置以跳过身份验证
test.use({ storageState: { cookies: [], origins: [] } });
test('匿名用户可加载首页', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('heading', { name: 'Welcome' })).toBeVisible();
});Writing Tests
编写测试
Basic Test Structure
基础测试结构
typescript
// e2e/tests/auth.spec.ts
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages';
test.describe('Authentication', () => {
test.beforeEach(async ({ page }) => {
// Skip stored auth for login tests
await page.context().clearCookies();
});
test('successful login redirects to dashboard', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login('user@example.com', 'password123');
await loginPage.expectLoggedIn();
});
test('invalid credentials show error', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login('wrong@example.com', 'wrongpass');
await loginPage.expectError('Invalid email or password');
});
test('empty form shows validation errors', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.submitButton.click();
await expect(page.getByText('Email is required')).toBeVisible();
await expect(page.getByText('Password is required')).toBeVisible();
});
});typescript
// e2e/tests/auth.spec.ts
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages';
test.describe('Authentication', () => {
test.beforeEach(async ({ page }) => {
// 登录测试跳过存储的身份验证
await page.context().clearCookies();
});
test('成功登录后跳转到控制台', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login('user@example.com', 'password123');
await loginPage.expectLoggedIn();
});
test('无效凭证显示错误信息', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.login('wrong@example.com', 'wrongpass');
await loginPage.expectError('Invalid email or password');
});
test('空表单显示验证错误', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.submitButton.click();
await expect(page.getByText('Email is required')).toBeVisible();
await expect(page.getByText('Password is required')).toBeVisible();
});
});User Flow Tests
用户流程测试
typescript
// e2e/tests/checkout.spec.ts
import { test, expect } from '@playwright/test';
test.describe('Checkout Flow', () => {
test('complete purchase flow', async ({ page }) => {
// 1. Browse products
await page.goto('/products');
await page.getByTestId('product-card')
.filter({ hasText: 'Pro Plan' })
.getByRole('button', { name: 'Add to cart' })
.click();
// 2. View cart
await page.getByRole('link', { name: 'Cart' }).click();
await expect(page.getByText('Pro Plan')).toBeVisible();
await expect(page.getByTestId('cart-total')).toContainText('$29.99');
// 3. Checkout
await page.getByRole('button', { name: 'Checkout' }).click();
// 4. Fill payment (use Stripe test card)
const stripeFrame = page.frameLocator('iframe[name*="stripe"]');
await stripeFrame.getByPlaceholder('Card number').fill('4242424242424242');
await stripeFrame.getByPlaceholder('MM / YY').fill('12/30');
await stripeFrame.getByPlaceholder('CVC').fill('123');
// 5. Complete purchase
await page.getByRole('button', { name: 'Pay now' }).click();
// 6. Verify success
await expect(page).toHaveURL(/.*success/);
await expect(page.getByRole('heading', { name: 'Thank you' })).toBeVisible();
});
});typescript
// e2e/tests/checkout.spec.ts
import { test, expect } from '@playwright/test';
test.describe('Checkout Flow', () => {
test('完成购买流程', async ({ page }) => {
// 1. 浏览产品
await page.goto('/products');
await page.getByTestId('product-card')
.filter({ hasText: 'Pro Plan' })
.getByRole('button', { name: 'Add to cart' })
.click();
// 2. 查看购物车
await page.getByRole('link', { name: 'Cart' }).click();
await expect(page.getByText('Pro Plan')).toBeVisible();
await expect(page.getByTestId('cart-total')).toContainText('$29.99');
// 3. 结账
await page.getByRole('button', { name: 'Checkout' }).click();
// 4. 填写支付信息(使用Stripe测试卡)
const stripeFrame = page.frameLocator('iframe[name*="stripe"]');
await stripeFrame.getByPlaceholder('Card number').fill('4242424242424242');
await stripeFrame.getByPlaceholder('MM / YY').fill('12/30');
await stripeFrame.getByPlaceholder('CVC').fill('123');
// 5. 完成购买
await page.getByRole('button', { name: 'Pay now' }).click();
// 6. 验证成功
await expect(page).toHaveURL(/.*success/);
await expect(page.getByRole('heading', { name: 'Thank you' })).toBeVisible();
});
});Assertions
断言
Web-First Assertions (Auto-Wait)
Web优先断言(自动等待)
typescript
// ✅ These wait and retry automatically
await expect(page.getByRole('button')).toBeVisible();
await expect(page.getByRole('button')).toBeEnabled();
await expect(page.getByRole('button')).toHaveText('Submit');
await expect(page).toHaveURL('/dashboard');
await expect(page).toHaveTitle(/Dashboard/);
// ❌ Avoid manual waits
await page.waitForTimeout(3000); // NEVER do thistypescript
// ✅ 这些断言会自动等待并重试
await expect(page.getByRole('button')).toBeVisible();
await expect(page.getByRole('button')).toBeEnabled();
await expect(page.getByRole('button')).toHaveText('Submit');
await expect(page).toHaveURL('/dashboard');
await expect(page).toHaveTitle(/Dashboard/);
// ❌ 避免手动等待
await page.waitForTimeout(3000); // 绝对不要这样做Soft Assertions
软断言
typescript
// Continue test even if assertion fails
await expect.soft(page.getByTestId('price')).toHaveText('$29.99');
await expect.soft(page.getByTestId('stock')).toHaveText('In Stock');
// Fail at end if any soft assertions failedtypescript
// 即使断言失败也继续执行测试
await expect.soft(page.getByTestId('price')).toHaveText('$29.99');
await expect.soft(page.getByTestId('stock')).toHaveText('In Stock');
// 如果有任何软断言失败,在测试结束时标记失败Common Assertions
常用断言
typescript
// Visibility
await expect(locator).toBeVisible();
await expect(locator).toBeHidden();
await expect(locator).toBeAttached();
// Text content
await expect(locator).toHaveText('exact text');
await expect(locator).toContainText('partial');
await expect(locator).toHaveValue('input value');
// State
await expect(locator).toBeEnabled();
await expect(locator).toBeDisabled();
await expect(locator).toBeChecked();
await expect(locator).toBeFocused();
// Count
await expect(locator).toHaveCount(5);
// Page
await expect(page).toHaveURL('/dashboard');
await expect(page).toHaveTitle('Dashboard | App');
await expect(page).toHaveScreenshot('dashboard.png');typescript
// 可见性
await expect(locator).toBeVisible();
await expect(locator).toBeHidden();
await expect(locator).toBeAttached();
// 文本内容
await expect(locator).toHaveText('exact text');
await expect(locator).toContainText('partial');
await expect(locator).toHaveValue('input value');
// 状态
await expect(locator).toBeEnabled();
await expect(locator).toBeDisabled();
await expect(locator).toBeChecked();
await expect(locator).toBeFocused();
// 数量
await expect(locator).toHaveCount(5);
// 页面
await expect(page).toHaveURL('/dashboard');
await expect(page).toHaveTitle('Dashboard | App');
await expect(page).toHaveScreenshot('dashboard.png');Mocking & Network
模拟与网络
Mock API Responses
模拟API响应
typescript
test('shows error when API fails', async ({ page }) => {
// Mock API to return error
await page.route('**/api/users', (route) => {
route.fulfill({
status: 500,
body: JSON.stringify({ error: 'Server error' }),
});
});
await page.goto('/users');
await expect(page.getByText('Failed to load users')).toBeVisible();
});
test('displays user data from API', async ({ page }) => {
// Mock successful response
await page.route('**/api/users', (route) => {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify([
{ id: 1, name: 'John Doe', email: 'john@example.com' },
{ id: 2, name: 'Jane Doe', email: 'jane@example.com' },
]),
});
});
await page.goto('/users');
await expect(page.getByText('John Doe')).toBeVisible();
await expect(page.getByText('Jane Doe')).toBeVisible();
});typescript
test('API失败时显示错误', async ({ page }) => {
// 模拟API返回错误
await page.route('**/api/users', (route) => {
route.fulfill({
status: 500,
body: JSON.stringify({ error: 'Server error' }),
});
});
await page.goto('/users');
await expect(page.getByText('Failed to load users')).toBeVisible();
});
test('显示来自API的用户数据', async ({ page }) => {
// 模拟成功响应
await page.route('**/api/users', (route) => {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify([
{ id: 1, name: 'John Doe', email: 'john@example.com' },
{ id: 2, name: 'Jane Doe', email: 'jane@example.com' },
]),
});
});
await page.goto('/users');
await expect(page.getByText('John Doe')).toBeVisible();
await expect(page.getByText('Jane Doe')).toBeVisible();
});Wait for API Calls
等待API调用
typescript
test('submits form and shows success', async ({ page }) => {
await page.goto('/contact');
// Fill form
await page.getByLabel('Name').fill('John');
await page.getByLabel('Email').fill('john@example.com');
await page.getByLabel('Message').fill('Hello!');
// Wait for API call on submit
const responsePromise = page.waitForResponse('**/api/contact');
await page.getByRole('button', { name: 'Send' }).click();
const response = await responsePromise;
expect(response.status()).toBe(200);
await expect(page.getByText('Message sent!')).toBeVisible();
});typescript
test('提交表单并显示成功信息', async ({ page }) => {
await page.goto('/contact');
// 填写表单
await page.getByLabel('Name').fill('John');
await page.getByLabel('Email').fill('john@example.com');
await page.getByLabel('Message').fill('Hello!');
// 提交时等待API调用
const responsePromise = page.waitForResponse('**/api/contact');
await page.getByRole('button', { name: 'Send' }).click();
const response = await responsePromise;
expect(response.status()).toBe(200);
await expect(page.getByText('Message sent!')).toBeVisible();
});Visual Testing
可视化测试
typescript
// Full page screenshot
await expect(page).toHaveScreenshot('homepage.png');
// Element screenshot
await expect(page.getByTestId('chart')).toHaveScreenshot('chart.png');
// With options
await expect(page).toHaveScreenshot('dashboard.png', {
maxDiffPixels: 100,
mask: [page.getByTestId('timestamp')], // Ignore dynamic content
});typescript
// 整页截图
await expect(page).toHaveScreenshot('homepage.png');
// 元素截图
await expect(page.getByTestId('chart')).toHaveScreenshot('chart.png');
// 带选项
await expect(page).toHaveScreenshot('dashboard.png', {
maxDiffPixels: 100,
mask: [page.getByTestId('timestamp')], // 忽略动态内容
});CI/CD Integration
CI/CD集成
GitHub Actions
GitHub Actions
yaml
undefinedyaml
undefined.github/workflows/e2e.yml
.github/workflows/e2e.yml
name: E2E Tests
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Install Playwright browsers
run: npx playwright install --with-deps chromium
- name: Run E2E tests
run: npx playwright test --project=chromium
env:
BASE_URL: ${{ secrets.STAGING_URL }}
TEST_USER_EMAIL: ${{ secrets.TEST_USER_EMAIL }}
TEST_USER_PASSWORD: ${{ secrets.TEST_USER_PASSWORD }}
- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-report
path: playwright-report/
retention-days: 7undefinedname: E2E Tests
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Install Playwright browsers
run: npx playwright install --with-deps chromium
- name: Run E2E tests
run: npx playwright test --project=chromium
env:
BASE_URL: ${{ secrets.STAGING_URL }}
TEST_USER_EMAIL: ${{ secrets.TEST_USER_EMAIL }}
TEST_USER_PASSWORD: ${{ secrets.TEST_USER_PASSWORD }}
- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-report
path: playwright-report/
retention-days: 7undefinedRun Specific Tests
运行特定测试
bash
undefinedbash
undefinedRun all tests
运行所有测试
npx playwright test
npx playwright test
Run specific file
运行特定文件
npx playwright test e2e/tests/auth.spec.ts
npx playwright test e2e/tests/auth.spec.ts
Run tests with tag
运行带标签的测试
npx playwright test --grep @critical
npx playwright test --grep @critical
Run in headed mode (debug)
带界面模式运行(调试)
npx playwright test --headed
npx playwright test --headed
Run specific browser
运行特定浏览器的测试
npx playwright test --project=chromium
npx playwright test --project=chromium
Debug mode
调试模式
npx playwright test --debug
npx playwright test --debug
Show HTML report
显示HTML报告
npx playwright show-report
---npx playwright show-report
---Test Data
测试数据
Factories
工厂函数
typescript
// e2e/utils/test-data.ts
import { faker } from '@faker-js/faker';
export const createUser = (overrides = {}) => ({
email: faker.internet.email(),
password: faker.internet.password({ length: 12 }),
name: faker.person.fullName(),
...overrides,
});
export const createProduct = (overrides = {}) => ({
name: faker.commerce.productName(),
price: faker.commerce.price({ min: 10, max: 100 }),
description: faker.commerce.productDescription(),
...overrides,
});typescript
// e2e/utils/test-data.ts
import { faker } from '@faker-js/faker';
export const createUser = (overrides = {}) => ({
email: faker.internet.email(),
password: faker.internet.password({ length: 12 }),
name: faker.person.fullName(),
...overrides,
});
export const createProduct = (overrides = {}) => ({
name: faker.commerce.productName(),
price: faker.commerce.price({ min: 10, max: 100 }),
description: faker.commerce.productDescription(),
...overrides,
});Environment Variables
环境变量
bash
undefinedbash
undefined.env.test
.env.test
BASE_URL=http://localhost:3000
TEST_USER_EMAIL=test@example.com
TEST_USER_PASSWORD=testpassword123
---BASE_URL=http://localhost:3000
TEST_USER_EMAIL=test@example.com
TEST_USER_PASSWORD=testpassword123
---Debugging
调试
Trace Viewer
跟踪查看器
typescript
// Enable in config for failures
use: {
trace: 'on-first-retry',
}
// View traces
npx playwright show-trace trace.ziptypescript
// 在配置中为失败的测试启用
use: {
trace: 'on-first-retry',
}
// 查看跟踪
npx playwright show-trace trace.zipDebug Mode
调试模式
bash
undefinedbash
undefinedStep through test
逐步执行测试
npx playwright test --debug
npx playwright test --debug
Pause at specific point
在特定位置暂停
await page.pause(); // In test code
undefinedawait page.pause(); // 在测试代码中
undefinedVS Code Extension
VS Code扩展
Install "Playwright Test for VS Code" for:
- Run tests from editor
- Debug with breakpoints
- Pick locators visually
- Watch mode
安装“Playwright Test for VS Code”以获得以下功能:
- 从编辑器运行测试
- 使用断点调试
- 可视化选择定位器
- 监听模式
Dead Link Detection (REQUIRED)
死链检测(必填)
Every project MUST include dead link detection tests. Run these on every deployment.
每个项目必须包含死链检测测试。 在每次部署时运行这些测试。
Link Validator Test
链接验证器测试
typescript
// e2e/tests/links.spec.ts
import { test, expect } from '@playwright/test';
const PAGES_TO_CHECK = ['/', '/about', '/pricing', '/blog', '/contact'];
test.describe('Dead Link Detection', () => {
for (const pagePath of PAGES_TO_CHECK) {
test(`no dead links on ${pagePath}`, async ({ page, request }) => {
await page.goto(pagePath);
// Get all links on the page
const links = await page.locator('a[href]').all();
const hrefs = await Promise.all(
links.map(link => link.getAttribute('href'))
);
// Filter to internal and absolute external links
const uniqueLinks = [...new Set(hrefs.filter(Boolean))] as string[];
for (const href of uniqueLinks) {
// Skip mailto, tel, and anchor links
if (href.startsWith('mailto:') || href.startsWith('tel:') || href.startsWith('#')) {
continue;
}
// Build full URL
const url = href.startsWith('http') ? href : new URL(href, page.url()).href;
// Check link status
const response = await request.get(url, {
timeout: 10000,
ignoreHTTPSErrors: true,
});
expect(
response.ok(),
`Dead link found on ${pagePath}: ${href} returned ${response.status()}`
).toBeTruthy();
}
});
}
});typescript
// e2e/tests/links.spec.ts
import { test, expect } from '@playwright/test';
const PAGES_TO_CHECK = ['/', '/about', '/pricing', '/blog', '/contact'];
test.describe('Dead Link Detection', () => {
for (const pagePath of PAGES_TO_CHECK) {
test(`页面${pagePath}上无死链`, async ({ page, request }) => {
await page.goto(pagePath);
// 获取页面上的所有链接
const links = await page.locator('a[href]').all();
const hrefs = await Promise.all(
links.map(link => link.getAttribute('href'))
);
// 筛选内部链接和绝对外部链接
const uniqueLinks = [...new Set(hrefs.filter(Boolean))] as string[];
for (const href of uniqueLinks) {
// 跳过mailto、tel和锚点链接
if (href.startsWith('mailto:') || href.startsWith('tel:') || href.startsWith('#')) {
continue;
}
// 构建完整URL
const url = href.startsWith('http') ? href : new URL(href, page.url()).href;
// 检查链接状态
const response = await request.get(url, {
timeout: 10000,
ignoreHTTPSErrors: true,
});
expect(
response.ok(),
`在页面${pagePath}上发现死链:${href}返回状态码${response.status()}`
).toBeTruthy();
}
});
}
});Comprehensive Link Crawler
全面链接爬虫
typescript
// e2e/tests/site-links.spec.ts
import { test, expect, Page, APIRequestContext } from '@playwright/test';
interface LinkResult {
url: string;
status: number;
foundOn: string;
}
async function checkAllLinks(
page: Page,
request: APIRequestContext,
startUrl: string
): Promise<LinkResult[]> {
const visited = new Set<string>();
const results: LinkResult[] = [];
const toVisit = [startUrl];
const baseUrl = new URL(startUrl).origin;
while (toVisit.length > 0) {
const currentUrl = toVisit.pop()!;
if (visited.has(currentUrl)) continue;
visited.add(currentUrl);
try {
await page.goto(currentUrl);
const links = await page.locator('a[href]').all();
for (const link of links) {
const href = await link.getAttribute('href');
if (!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) {
continue;
}
const fullUrl = href.startsWith('http') ? href : new URL(href, currentUrl).href;
// Check link
const response = await request.get(fullUrl, {
timeout: 10000,
ignoreHTTPSErrors: true,
});
results.push({
url: fullUrl,
status: response.status(),
foundOn: currentUrl,
});
// Add internal links to queue
if (fullUrl.startsWith(baseUrl) && !visited.has(fullUrl)) {
toVisit.push(fullUrl);
}
}
} catch (error) {
results.push({
url: currentUrl,
status: 0,
foundOn: 'navigation',
});
}
}
return results;
}
test('no dead links on entire site', async ({ page, request, baseURL }) => {
const results = await checkAllLinks(page, request, baseURL!);
const deadLinks = results.filter(r => r.status >= 400 || r.status === 0);
if (deadLinks.length > 0) {
console.error('Dead links found:');
deadLinks.forEach(link => {
console.error(` ${link.url} (${link.status}) - found on ${link.foundOn}`);
});
}
expect(deadLinks, `Found ${deadLinks.length} dead links`).toHaveLength(0);
});typescript
// e2e/tests/site-links.spec.ts
import { test, expect, Page, APIRequestContext } from '@playwright/test';
interface LinkResult {
url: string;
status: number;
foundOn: string;
}
async function checkAllLinks(
page: Page,
request: APIRequestContext,
startUrl: string
): Promise<LinkResult[]> {
const visited = new Set<string>();
const results: LinkResult[] = [];
const toVisit = [startUrl];
const baseUrl = new URL(startUrl).origin;
while (toVisit.length > 0) {
const currentUrl = toVisit.pop()!;
if (visited.has(currentUrl)) continue;
visited.add(currentUrl);
try {
await page.goto(currentUrl);
const links = await page.locator('a[href]').all();
for (const link of links) {
const href = await link.getAttribute('href');
if (!href || href.startsWith('#') || href.startsWith('mailto:') || href.startsWith('tel:')) {
continue;
}
const fullUrl = href.startsWith('http') ? href : new URL(href, currentUrl).href;
// 检查链接
const response = await request.get(fullUrl, {
timeout: 10000,
ignoreHTTPSErrors: true,
});
results.push({
url: fullUrl,
status: response.status(),
foundOn: currentUrl,
});
// 将内部链接加入队列
if (fullUrl.startsWith(baseUrl) && !visited.has(fullUrl)) {
toVisit.push(fullUrl);
}
}
} catch (error) {
results.push({
url: currentUrl,
status: 0,
foundOn: 'navigation',
});
}
}
return results;
}
test('整个站点无死链', async ({ page, request, baseURL }) => {
const results = await checkAllLinks(page, request, baseURL!);
const deadLinks = results.filter(r => r.status >= 400 || r.status === 0);
if (deadLinks.length > 0) {
console.error('发现死链:');
deadLinks.forEach(link => {
console.error(` ${link.url} (${link.status}) - 在${link.foundOn}页面发现`);
});
}
expect(deadLinks, `发现${deadLinks.length}个死链`).toHaveLength(0);
});Image Link Validation
图片链接验证
typescript
// e2e/tests/images.spec.ts
import { test, expect } from '@playwright/test';
test('no broken images on homepage', async ({ page, request }) => {
await page.goto('/');
const images = await page.locator('img[src]').all();
for (const img of images) {
const src = await img.getAttribute('src');
if (!src) continue;
const url = src.startsWith('http') ? src : new URL(src, page.url()).href;
// Skip data URLs
if (url.startsWith('data:')) continue;
const response = await request.get(url);
expect(
response.ok(),
`Broken image: ${src}`
).toBeTruthy();
// Verify it's actually an image
const contentType = response.headers()['content-type'];
expect(
contentType?.startsWith('image/'),
`${src} is not an image (${contentType})`
).toBeTruthy();
}
});typescript
// e2e/tests/images.spec.ts
import { test, expect } from '@playwright/test';
test('首页无损坏图片', async ({ page, request }) => {
await page.goto('/');
const images = await page.locator('img[src]').all();
for (const img of images) {
const src = await img.getAttribute('src');
if (!src) continue;
const url = src.startsWith('http') ? src : new URL(src, page.url()).href;
// 跳过data URL
if (url.startsWith('data:')) continue;
const response = await request.get(url);
expect(
response.ok(),
`损坏图片:${src}`
).toBeTruthy();
// 验证确实是图片
const contentType = response.headers()['content-type'];
expect(
contentType?.startsWith('image/'),
`${src}不是图片(${contentType})`
).toBeTruthy();
}
});CI Integration for Link Checking
链接检查的CI集成
yaml
undefinedyaml
undefined.github/workflows/link-check.yml
.github/workflows/link-check.yml
name: Link Check
on:
schedule:
- cron: '0 6 * * 1' # Weekly on Monday
push:
branches: [main]
jobs:
link-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install chromium
- run: npx playwright test e2e/tests/links.spec.ts --project=chromium
env:
BASE_URL: ${{ secrets.PRODUCTION_URL }}
---name: Link Check
on:
schedule:
- cron: '0 6 * * 1' # 每周一早上6点
push:
branches: [main]
jobs:
link-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx playwright install chromium
- run: npx playwright test e2e/tests/links.spec.ts --project=chromium
env:
BASE_URL: ${{ secrets.PRODUCTION_URL }}
---Anti-Patterns
反模式
- Hardcoded waits - Use auto-waiting assertions instead
- CSS/XPath selectors - Use role/text/testid locators
- Testing third-party sites - Mock external dependencies
- Shared state between tests - Each test must be isolated
- Missing awaits - Use ESLint rule
no-floating-promises - Flaky time-based tests - Mock dates/times
- Testing implementation details - Test user-visible behavior
- Huge test files - Split by feature/page
- 硬编码等待 - 使用自动等待的断言替代
- CSS/XPath选择器 - 使用角色/文本/测试ID定位器
- 测试第三方站点 - 模拟外部依赖
- 测试间共享状态 - 每个测试必须独立
- 缺少await - 使用ESLint规则
no-floating-promises - 基于时间的不稳定测试 - 模拟日期/时间
- 测试实现细节 - 测试用户可见的行为
- 过大的测试文件 - 按功能/页面拆分
Quick Reference
快速参考
bash
undefinedbash
undefinedInstall
安装
npm init playwright@latest
npm init playwright@latest
Run tests
运行测试
npx playwright test
npx playwright test --headed
npx playwright test --project=chromium
npx playwright test --grep @smoke
npx playwright test
npx playwright test --headed
npx playwright test --project=chromium
npx playwright test --grep @smoke
Debug
调试
npx playwright test --debug
npx playwright show-report
npx playwright show-trace trace.zip
npx playwright test --debug
npx playwright show-report
npx playwright show-trace trace.zip
Generate tests
生成测试
npx playwright codegen localhost:3000
undefinednpx playwright codegen localhost:3000
undefinedPackage.json Scripts
Package.json脚本
json
{
"scripts": {
"test:e2e": "playwright test",
"test:e2e:headed": "playwright test --headed",
"test:e2e:debug": "playwright test --debug",
"test:e2e:report": "playwright show-report",
"test:e2e:codegen": "playwright codegen"
}
}json
{
"scripts": {
"test:e2e": "playwright test",
"test:e2e:headed": "playwright test --headed",
"test:e2e:debug": "playwright test --debug",
"test:e2e:report": "playwright show-report",
"test:e2e:codegen": "playwright codegen"
}
}