Debug and Fix Failing Autotests
Diagnose failing automated tests and apply targeted fixes.
Follow the four steps in order.
Detailed fixes and code examples: references/DEBUGGING_QUICK_REFERENCE.md.
Step 1: Analyze Failure
- Identify the framework. It determines the debug tools:
- Playwright: CLI debug, trace viewer, MCP snapshots.
- CodeceptJS:
--steps mode, CDP inspection.
- Cypress: devTools, screenshots on failure.
- WebdriverIO: Selenium logs.
- Categorize the error:
- Locator (element missing/unreachable).
- Timing (timeout/race).
- Assertion (expected ≠ actual).
- Flow (navigation, preconditions).
- Infrastructure (CI/browser/network).
- End with a hypothesis: root cause + fix category.
Step 2: Inspect and Diagnose
- DOM inspection:
- Playwright:
page.locator('selector').evaluate(el => el.outerHTML).
- CodeceptJS:
I.executeScript(() => document.querySelector('.selector').outerHTML).
- MCP snapshot: capture live DOM state for complex elements.
- Console and network logs:
- Console:
page.on('console', msg => console.log(msg.text())).
- Failed requests:
page.on('requestfailed', r => console.log(r.failure().errorText)).
- Trace analysis (Playwright):
npx playwright show-trace trace.zip.
- Inspect timing and order of clicks, navigation, network requests.
Step 3: Apply Fix
Fix in priority order: locators → timing → assertions → flow.
- Locators: use stable selectors (
data-testid → aria-label → role → id → text → CSS/XPath).
- Re-fetch stale elements, scroll hidden elements into view, use
.first() for multiple matches.
- Timing: use framework-native waits (
waitFor, waitForNavigation, waitForLoadState('networkidle')).
- No hard sleeps like
sleep(5000) or wait(2).
- Assertions: use contains/regex for flexible matching.
- Verify expected values against the test spec, not assumptions.
- Flow: ensure preconditions (login, navigation order, test data).
- Isolate test state from prior runs.
Before/after examples for each category: references/DEBUGGING_QUICK_REFERENCE.md.
Step 4: Verify and Stabilize
Re-run the test:
npx playwright test path/to/test.spec.ts
npx codeceptjs run path/to/test.js
Test is stable when:
- Passes 2 consecutive runs.
- Uses resilient locators.
- Waits properly for dynamic content.
Max 3 healing attempts total. After 3 failed attempts: stop, document what was tried, ask the user for guidance.
Document the fix: root cause, applied fix, verification result.
MCP Debug Tools (Optional)
Use MCP when standard fixes fail, the UI is complex (dropdowns, modals, dynamic content), or live DOM inspection is needed:
- Playwright MCP: live snapshots, element counts, DOM inspection after each action.
- CodeceptJS MCP: CDP-based inspection,
--steps mode for live debugging.
User Interaction
Ask when unclear:
❓ Which element should I target for this action?
Options:
1. Submit button (data-testid="submit")
2. Save button (aria-label="Save")
3. Other (specify)
Report progress:
🔍 Analyzing failure...
- Error: Element not found
- Context: Login test, line 42
🔧 Applying fix...
- Issue: Selector too broad
- Fix: Using getByRole('button', { name: 'Login' })
✅ Test passed! (Run 1/2)
1---2name: debug-fix-failed-flaky-autotests3description: Diagnose and fix failing automated tests. Analyzes failures, inspects DOM, identifies root causes, and applies targeted fixes using framework tools and MCP/CLI debug modes. Use when tests fail, are flaky, or behave inconsistently (e.g., pass locally but fail in CI).4license: MIT5---67# Debug and Fix Failing Autotests89Diagnose failing automated tests and apply targeted fixes.10Follow the four steps in order.11Detailed fixes and code examples: [references/DEBUGGING_QUICK_REFERENCE.md](references/DEBUGGING_QUICK_REFERENCE.md).1213## Step 1: Analyze Failure1415- Identify the framework. It determines the debug tools:16 - Playwright: CLI debug, trace viewer, MCP snapshots.17 - CodeceptJS: `--steps` mode, CDP inspection.18 - Cypress: devTools, screenshots on failure.19 - WebdriverIO: Selenium logs.20- Categorize the error:21 - Locator (element missing/unreachable).22 - Timing (timeout/race).23 - Assertion (expected ≠ actual).24 - Flow (navigation, preconditions).25 - Infrastructure (CI/browser/network).26- End with a hypothesis: root cause + fix category.2728## Step 2: Inspect and Diagnose2930- DOM inspection:31 - Playwright: `page.locator('selector').evaluate(el => el.outerHTML)`.32 - CodeceptJS: `I.executeScript(() => document.querySelector('.selector').outerHTML)`.33 - MCP snapshot: capture live DOM state for complex elements.34- Console and network logs:35 - Console: `page.on('console', msg => console.log(msg.text()))`.36 - Failed requests: `page.on('requestfailed', r => console.log(r.failure().errorText))`.37- Trace analysis (Playwright): `npx playwright show-trace trace.zip`.38 - Inspect timing and order of clicks, navigation, network requests.3940## Step 3: Apply Fix4142**Fix in priority order: locators → timing → assertions → flow.**4344- Locators: use stable selectors (`data-testid` → `aria-label` → `role` → `id` → `text` → CSS/XPath).45 - Re-fetch stale elements, scroll hidden elements into view, use `.first()` for multiple matches.46- Timing: use framework-native waits (`waitFor`, `waitForNavigation`, `waitForLoadState('networkidle')`).47 - **No hard sleeps** like `sleep(5000)` or `wait(2)`.48- Assertions: use contains/regex for flexible matching.49 - Verify expected values against the test spec, not assumptions.50- Flow: ensure preconditions (login, navigation order, test data).51 - Isolate test state from prior runs.5253Before/after examples for each category: [references/DEBUGGING_QUICK_REFERENCE.md](references/DEBUGGING_QUICK_REFERENCE.md).5455## Step 4: Verify and Stabilize5657Re-run the test:5859```bash60npx playwright test path/to/test.spec.ts61npx codeceptjs run path/to/test.js62```6364Test is stable when:65- Passes 2 consecutive runs.66- Uses resilient locators.67- Waits properly for dynamic content.6869**Max 3 healing attempts total.** After 3 failed attempts: stop, document what was tried, ask the user for guidance.7071Document the fix: root cause, applied fix, verification result.7273## MCP Debug Tools (Optional)7475Use MCP when standard fixes fail, the UI is complex (dropdowns, modals, dynamic content), or live DOM inspection is needed:76- Playwright MCP: live snapshots, element counts, DOM inspection after each action.77- CodeceptJS MCP: CDP-based inspection, `--steps` mode for live debugging.7879## User Interaction8081Ask when unclear:8283```84❓ Which element should I target for this action?8586Options:871. Submit button (data-testid="submit")882. Save button (aria-label="Save")893. Other (specify)90```9192Report progress:9394```95🔍 Analyzing failure...96 - Error: Element not found97 - Context: Login test, line 429899🔧 Applying fix...100 - Issue: Selector too broad101 - Fix: Using getByRole('button', { name: 'Login' })102103✅ Test passed! (Run 1/2)104```