mcpField
P
playwright-expert

playwright-expert

github.com/farmage测试与质量223297 次安装5 周前更新
代码仓库

关于 playwright-expert

面向需要构建可维护浏览器自动化和端到端测试的团队的 Playwright 技能,涵盖测试设计、调试与 CI/CD 集成,并强调 Page Object Model 和自动等待。

主要功能

  1. 01端到端测试流程
  2. 02Page Object Model 指导
  3. 03基于角色的选择器与自动等待
  4. 04API 模拟指导
  5. 05使用 Trace 和截图调试
  6. 06并行执行测试

适用场景

  1. 01测试浏览器用户流程
  2. 02组织可维护的 Playwright 测试
  3. 03排查不稳定的测试失败
  4. 04将测试接入 CI/CD 流水线

安装

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

$npx skills add https://github.com/farmage/opencode-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测试不稳定、使用追踪查看器时

约束

必须做到
  • 尽可能使用基于角色的选择器
  • 利用自动等待(不要添加随意设置的超时时间)
  • 保持测试彼此独立(不共享状态)
  • 使用页面对象模型,提升可维护性
  • 启用追踪记录和截图以便调试
  • 并行运行测试
切勿
  • 使用 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]');
页面对象模型 + 测试文件
// 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]', 'correct-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、页面对象模型、自动等待、定位器、fixture、API 模拟、追踪查看器、视觉对比、并行执行、CI/CD 集成

文档

评论

支持 Markdown