AgentSkillsCN

Ui Testing Strategy

UI 测试策略

SKILL.md

UI Testing Strategy

Purpose

Design a comprehensive UI testing strategy for a frontend application, covering component testing, visual regression, accessibility, snapshot testing, and E2E flows. Produce a layered testing plan with tool recommendations and implementation guidance.

When to Use

  • Setting up testing for a new frontend project
  • Adding test coverage to an existing frontend codebase
  • Reviewing or improving the test strategy for a UI-heavy application
  • Migrating between testing frameworks or approaches

Steps

Step 1: Assess the Frontend Stack

Identify:

  • Framework: React, Vue, Angular, Svelte, etc.
  • Styling: CSS Modules, Tailwind, Styled Components, etc.
  • State management: Redux, Zustand, Pinia, Context, etc.
  • Routing: React Router, Vue Router, Next.js, etc.
  • API layer: REST, GraphQL, tRPC, etc.
  • Build tool: Vite, Webpack, Turbopack, etc.
  • Existing tests: what exists today (if anything)

Step 2: Define the Test Pyramid

Establish the distribution of test types:

code
                  ┌─────────┐
                  │   E2E   │  5-10% -- Critical user journeys
                 ─┼─────────┼─
                │  Visual   │  10-15% -- Screenshot comparisons
               ─┼───────────┼─
              │ Integration  │  20-30% -- Component + context/API
             ─┼──────────────┼─
            │   Component     │  50-60% -- Isolated component behavior
           ─┼─────────────────┼─

Step 3: Select Tools

LayerRecommended ToolsPurpose
ComponentReact Testing Library, Vue Test UtilsBehavior-driven component tests
IntegrationMSW (Mock Service Worker) + RTLComponent + API interaction
VisualPlaywright screenshots or ChromaticPixel-level regression detection
Accessibilityaxe-core + jest-axe + Playwright axeWCAG 2.2 AA compliance
SnapshotJest/Vitest inline snapshotsResponse shape and output stability
E2EPlaywright or CypressFull user flow validation

Step 4: Design Component Tests

For each component, test:

  1. Rendering: Does it render with required props?
  2. User interaction: Do clicks, typing, and selection work?
  3. Conditional rendering: Do elements show/hide based on state?
  4. Error states: Does it handle invalid data gracefully?
  5. Accessibility: Does it have proper roles, labels, and keyboard support?

Follow the query priority hierarchy:

  1. getByRole > getByLabelText > getByText > getByTestId

Step 5: Design Visual Regression Tests

  1. Identify components with strict visual requirements
  2. Create Storybook stories for each visual state
  3. Configure screenshot comparison tool:
    • Set diff threshold (0.1% standard, 0% for pixel-perfect)
    • Define responsive breakpoints (mobile, tablet, desktop)
    • Mask dynamic content (timestamps, avatars)
  4. Establish baseline update workflow (who approves, how)

Step 6: Design Accessibility Tests

  1. Add jest-axe assertions to every component test
  2. Create dedicated keyboard navigation tests for interactive elements
  3. Verify ARIA landmarks, roles, and labels
  4. Test focus management (modals, dropdowns, dynamic content)
  5. Configure CI to fail on axe violations

Step 7: Design E2E Tests

Identify critical user journeys (limit to 10-20 for speed):

  • Authentication flow (login, logout, password reset)
  • Primary business flow (e.g., create order, make payment)
  • Error recovery flows (network failure, validation errors)
  • Cross-cutting concerns (navigation, search, filtering)

Step 8: Configure CI Integration

yaml
test-pipeline:
  component-tests:     # Fast, run on every commit
    - npm run test:unit
  accessibility-tests:  # Fast, run on every commit
    - npm run test:a11y
  visual-tests:         # Medium, run on PR
    - npm run test:visual
  e2e-tests:            # Slow, run on PR merge to main
    - npm run test:e2e

Step 9: Define Coverage Targets

MetricTarget
Component test coverage80%+
axe-core violations0
Visual regression baselinesAll components in Storybook
E2E journey coverageAll critical paths
Snapshot coverageAPI response shapes

Output Format

Produce:

  1. A testing strategy document with tool selections and rationale
  2. Example test files for each layer (component, visual, a11y, E2E)
  3. CI configuration for running tests at appropriate stages
  4. Coverage targets and measurement approach