Playwright e2e testing

From findscripter/everything-skills · View interactive version

Playwright e2e testing is an AI agent skill from the GitHub repository findscripter/everything-skills. 当需要为 Web 应用搭建或扩展端到端(E2E)自动化测试时使用;用 Playwright 完成安装配置、编写测试脚本、视觉回归、跨浏览器与移动端模拟、并接入 CI/CD,产出可运行的测试套件与流水线;不适用于单元/接口测试、纯手工探索测试或非浏览器场景;触发词:Playwright、E2E、端到端测试、跨浏览器、视觉回归、浏览器自动化、CI 测试

How to use this skill

Skill file

何时使用

当你要为 Web 应用建立或扩展浏览器端到端(E2E)自动化测试时使用,典型场景:

不该用的边界:

步骤

  1. 环境搭建:安装 Playwright,初始化配置,建立 tests/ 目录,安装浏览器内核。
  2. 测试设计:梳理关键流程,规划测试数据与 fixtures,按页面建立 Page Object,避免选择器散落。
  3. 编写实现:写测试脚本与断言,用自动等待(web-first assertions)代替硬编码 sleep,处理动态内容与异常。
  4. 浏览器能力:按需开启 headless、截图、录像、trace 追踪、移动端模拟。
  5. 视觉回归:生成基线图,加 toHaveScreenshot 断言,设置容差阈值,审查差异。
  6. 跨浏览器:在 config 的 projects 中配置多内核与移动设备,对比结果。
  7. 接入 CI/CD:编写流水线,分片并行,上传 report/trace 产物,配置失败通知。

指令

# 1. 初始化(自动生成 config、示例用例、CI 模板)
npm init playwright@latest

# 2. 仅安装浏览器内核(已有项目)
npx playwright install --with-deps

# 3. 运行 / 调试
npx playwright test                 # 全量
npx playwright test --project=chromium
npx playwright test --ui            # UI 模式交互调试
npx playwright test --debug         # Inspector 单步
npx playwright codegen <url>        # 录制生成脚本

# 4. 报告与追踪
npx playwright show-report
npx playwright show-trace trace.zip

# 5. 视觉回归基线(首次或刻意更新)
npx playwright test --update-snapshots

示例

playwright.config.ts —— 跨浏览器 + 失败追踪:

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

export default defineConfig({
  testDir: './tests',
  fullyParallel: true,
  retries: process.env.CI ? 2 : 0,
  reporter: 'html',
  use: {
    baseURL: 'http://localhost:3000',
    trace: 'on-first-retry',     // 仅重试时收集 trace,降低开销
    screenshot: 'only-on-failure',
  },
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'firefox',  use: { ...devices['Desktop Firefox'] } },
    { name: 'webkit',   use: { ...devices['Desktop Safari'] } },
    { name: 'mobile',   use: { ...devices['Pixel 5'] } },
  ],
});

测试脚本 —— 自动等待 + 视觉回归:

import { test, expect } from '@playwright/test';

test('登录后跳转到仪表盘', async ({ page }) => {
  await page.goto('/login');
  await page.getByLabel('用户名').fill('demo');
  await page.getByLabel('密码').fill('secret');
  await page.getByRole('button', { name: '登录' }).click();

  // web-first 断言,自动重试等待,无需手写 sleep
  await expect(page.getByRole('heading', { name: '仪表盘' })).toBeVisible();
  await expect(page).toHaveScreenshot('dashboard.png', { maxDiffPixelRatio: 0.01 });
});

CI/CD(GitHub Actions):

name: e2e
on: [push, pull_request]
jobs:
  test:
    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 --with-deps
      - run: npx playwright test
      - uses: actions/upload-artifact@v4
        if: ${{ !cancelled() }}
        with:
          name: playwright-report
          path: playwright-report/
          retention-days: 7

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT 许可证),已按「技能大典」规范适配重写并补充可执行命令与代码示例。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.