AgentSkillsCN

create-e2e-test

Playwright E2E测试编写指南:结合项目的认证机制、辅助工具与隔离模式

SKILL.md
--- frontmatter
name: create-e2e-test
description: Guide for writing Playwright E2E tests with the project's auth, helpers, and isolation patterns

Skill: Create E2E Test

Use this skill when adding a new Playwright E2E test spec. See examples/ for annotated reference tests.

Infrastructure

FilePurpose
playwright.config.tsSerial execution, 1 worker, port 3001, auth project
tests/global-setup.tsCreates fresh ynab_test DB, seeds CSV data, 2 users
tests/auth.setup.tsLogs in as TEST_USER, saves session to .auth/user.json
tests/test-constants.tsCredentials, URLs, DB names
tests/e2e-helpers.tsNavigation helpers

Steps

1. Create the spec file

code
tests/<feature-name>.spec.ts

Use the template from examples/example.spec.ts.

2. Use navigation helpers (mandatory)

typescript
import {
  gotoBudgetPage,
  gotoFirstAccount,
  getTestBudgetId,
} from "./e2e-helpers";
HelperWhat it does
gotoBudgetPage(page, request)Navigate + wait for budget-table, returns budgetId
gotoFirstAccount(page, request)Navigate + click first sidebar account
getTestBudgetId(request)Fetch budgetId via API (cached)
budgetUrl(budgetId)Returns /budgets/{id}/budget
accountUrl(budgetId, accountId)Returns /budgets/{id}/accounts/{accountId}
allAccountsUrl(budgetId)Returns /budgets/{id}/accounts

Never hardcode URLs — SaaS routes use /budgets/[id]/budget, not /budget.

3. Use data-testid selectors

typescript
page.getByTestId("rta-amount");
page.locator('[data-testid^="category-row-"]'); // prefix match
page.locator('[data-testid^="sidebar-account-"]');

4. Wait for server roundtrips after mutations

typescript
await page.getByTestId("save-button").click();
await page.waitForResponse(
  (resp) => resp.url().includes("/api/budgets/") && resp.status() === 200,
);
await expect(page.getByTestId("value")).toHaveText("Updated");

5. Run

bash
npm run test:e2e                              # full suite
npm run test:e2e -- tests/my-feature.spec.ts  # single file
npm run test:e2e -- --ui                      # debug UI

Special Patterns

Common Pitfalls

PitfallFix
Wrong URLUse helpers, not page.goto('/budget')
Intermittent assertion failureWait for server roundtrip before asserting
RTA shows $ 0,00Poll for animated value stability
Login failsUI labels are in Spanish (Contraseña, Iniciar Sesión)
storageState not foundauth-setup project runs as dependency (configured in playwright.config.ts)

Examples Directory

FilePattern
example.spec.tsStandard test with navigation, mutation, animated values
isolation.spec.tsTenant isolation with manual login