playwright-e2e-test

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Playwright E2E Test

Playwright E2E 测试

Goal

目标

Give the agent eyes on the real app. An agent that only reads code can only say "I think it works". A Playwright test runs the flow in a real browser and answers with a fact: green or red.
This skill produces one test for one critical flow — a minimal safety net, not a QA plan.
让 Agent 直接看到真实应用。一个只阅读代码的 Agent 只能说“我认为它能正常工作”。Playwright 测试会在真实浏览器中运行流程,并给出一个确定的事实:通过或失败。
此 skill 为一个关键流程创建一个测试——它是一张最小安全网,而不是一份 QA 计划。

Non-negotiable rules

不可妥协的规则

  1. Plan before code. Propose the test plan in plain language and STOP until the user approves it.
  2. Never weaken a test to make it pass. No editing assertions to match broken behavior, no
    test.skip
    , no
    test.fixme
    , no extra retries, no longer timeouts to hide a failure. A red test is reported, not silenced.
  3. The human decides. When the test fails, diagnose and propose — the user chooses whether to fix the app or the test.
  4. Never run against production. Local or staging only. If the flow writes to a real database, say so before the first run.
  1. 先规划,后编码。 用通俗语言提出测试计划,然后停止操作,直到用户批准。
  2. 绝不削弱测试来让它通过。 不得修改断言以适配错误行为,不得使用
    test.skip
    ,不得使用
    test.fixme
    ,不得增加重试,也不得延长超时时间以掩盖失败。失败的测试应被报告,而不是被掩盖。
  3. 由人类决定。 测试失败时,先进行诊断并提出建议——由用户选择修复应用还是测试。
  4. 绝不针对生产环境运行。 只能在本地或 staging 环境中运行。如果流程会写入真实数据库,必须在首次运行前说明。

Step 1 — Understand the flow

第 1 步——理解流程

  • Ask which flow to cover if the user did not name one. One flow per run of this skill.
  • Read the code that implements it (pages, components, the API it calls) and any project docs (
    CLAUDE.md
    ,
    README
    , PRD) to learn how the app starts and what "success" looks like.
  • Identify the visible success outcome a user would see (a heading, a confirmation message, a new row in a list). That outcome becomes the final assertion.
  • 如果用户没有指定流程,询问需要覆盖哪个流程。每次运行此 skill 只处理一个流程。
  • 阅读实现该流程的代码(页面、组件及其调用的 API)以及项目文档(
    CLAUDE.md
    、
    README
    、PRD),了解应用的启动方式和“成功”的具体表现。
  • 确定用户能够看到的成功结果(例如标题、确认消息或列表中的新行)。该结果将成为最终断言。

Step 2 — Propose the plan and STOP

第 2 步——提出计划并停止

Present, in plain language:
Flow: <name>
Start at: <url>
Steps:
  1. <user action> (e.g. click "Continue")
  2. ...
Final assertion: <what must be visible at the end, with its exact text>
Test data: <what it fills in, and whether it writes to a real database>
File: <path of the spec>
Wait for the user's OK or adjustments. Do not write code before that.
用通俗语言呈现:
Flow: <name>
Start at: <url>
Steps:
  1. <user action> (e.g. click "Continue")
  2. ...
Final assertion: <what must be visible at the end, with its exact text>
Test data: <what it fills in, and whether it writes to a real database>
File: <path of the spec>
等待用户确认或提出调整。在得到确认前不要编写代码。

Step 3 — Set up Playwright (only if missing)

第 3 步——配置 Playwright(仅在缺失时)

If the project already has
playwright.config.*
: reuse it.
Put the spec where the existing specs live and follow their conventions (auth setup, helpers, fixtures). Do not create a second harness.
If it does not, install the minimum:
bash
npm install -D @playwright/test
npx playwright install chromium
On Linux or WSL the browser also needs system libraries (otherwise it fails with errors like
libnspr4.so: cannot open shared object file
). Installing them requires
sudo
, so ask the user to run it — never try to obtain their password:
bash
sudo npx playwright install-deps chromium
And create
playwright.config.js
(
.ts
if the project uses TypeScript) with only what matters:
js
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './e2e',
  use: {
    baseURL: 'http://localhost:3000',     // the app's local URL
    trace: 'retain-on-failure',           // step-by-step recording, kept only when a test fails
    screenshot: 'only-on-failure',        // evidence for the human
  },
  webServer: {
    command: 'npm run dev',               // the project's real dev command
    url: 'http://localhost:3000',
    reuseExistingServer: true,
  },
  projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
});
Take the port and the start command from the project — do not guess them. Add
test-results/
and
playwright-report/
to
.gitignore
.
如果项目已有
playwright.config.*
:直接复用。
将 spec 放在现有 spec 所在的位置,并遵循项目已有约定(认证配置、辅助函数、fixtures)。不要创建第二套测试框架。
如果没有,则安装最小化配置:
bash
npm install -D @playwright/test
npx playwright install chromium
在 Linux 或 WSL 上,浏览器还需要系统依赖库,否则会出现类似
libnspr4.so: cannot open shared object file
的错误。安装这些依赖需要
sudo
,因此应请用户自行运行——绝不要尝试获取他们的密码:
bash
sudo npx playwright install-deps chromium
并创建
playwright.config.js
(如果项目使用 TypeScript,则使用
.ts
),仅保留必要配置:
js
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  testDir: './e2e',
  use: {
    baseURL: 'http://localhost:3000',     // the app's local URL
    trace: 'retain-on-failure',           // step-by-step recording, kept only when a test fails
    screenshot: 'only-on-failure',        // evidence for the human
  },
  webServer: {
    command: 'npm run dev',               // the project's real dev command
    url: 'http://localhost:3000',
    reuseExistingServer: true,
  },
  projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
});
端口和启动命令必须取自项目本身——不要猜测。将
test-results/
和
playwright-report/
添加到
.gitignore
。

Step 4 — Write the test

第 4 步——编写测试

  • Locators the way a user sees the page:
    getByRole
    ,
    getByLabel
    ,
    getByText
    ,
    getByPlaceholder
    . Use
    getByTestId
    only if nothing visible identifies the element. Never CSS classes or XPath.
  • No manual waits: no
    waitForTimeout
    , no
    networkidle
    . Playwright waits on its own; web-first assertions (
    await expect(locator).toBeVisible()
    ) retry until they pass or time out.
  • At least one assertion on the final visible outcome. Clicks without an assertion prove nothing.
  • Data that can run twice: if the flow creates records or takes a limited resource (a time slot, a username), pick values that do not collide on the next run.
  • Short comments only where a step is not obvious from the code.
  • 像用户感知页面一样选择定位器:使用
    getByRole
    、
    getByLabel
    、
    getByText
    、
    getByPlaceholder
    。仅当没有任何可见特征能够标识元素时,才使用
    getByTestId
    。绝不使用 CSS class 或 XPath。
  • 不要手动等待:不要使用
    waitForTimeout
    ,也不要使用
    networkidle
    。Playwright 会自动等待;Web-first 断言(
    await expect(locator).toBeVisible()
    )会持续重试,直到通过或超时。
  • 至少对最终可见结果进行一次断言。 只有点击而没有断言,无法证明任何事情。
  • 确保数据可以重复运行:如果流程会创建记录或占用有限资源(例如时间段、用户名),请选择不会在下次运行时发生冲突的值。
  • 仅在代码无法清楚说明某个步骤时添加简短注释。

Step 5 — Run only this spec

第 5 步——只运行此 spec

bash
npx playwright test <path-to-spec> --reporter=list
Never run the whole suite unless the user asks — on real projects it can take many minutes.
bash
npx playwright test <path-to-spec> --reporter=list
除非用户明确要求,否则绝不运行整个测试套件——在真实项目中,这可能需要数分钟。

Step 6 — Prove the test can fail

第 6 步——证明测试能够失败

A test that passes with and without the feature verifies nothing. Once it is green:
  1. Make one small, temporary change in the app that breaks the expected outcome (e.g. stop sending a required field), and tell the user what you changed.
  2. Run the spec again — it must go red, for the reason you expect.
  3. Revert the change and confirm with
    git diff
    that only the test files remain changed.
一个无论功能存在与否都能通过的测试无法验证任何事情。测试通过后:
  1. 对应用进行一项小而临时的修改,使预期结果失效(例如,不再发送某个必填字段),并告知用户具体修改了什么。
  2. 再次运行该 spec——它必须因为预期原因而失败。
  3. 撤销修改,并使用
    git diff
    确认只有测试文件仍处于已修改状态。

Step 7 — Report

第 7 步——报告

undefined
undefined

E2E test: <flow>

E2E test: <flow>

  • Spec: <path>
  • Result: PASSED | FAILED
  • Proof it can fail: <what was broken, and the error it produced>
  • Command: npx playwright test <path>

If the test failed, add:
  • What failed: <step and assertion>
  • Evidence: <screenshot,
    error-context.md
    and trace paths under
    test-results/
    ; open the trace with
    npx playwright show-trace <trace.zip>
    — its network tab shows the API status codes>
  • Diagnosis: app bug | outdated test | environment (server down, data) — with the reasoning
  • Proposed fix: <what to change, and where>

Then stop. The user decides what gets fixed.
  • Spec: <path>
  • Result: PASSED | FAILED
  • Proof it can fail: <what was broken, and the error it produced>
  • Command: npx playwright test <path>

如果测试失败,补充:
  • What failed: <step and assertion>
  • Evidence: <screenshot,
    error-context.md
    and trace paths under
    test-results/
    ; open the trace with
    npx playwright show-trace <trace.zip>
    — its network tab shows the API status codes>
  • Diagnosis: app bug | outdated test | environment (server down, data) — with the reasoning
  • Proposed fix: <what to change, and where>

然后停止。由用户决定要修复什么。