Communitygithub.com

YepAPI/skills

Playwright 端到端测试最佳实践:页面对象模型、定位器策略、CI 集成、视觉回归与并行执行。

skills 是什么?

本技能提供了基于 Playwright 的端到端测试开发与执行的最佳实践指南。它涵盖了从定位器选择(优先使用 getByRole、getByLabel 等可访问性角色定位器,避免 CSS 选择器)、页面对象模型(POM)封装(如 LoginPage.goto()、LoginPage.login()),到测试隔离、自动等待和 web 优先断言等核心规则。该技能还指导如何在 CI 环境中配置重试机制、保存失败追踪与截图,并支持视觉回归测试(通过 toHaveScreenshot() 自动比对截图)。同时强调避免手动延时、测试相互依赖、直接调用第三方服务以及忽略移动端视口等常见陷阱。支持所有 Playwright 运行环境(如 Chromium、Mobile 设备),默认启用并行执行以提升测试效率。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/e2e-testing

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

端到端测试

规则

  • 使用 Playwright 进行端到端测试——跨浏览器、自动等待、内置测试运行器、优秀的 CI 支持
  • 定位器优先级:getByRole() > getByLabel() > getByPlaceholder() > getByTestId() > CSS 选择器
  • 页面对象模型:将页面交互封装在类中——LoginPage.login(email, password)
  • 测试隔离:每个测试以全新状态开始——使用 beforeEach 进行设置,避免测试间依赖
  • 自动等待:Playwright 自动等待元素——不要添加手动 waitForTimeout() 调用
  • 断言:使用 expect(locator) web 优先断言——toBeVisible()、toHaveText()、toHaveURL()
  • 并行执行:默认并行运行测试——确保测试之间没有共享的可变状态
  • CI:在无头模式下运行,配置重试次数(CI 中设置 retries: 2),失败时上传追踪
  • 视觉回归:expect(page).toHaveScreenshot()——使用 --update-snapshots 更新基线

模式

// 页面对象模型
class LoginPage {
  constructor(private page: Page) {}
  async goto() { await this.page.goto("/login"); }
  async login(email: string, password: string) {
    await this.page.getByLabel("邮箱").fill(email);
    await this.page.getByLabel("密码").fill(password);
    await this.page.getByRole("button", { name: "登录" }).click();
    await this.page.waitForURL("/dashboard");
  }
}

// playwright.config.ts
export default defineConfig({
  retries: process.env.CI ? 2 : 0,
  use: { trace: "on-first-retry", screenshot: "only-on-failure" },
  projects: [
    { name: "chromium", use: { ...devices["Desktop Chrome"] } },
    { name: "mobile", use: { ...devices["Pixel 5"] } },
  ],
});

避免

  • page.waitForTimeout() —— 使用自动等待或 waitForURL()、waitForResponse()
  • 为交互元素使用 CSS 选择器 —— 使用基于角色的定位器以获得弹性
  • 依赖于其他测试状态的测试 —— 每个测试必须独立可运行
  • 直接测试第三方服务 —— 使用 page.route() 模拟外部 API
  • 跳过移动端视口 —— 在你的项目矩阵中至少测试一个移动设备

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能