[ PROMPT_NODE_27738 ]
Playwright Skill API 参考
[ SKILL_DOCUMENTATION ]
# Playwright 技能 - 完整 API 参考
本文档包含全面的 Playwright API 参考和高级模式。有关快速启动执行模式,请参阅 [SKILL.md](SKILL.md)。
## 目录
- [安装与设置](#installation--setup)
- [核心模式](#core-patterns)
- [选择器与定位器](#selectors--locators)
- [常见操作](#common-actions)
- [等待策略](#waiting-strategies)
- [断言](#assertions)
- [页面对象模型 (POM)](#page-object-model-pom)
- [网络与 API 测试](#network--api-testing)
- [身份验证与会话管理](#authentication--session-management)
- [视觉测试](#visual-testing)
- [移动端测试](#mobile-testing)
- [调试](#debugging)
- [性能测试](#performance-testing)
- [并行执行](#parallel-execution)
- [数据驱动测试](#data-driven-testing)
- [无障碍测试](#accessibility-testing)
- [CI/CD 集成](#cicd-integration)
- [最佳实践](#best-practices)
- [常见模式与解决方案](#common-patterns--solutions)
- [故障排除](#troubleshooting)
## 安装与设置
### 先决条件
在使用此技能前,请确保 Playwright 可用:
bash
# 检查是否安装了 Playwright
npm list playwright 2>/dev/null || echo "Playwright not installed"
# 安装(如果需要)
cd ~/.claude/skills/playwright-skill
npm run setup
### 基础配置
创建 `playwright.config.ts`:
typescript
import { defineConfig, devices } from '@playwright/test';
export default defineConfig({
testDir: './tests',
fullyParallel: true,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 2 : 0,
workers: process.env.CI ? 1 : undefined,
reporter: 'html',
use: {
baseURL: 'http://localhost:3000',
trace: 'on-first-retry',
screenshot: 'only-on-failure',
video: 'retain-on-failure',
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
webServer: {
command: 'npm run start',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
});
## 核心模式
### 基础浏览器自动化
javascript
const { chromium } = require('playwright');
(async () => {
// 启动浏览器
const browser = await chromium.launch({
headless: false, // 设置为 true 以开启无头模式
slowMo: 50 // 将操作减慢 50ms
});
const context = await browser.newContext({
viewport: { width: 1280, height: 720 },
userAgent: