1
---
2
name: e2e-testing-patterns
3
description: Master end-to-end testing with Playwright and Cypress to build reliable test suites that catch bugs, improve confidence, and enable fast deployment. Use when implementing E2E tests, debugging flaky tests, or establishing testing standards.
4
---
5
6
# E2E Testing Patterns
7
8
Build reliable, fast, and maintainable end-to-end test suites that provide confidence to ship code quickly and catch regressions before users do.
9
10
## When to Use This Skill
11
12
- Implementing end-to-end test automation
13
- Debugging flaky or unreliable tests
14
- Testing critical user workflows
15
- Setting up CI/CD test pipelines
16
- Testing across multiple browsers
17
- Validating accessibility requirements
18
- Testing responsive designs
19
- Establishing E2E testing standards
20
21
## Core Concepts
22
23
### 1. E2E Testing Fundamentals
24
25
**What to Test with E2E:**
26
27
- Critical user journeys (login, checkout, signup)
28
- Complex interactions (drag-and-drop, multi-step forms)
29
- Cross-browser compatibility
30
- Real API integration
31
- Authentication flows
32
33
**What NOT to Test with E2E:**
34
35
- Unit-level logic (use unit tests)
36
- API contracts (use integration tests)
37
- Edge cases (too slow)
38
- Internal implementation details
39
40
### 2. Test Philosophy
41
42
**The Testing Pyramid:**
43
44
```
45
/\
46
/E2E\ ← Few, focused on critical paths
47
/─────\
48
/Integr\ ← More, test component interactions
49
/────────\
50
/Unit Tests\ ← Many, fast, isolated
51
/────────────\
52
```
53
54
**Best Practices:**
55
56
- Test user behavior, not implementation
57
- Keep tests independent
58
- Make tests deterministic
59
- Optimize for speed
60
- Use data-testid, not CSS selectors
61
62
## Detailed patterns and worked examples
63
64
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
65
66
## Best Practices
67
68
1. **Use Data Attributes**: `data-testid` or `data-cy` for stable selectors
69
2. **Avoid Brittle Selectors**: Don't rely on CSS classes or DOM structure
70
3. **Test User Behavior**: Click, type, see - not implementation details
71
4. **Keep Tests Independent**: Each test should run in isolation
72
5. **Clean Up Test Data**: Create and destroy test data in each test
73
6. **Use Page Objects**: Encapsulate page logic
74
7. **Meaningful Assertions**: Check actual user-visible behavior
75
8. **Optimize for Speed**: Mock when possible, parallel execution
76
77
```typescript
78
// ❌ Bad selectors
79
cy.get(".btn.btn-primary.submit-button").click();
80
cy.get("div > form > div:nth-child(2) > input").type("text");
81
82
// ✅ Good selectors (findBy* commands come from @testing-library/cypress;
83
// add `import "@testing-library/cypress/add-commands";` to your Cypress support file)
84
cy.findByRole("button", { name: "Submit" }).click();
85
cy.findByLabelText("Email address").type("user@example.com");
86
cy.get('[data-testid="email-input"]').type("user@example.com");
87
```
88
89
## Common Pitfalls
90
91
- **Flaky Tests**: Use proper waits, not fixed timeouts
92
- **Slow Tests**: Mock external APIs, use parallel execution
93
- **Over-Testing**: Don't test every edge case with E2E
94
- **Coupled Tests**: Tests should not depend on each other
95
- **Poor Selectors**: Avoid CSS classes and nth-child
96
- **No Cleanup**: Clean up test data after each test
97
- **Testing Implementation**: Test user behavior, not internals
98
99
## Debugging Failing Tests
100
101
```typescript
102
// Playwright debugging
103
// 1. Run in headed mode
104
npx playwright test --headed
105
106
// 2. Run in debug mode
107
npx playwright test --debug
108
109
// 3. Use trace viewer (record with `use: { trace: 'on-first-retry' }` in the config)
110
npx playwright show-trace path/to/trace.zip
111
await page.screenshot({ path: 'screenshot.png' });
112
113
// 4. Add test.step for better reporting
114
test('checkout flow', async ({ page }) => {
115
await test.step('Add item to cart', async () => {
116
await page.goto('/products');
117
await page.getByRole('button', { name: 'Add to Cart' }).click();
118
});
119
120
await test.step('Proceed to checkout', async () => {
121
await page.goto('/cart');
122
await page.getByRole('button', { name: 'Checkout' }).click();
123
});
124
});
125
126
// 5. Inspect page state
127
await page.pause(); // Pauses execution, opens inspector
128
```
129