Communitygithub.com

YepAPI/skills

Playwright 模式:頁面物件、定位器最佳實務、CI 整合、視覺回歸與平行執行。

skills 是什麼?

此技能提供了一套基於 Playwright 的端到端測試最佳實務指南。它詳細說明了有效的測試策略,包括使用頁面物件模型封裝頁面互動、根據可及性角色優先選擇定位器、確保測試隔離以避免狀態污染,以及利用 Playwright 內建的自動等待與 web-first 斷言來編寫可靠的測試。此技能也涵蓋了 CI 整合,例如在 CI 環境中設定重試機制(retries: 2)、啟用失敗追蹤,以及透過 `expect(page).toHaveScreenshot()` 實現視覺回歸測試。它支援多瀏覽器與行動裝置的平行執行,並明確指出了應避免的常見反模式,例如手動 `waitForTimeout` 或依賴其他測試狀態,從而幫助開發者建立堅固、高效能的測試套件。

相容平台~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-first 斷言 — 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.

相關技能