AgentSkillsCN

e2e-testing

在编写端到端测试、设计 E2E 测试策略,或调试不稳定、易出错的 E2E 测试时,此功能不可或缺。

SKILL.md
--- frontmatter
name: e2e-testing
description: Use when writing end-to-end tests, designing E2E test strategy, or debugging flaky E2E tests

E2E Testing

Announce at start: "Following the e2e-testing skill for end-to-end test design."

Core Rule

Test critical user journeys, not every interaction. E2E tests are expensive — be selective.

When to Write E2E Tests

Write E2EDon't Write E2E
Critical user flows (login, checkout, signup)Individual component behavior (use unit tests)
Multi-step workflows spanning pagesSimple CRUD operations
Cross-browser/device verificationInternal API validation (use integration tests)
Regression for UI bugs that unit tests can't catchStyling or layout checks

Process

1. Identify — Select the Right Flows

  • List critical user journeys (max 10-20 for most apps)
  • Prioritize by business impact and failure risk
  • Avoid duplicating what integration tests already cover

2. Structure — Page Object Model

  • One page object per page/component
  • Encapsulate selectors and actions
  • Tests read like user stories

3. Write — Stable Selectors

PrioritySelectorWhy
BestgetByTestId('submit-btn')Stable, explicit, refactor-proof
GoodgetByRole('button', { name: 'Submit' })Semantic, accessible
Avoid.btn-primary, div > span:nth-child(2)Brittle, breaks on styling changes

4. Handle Async — No Sleep Statements

  • Use explicit waits (waitForSelector, waitForResponse)
  • Use web-first assertions (auto-retry)
  • Never use sleep() or setTimeout() for timing

5. Verify — Test Isolation

  • Each test starts from a clean state
  • Tests don't depend on each other's execution order
  • Tests clean up created data

Flaky Test Triage

SymptomLikely CauseFix
Passes locally, fails in CITiming / resource differencesAdd explicit waits
Fails intermittentlyRace conditionUse waitFor patterns
Fails after unrelated changeFragile selectorSwitch to getByTestId
Slow (> 30s per test)Too many page navigationsReuse auth state

Red Flags — STOP

SignalAction
Writing E2E for logic that unit tests coverDelete and write a unit test instead
Using sleep() anywhereReplace with explicit waits
Test depends on test execution orderIsolate the test
E2E suite takes > 10 minutesPrune — you have too many

Related Skills

WhenInvoke
Need unit/integration tests insteadtesting
E2E test reveals a bugdebugging
Ready to submit changespr-writing

Deep Reference

For principles, rationale, anti-patterns, and examples: guides/e2e-testing/e2e-testing.md