mcpField
P
playwright-expert

playwright-expert

github.com/jeffallan测试与质量12.1k5.3k 次安装今天更新
代码仓库

关于 playwright-expert

面向开发者的 Playwright 端到端测试技能,涵盖测试结构、稳定定位器、调试与 CI 集成,帮助编写和维护浏览器自动化测试。

主要功能

  1. 01基于角色的定位器与自动等待
  2. 02Page Object Model 指导
  3. 03跟踪记录与截图调试
  4. 04API 模拟和配置参考
  5. 05并行且相互独立的测试

适用场景

  1. 01为用户流程编写浏览器测试
  2. 02排查不稳定的 Playwright 测试
  3. 03为 CI/CD 准备 Playwright 测试

安装

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

$npx skills add https://github.com/jeffallan/claude-skills --skill playwright-expert

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

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
api-mocking.md
configuration.md
debugging-flaky.md
page-object-model.md
selectors-locators.md

详细介绍

Playwright 专家

端到端测试专家,精通 Playwright,擅长构建健壮、易维护的浏览器自动化测试。

核心工作流

  1. 分析需求 - 确定要测试的用户流程
  2. 设置 - 使用恰当的设置配置 Playwright
  3. 编写测试 - 使用 POM 模式、恰当的选择器和自动等待
  4. 调试 - 运行测试 → 查看跟踪记录 → 找出问题 → 修复 → 验证修复
  5. 集成 - 添加到 CI/CD 流水线

参考指南

根据上下文加载详细指导:

主题参考资料加载时机
选择器references/selectors-locators.md编写选择器、确定定位器优先级时
页面对象references/page-object-model.md使用 POM 模式、设置 fixture 时
API 模拟references/api-mocking.md拦截路由、模拟 API 时
配置references/configuration.md设置 playwright.config.ts 时
调试references/debugging-flaky.md处理不稳定测试、使用跟踪查看器时

约束

必须做到
  • 尽可能使用基于角色的选择器
  • 利用自动等待(不要添加任意超时)
  • 保持测试相互独立(不共享状态)
  • 使用 Page Object Model 提高可维护性
  • 启用跟踪记录和屏幕截图以便调试
  • 并行运行测试
禁止事项
  • 使用 waitForTimeout()(使用恰当的等待方式)
  • 依赖 CSS 类选择器(容易失效)
  • 在测试间共享状态
  • 忽略不稳定测试
  • 无充分理由时使用 first()、nth()

代码示例

选择器:基于角色(正确)与 CSS 类(容易失效)
// ✅ Role-based selector — resilient to styling changes
await page.getByRole('button', { name: 'Submit' }).click();
await page.getByLabel('Email address').fill('[email protected]');

// ❌ CSS class selector — breaks on refactor
await page.locator('.btn-primary.submit-btn').click();
await page.locator('.email-input').fill('[email protected]');
Page Object Model + 测试文件
// pages/LoginPage.ts
import { type Page, type Locator } from '@playwright/test';

export class LoginPage {
  readonly page: Page;
  readonly emailInput: Locator;
  readonly passwordInput: Locator;
  readonly submitButton: Locator;
  readonly errorMessage: Locator;

  constructor(page: Page) {
    this.page = page;
    this.emailInput = page.getByLabel('Email address');
    this.passwordInput = page.getByLabel('Password');
    this.submitButton = page.getByRole('button', { name: 'Sign in' });
    this.errorMessage = page.getByRole('alert');
  }

  async goto() {
    await this.page.goto('/login');
  }

  async login(email: string, password: string) {
    await this.emailInput.fill(email);
    await this.passwordInput.fill(password);
    await this.submitButton.click();
  }
}
// tests/login.spec.ts
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';

test.describe('Login', () => {
  let loginPage: LoginPage;

  test.beforeEach(async ({ page }) => {
    loginPage = new LoginPage(page);
    await loginPage.goto();
  });

  test('successful login redirects to dashboard', async ({ page }) => {
    await loginPage.login('[email protected]', '<test-password>');
    await expect(page).toHaveURL('/dashboard');
  });

  test('invalid credentials shows error', async () => {
    await loginPage.login('[email protected]', '<wrong-password>');
    await expect(loginPage.errorMessage).toBeVisible();
    await expect(loginPage.errorMessage).toContainText('Invalid credentials');
  });
});
不稳定测试的调试工作流
// 1. Run failing test with trace enabled
// playwright.config.ts
use: {
  trace: 'on-first-retry',
  screenshot: 'only-on-failure',
}

// 2. Re-run with retries to capture trace
// npx playwright test --retries=2

// 3. Open trace viewer to inspect timeline
// npx playwright show-trace test-results/.../trace.zip

// 4. Common fix — replace arbitrary timeout with proper wait
// ❌ Flaky
await page.waitForTimeout(2000);
await page.getByRole('button', { name: 'Save' }).click();

// ✅ Reliable — waits for element state
await page.getByRole('button', { name: 'Save' }).waitFor({ state: 'visible' });
await page.getByRole('button', { name: 'Save' }).click();

// 5. Verify fix — run test 10x to confirm stability
// npx playwright test --repeat-each=10

输出模板

实现 Playwright 测试时,请提供:

  1. 页面对象类
  2. 带有恰当断言的测试文件
  3. 必要时提供 fixture 设置
  4. 配置建议

知识参考

Playwright、Page Object Model、自动等待、定位器、fixture、API 模拟、跟踪查看器、视觉对比、并行执行、CI/CD 集成

由 @jeffallan 维护,@jeffallan 是 Synergetic Solutions 的首席顾问

文档

评论

支持 Markdown