|
| 1 | +--- |
| 2 | +description: Run E2E tests and fix any failures |
| 3 | +argument-hint: <test-pattern> |
| 4 | +--- |
| 5 | + |
| 6 | +# Fix E2E Tests |
| 7 | + |
| 8 | +Run E2E tests matching a pattern, analyze failures, and fix them. |
| 9 | + |
| 10 | +**Follow all standards in `.ai/rules/e2e-testing.md`** |
| 11 | + |
| 12 | +## Input |
| 13 | + |
| 14 | +**Test pattern** (required) - Test name or describe block pattern |
| 15 | +- Examples: `"Analytics > Slow Log"`, `"should add string key"`, `"@smoke"` |
| 16 | + |
| 17 | +## Process |
| 18 | + |
| 19 | +### Step 1: Run the Tests |
| 20 | + |
| 21 | +```bash |
| 22 | +cd tests/e2e-playwright |
| 23 | +npx playwright test --grep "<pattern>" --reporter=list 2>&1 | tail -50 |
| 24 | +``` |
| 25 | + |
| 26 | +### Step 2: Analyze Failures |
| 27 | + |
| 28 | +If tests fail, check the error context file: |
| 29 | + |
| 30 | +```bash |
| 31 | +cat test-results/<test-folder>/error-context.md | head -150 |
| 32 | +``` |
| 33 | + |
| 34 | +The error context contains: |
| 35 | +- **Page snapshot** - Current UI state (element tree with refs) |
| 36 | +- **Error message** - What assertion failed |
| 37 | +- **Call log** - Playwright's action log |
| 38 | + |
| 39 | +### Step 3: Diagnose the Issue |
| 40 | + |
| 41 | +Common failure patterns: |
| 42 | + |
| 43 | +| Error | Likely Cause | Solution | |
| 44 | +|-------|--------------|----------| |
| 45 | +| `element(s) not found` | Wrong selector or element not rendered | Check snapshot for correct testid/role | |
| 46 | +| `Timeout waiting for` | Element takes too long to appear | Add proper wait or check if element exists | |
| 47 | +| `expected visible` | Element hidden or removed | Verify UI flow, check if dialog closed | |
| 48 | +| `not.toBeVisible failed` | Element still visible | Wait for element to be removed | |
| 49 | + |
| 50 | +### Step 4: Explore UI if Needed |
| 51 | + |
| 52 | +If the error context doesn't reveal the issue, use Playwright MCP to explore: |
| 53 | + |
| 54 | +``` |
| 55 | +browser_navigate_Playwright → http://localhost:8080 |
| 56 | +browser_snapshot_Playwright |
| 57 | +browser_click_Playwright → element, ref |
| 58 | +``` |
| 59 | + |
| 60 | +### Step 5: Fix the Test |
| 61 | + |
| 62 | +Apply fixes following these priorities: |
| 63 | + |
| 64 | +1. **Fix selectors** - Use correct `data-testid` or role from snapshot |
| 65 | +2. **Fix waits** - Replace `waitForTimeout` with proper element waits |
| 66 | +3. **Fix test data** - Ensure unique prefixes to avoid conflicts with other tests |
| 67 | +4. **Fix assertions** - Match actual UI behavior |
| 68 | + |
| 69 | +### Step 6: Verify the Fix |
| 70 | + |
| 71 | +```bash |
| 72 | +cd tests/e2e-playwright |
| 73 | +npx playwright test --grep "<pattern>" --reporter=list 2>&1 | tail -30 |
| 74 | +``` |
| 75 | + |
| 76 | +### Step 7: Run Linter and Type Check |
| 77 | + |
| 78 | +**REQUIRED before completing:** |
| 79 | + |
| 80 | +```bash |
| 81 | +cd tests/e2e-playwright |
| 82 | +npm run lint && npx tsc --noEmit |
| 83 | +``` |
| 84 | + |
| 85 | +Both must pass. |
| 86 | + |
| 87 | +## Debugging Tips |
| 88 | + |
| 89 | +### Check Page Snapshot for Correct Selectors |
| 90 | + |
| 91 | +```yaml |
| 92 | +# Look for data-testid in the snapshot |
| 93 | +- button "Add" [ref=e123] [cursor=pointer] # Use getByRole('button', { name: 'Add' }) |
| 94 | +- generic "my-testid" [ref=e456] # Use getByTestId('my-testid') |
| 95 | +- treeitem "String keyname..." [ref=e789] # Use getByRole('treeitem', { name: /keyname/ }) |
| 96 | +``` |
| 97 | +
|
| 98 | +### Handle View Mode Differences |
| 99 | +
|
| 100 | +Browser page has List view and Tree view with different element structures: |
| 101 | +
|
| 102 | +```typescript |
| 103 | +// List view uses gridcell |
| 104 | +page.getByRole('gridcell', { name: keyName }) |
| 105 | + |
| 106 | +// Tree view uses treeitem |
| 107 | +page.getByRole('treeitem', { name: new RegExp(keyName) }) |
| 108 | + |
| 109 | +// KeyList.getKeyRow() handles both |
| 110 | +``` |
| 111 | + |
| 112 | +### Test Isolation Issues |
| 113 | + |
| 114 | +If test fails inconsistently, check for: |
| 115 | +- Missing unique suffix in test data (conflicts with parallel runs) |
| 116 | +- Missing cleanup in `afterEach` |
| 117 | +- Shared state between tests (use `test.describe.serial` if needed) |
| 118 | + |
| 119 | +### Dropdown/Dialog Issues |
| 120 | + |
| 121 | +If clicking fails after interacting with dropdown: |
| 122 | +```typescript |
| 123 | +// Close dropdown before next action |
| 124 | +await page.keyboard.press('Escape'); |
| 125 | +``` |
| 126 | + |
| 127 | +## Example Usage |
| 128 | + |
| 129 | +``` |
| 130 | +@e2e-fix "Analytics > Slow Log" |
| 131 | +@e2e-fix "should add string key" |
| 132 | +@e2e-fix "Browser > Key Details > String" |
| 133 | +@e2e-fix "@smoke" |
| 134 | +``` |
| 135 | + |
| 136 | +## Quick Reference |
| 137 | + |
| 138 | +| Command | Purpose | |
| 139 | +|---------|---------| |
| 140 | +| `npx playwright test --grep "pattern"` | Run matching tests | |
| 141 | +| `npx playwright test --grep "pattern" --debug` | Run with inspector | |
| 142 | +| `npx playwright show-trace <trace.zip>` | View test trace | |
| 143 | +| `npm run lint` | Check for lint errors | |
| 144 | +| `npx tsc --noEmit` | Check for type errors | |
| 145 | + |
0 commit comments