mcpField
P
playwright-testing

playwright-testing

github.com/laurigates测试与质量5999 次安装今天更新
代码仓库

关于 playwright-testing

Playwright Testing 是为编码 Agent 提供的 Playwright 端到端浏览器测试 Skill,涵盖跨浏览器、移动视口和视觉回归测试,并提供配置、编写、运行与调试指南。

主要功能

  1. 01覆盖 Chromium、Firefox 和 WebKit
  2. 02指导编写端到端浏览器测试
  3. 03涵盖网络请求模拟与视觉回归测试
  4. 04提供测试运行、调试和报告命令
  5. 05支持移动视口测试与并行执行

适用场景

  1. 01验证 Web 应用的登录流程
  2. 02在多个浏览器中运行端到端测试
  3. 03测试移动视口下的页面行为
  4. 04调试失败测试并查看报告

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/laurigates/claude-plugins --skill playwright-testing

安装后在 Claude Code 中输入/playwright-testing确认已加载。

附带文件REFERENCE.md

详细介绍

Playwright 测试

Playwright 是一款现代化的 Web 应用端到端测试框架。它提供可靠、快速的跨浏览器测试体验,并带来出色的开发者体验。

何时使用此技能

在以下情况下使用此技能……在以下情况下改用其他技能……
编写 E2E 浏览器测试编写单元测试(使用 vitest-testing)
在 Chromium、Firefox、WebKit 上测试测试 Python 代码(使用 python-testing)
设置视觉回归测试分析测试质量(使用 test-quality-analysis)
在 E2E 测试中模拟网络请求生成基于属性的测试(使用 property-based-testing)
测试移动设备视口仅测试 API 契约(使用 api-testing)

核心专长

  • 跨浏览器:在 Chromium、Firefox、WebKit(Safari)上测试
  • 可靠:自动等待、自动重试,不会出现不稳定的测试
  • 快速:并行执行、浏览器上下文隔离
  • 现代化:以 TypeScript 为先、使用 async/await、支持自动补全
  • 多平台:Windows、macOS、Linux

安装

bun create playwright                  # Initialize (recommended)
bun add --dev @playwright/test         # Or install manually
bunx playwright install                # Install browsers
bunx playwright install --with-deps    # With system deps (Linux)
bunx playwright --version              # Verify

配置(playwright.config.ts)

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './tests',
  timeout: 30000,
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',
  },
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },
  ],
});

基本命令

bunx playwright test                           # Run all tests
bunx playwright test tests/login.spec.ts       # Specific file
bunx playwright test --headed                  # See browser
bunx playwright test --debug                   # Debug mode
bunx playwright test --project=chromium        # Specific browser
bunx playwright test --ui                      # UI mode
bunx playwright codegen http://localhost:3000  # Record tests
bunx playwright show-report                    # Last report
bunx playwright show-trace trace.zip           # Trace viewer
bunx playwright test --update-snapshots        # Update snapshots

编写测试

基本测试结构
import { test, expect } from '@playwright/test';

test('basic test', async ({ page }) => {
  await page.goto('https://example.com');
  await expect(page).toHaveTitle(/Example/);
});

test.describe('login flow', () => {
  test('should login successfully', async ({ page }) => {
    await page.goto('/login');
    await page.fill('input[name="email"]', '[email protected]');
    await page.fill('input[name="password"]', 'password123');
    await page.click('button[type="submit"]');
    await expect(page).toHaveURL('/dashboard');
  });
});
选择器和定位器
// Text/Role selectors (recommended)
await page.getByText('Sign in').click();
await page.getByRole('button', { name: 'Submit' }).click();
await page.getByLabel('Email').fill('[email protected]');
await page.getByPlaceholder('Enter your name').fill('John');
await page.getByTestId('login-button').click();

// CSS/XPath selectors
await page.locator('.button-primary').click();
await page.locator('xpath=//button[text()="Submit"]').click();

// Chaining
await page.locator('.card').filter({ hasText: 'Product' }).getByRole('button').click();
关键断言
断言说明
toHaveTitle(title)页面标题
toHaveURL(url)页面 URL
toBeVisible()元素可见
toBeEnabled()元素已启用
toHaveText(text)元素文本
toContainText(text)部分文本
toHaveAttribute(name, value)属性值
toHaveClass(class)CSS 类
toHaveValue(value)输入值
toBeEmpty()输入为空
toHaveCount(n)元素数量
not.toBeDisabled()否定断言

Agent 优化

场景命令
快速测试bunx playwright test --reporter=line --bail
CI 测试bunx playwright test --reporter=github
单个浏览器bunx playwright test --project=chromium --reporter=line
调试失败的测试bunx playwright test --trace on --reporter=line
有头模式调试bunx playwright test --headed --debug

如需详细示例、高级模式和最佳实践,请参阅 REFERENCE.md。

在不带 GPU 的 CI 运行器上测试 WebGL 应用(CesiumJS、three.js、MapLibre):SwiftShader 标志、lhci 的 chromeFlags 字符串陷阱和帧率限制详见 无 GPU CI 环境中的 WebGL 应用。

参考资料

评论

支持 Markdown