選項

產生 Playwright 測試。當使用者說「撰寫測試」、「產生測試」、「為...新增測試」、「測試此元件」、「端到端測試」、 「為...建立測試」、「測試此頁面」或「測試此功能」時使用。

...展開全部
10
更新時間 2026-08-25

關於 generate

這項技能「generate」能根據使用者故事、URL、元件路徑或功能描述,生成可直接投入生產環境的 Playwright 端到端測試。 它解決了手動撰寫可靠且符合約定的瀏覽器測試所面臨的問題:在生成任何內容之前,該技能會先探索現有專案以了解其測試目錄、基礎 URL、測試數據及頁面物件約定,因此產出的內容能契合目標程式碼庫,而非僅是通用範本。

此工作流程會解析待測試內容,透過「探索」(Explore) 子代理程式讀取 Playwright 設定檔與現有測試,選取相符的範本(認證、CRUD、結帳、搜尋、表單、儀表板、設定、入門、API、無障礙),並運用實際的選擇器與資料進行調整。Generate d 測試遵循嚴格的品質規範:定位器優先順序會優先採用 getByRole、getByLabel 及其他語義定位器,而非純 CSS;採用以網頁為先的自動重試斷言;並明確禁止使用如 waitForTimeout、page.$ 選擇器及不必要的 page.evaluate 等反模式。 它符合專案在 TypeScript 與 JavaScript 選擇、頁面物件以及固定裝置(fixtures)方面的規範,並可在必要時透過 generate 支援頁面物件、固定裝置及測試資料檔案。最後,它會使用 Playwright CLI 執行 generate d 測試,並針對失敗情況進行迭代處理。

本工具旨在協助使用 Playwright 的網頁開發人員與品質保證工程師,使其能以自身專案的風格,建立一致且易於維護的端到端測試覆蓋率 generate。 應用場景包括測試登入與結帳流程、表單驗證、搜尋與篩選介面,以及元件行為。此技能隨附 SKILL.md 及 patterns.md 檔案,其中收錄了針對身分驗證、CRUD 及驗證流程的具體測試範例。

常見問題

它接受哪些輸入?

使用者故事、元件檔案路徑、頁面或 URL,或是功能名稱 — 例如「使用者可透過電子郵件和密碼登入」或「src/components/UserProfile.tsx」。

它強制採用何種定位器與斷言風格?

它優先採用語義定位器(依序為 getByRole、getByLabel、getByText、getByPlaceholder、getByTestId),並始終使用以網頁為優先的自動重試斷言,而非手動文字擷取。

它會避免哪些反模式?

它絕不使用 page.waitForTimeout()、page.$/page.$$ 選擇器、除非萬不得已否則不使用裸 CSS 選擇器,以及對於定位器可處理的事項,絕不使用 page.evaluate()。

它會驗證自己編寫的測試嗎?

是的——它會執行 generate d 測試(使用 'npx playwright test --reporter=list'),讀取任何錯誤,並修正測試而非應用程式,將真正的應用程式問題回報給您。

它會符合我專案的規範嗎?

它會讀取 playwright.config.ts 及現有測試,以匹配 TypeScript 與 JavaScript 的差異、現有的頁面物件、自訂固定裝置以及 test-data 目錄。

所有檔案

2 個檔案 SKILL.md 4.4 KB Viewpatterns.md 5.7 KB View
在 GitHub 上查看

Generate production-ready Playwright tests from a user story, URL, component name, or feature description.

Input

$ARGUMENTS contains what to test. Examples:

  • "user can log in with email and password"
  • "the checkout flow"
  • "src/components/UserProfile.tsx"
  • "the search page with filters"

Steps

1. Understand the Target

Parse $ARGUMENTS to determine:

  • User story: Extract the behavior to verify
  • Component path: Read the component source code
  • Page/URL: Identify the route and its elements
  • Feature name: Map to relevant app areas

2. Explore the Codebase

Use the Explore subagent to gather context:

  • Read playwright.config.ts for testDir, baseURL, projects
  • Check existing tests in testDir for patterns, fixtures, and conventions
  • If a component path is given, read the component to understand its props, states, and interactions
  • Check for existing page objects in pages/
  • Check for existing fixtures in fixtures/
  • Check for auth setup (auth.setup.ts or storageState config)

3. Select Templates

Check templates/ in this plugin for matching patterns:

If testing...Load template from
Login/auth flow../pw/templates/auth/login.md
CRUD operationstemplates/crud/
Checkout/paymenttemplates/checkout/
Search/filter UItemplates/search/
Form submissiontemplates/forms/
Dashboard/datatemplates/dashboard/
Settings pagetemplates/settings/
Onboarding flowtemplates/onboarding/
API endpointstemplates/api/
Accessibilitytemplates/accessibility/

Adapt the template to the specific app — replace {{placeholders}} with actual selectors, URLs, and data.

4. Generate the Test

Follow these rules:

Structure:

import { test, expect } from '@playwright/test';// Import custom fixtures if the project uses themtest.describe('Feature Name', () => {  // Group related behaviors  test('should <expected behavior>', async ({ page }) => {    // Arrange: navigate, set up state    // Act: perform user action    // Assert: verify outcome  });});

Locator priority (use the first that works):

  1. getByRole() — buttons, links, headings, form elements
  2. getByLabel() — form fields with labels
  3. getByText() — non-interactive text content
  4. getByPlaceholder() — inputs with placeholder text
  5. getByTestId() — when semantic options aren't available

Assertions — always web-first:

// GOOD — auto-retriesawait expect(page.getByRole('heading')).toBeVisible();await expect(page.getByRole('alert')).toHaveText('Success');// BAD — no retryconst text = await page.textContent('.msg');expect(text).toBe('Success');

Never use:

  • page.waitForTimeout()
  • page.$(selector) or page.$$(selector)
  • Bare CSS selectors unless absolutely necessary
  • page.evaluate() for things locators can do

Always include:

  • Descriptive test names that explain the behavior
  • Error/edge case tests alongside happy path
  • Proper await on every Playwright call
  • baseURL-relative navigation (page.goto('/') not page.goto('http://...'))

5. Match Project Conventions

  • If project uses TypeScript → generate .spec.ts
  • If project uses JavaScript → generate .spec.js with require() imports
  • If project has page objects → use them instead of inline locators
  • If project has custom fixtures → import and use them
  • If project has a test data directory → create test data files there

6. Generate Supporting Files (If Needed)

  • Page object: If the test touches 5+ unique locators on one page, create a page object
  • Fixture: If the test needs shared setup (auth, data), create or extend a fixture
  • Test data: If the test uses structured data, create a JSON file in test-data/

7. Verify

Run the generated test:

npx playwright test <generated-file> --reporter=list

If it fails:

  1. Read the error
  2. Fix the test (not the app)
  3. Run again
  4. If it's an app issue, report it to the user

Output

  • Generated test file(s) with path
  • Any supporting files created (page objects, fixtures, data)
  • Test run result
  • Coverage note: what behaviors are now tested

所有檔案

2 個檔案

安裝 generate

請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。

下載 ZIP

複製儲存庫並將技能檔案複製到您的專案中。

git clone https://github.com/alirezarezvani/claude-skills/blob/main/engineering-team/playwright-pro/skills/generate/SKILL.md # Copy SKILL.md to your .claude/skills/ directory

複製 複製
快速設定: 將技能資料夾複製到 .claude/skills/,Claude 會自動偵測並使用該技能

相關技能

playwright-cli
更新時間 2026-06-29
frontend-testing-best-practices
更新時間 2026-07-07
Playwright Browser Automation
更新時間 2026-06-29
playwright-generate-test
更新時間 2026-06-29
OR