Back to docs

AI Test Repair

Automatically analyze failing tests and suggest or apply fixes. (v6.2.0)


The Problem

Tests break constantly:

  • Selectors change when UI updates
  • Timing issues cause flaky failures
  • Page content changes break assertions
  • Elements move or get renamed

Manually fixing tests is tedious and time-consuming.


The Solution

CBrowser's AI Test Repair:

  1. Runs your tests
  2. Analyzes failures
  3. Suggests specific fixes
  4. Optionally applies them automatically
  5. Verifies the repairs work

Quick Start

# Analyze a broken test and see suggestions
npx cbrowser repair-tests broken-test.txt

# Automatically apply repairs
npx cbrowser repair-tests tests.txt --auto-apply

# Apply repairs and verify they work
npx cbrowser repair-tests tests.txt --auto-apply --verify

# Save repaired tests to new file
npx cbrowser repair-tests tests.txt --auto-apply --output fixed-tests.txt

What It Detects

Failure Type Detection Repair Strategy
selector_not_found Element doesn't exist Find alternative selectors on current page
assertion_failed Verify statement false Suggest updated assertions based on actual content
timeout Step took too long Add explicit wait statements
element_not_interactable Element hidden/disabled Add scroll/wait before interaction

Example Output

πŸ”§ AI TEST REPAIR REPORT
══════════════════════════════════════════════════════════════

πŸ“‹ Analyzing: Login Tests

──────────────────────────────────────────────────────────────
TEST: Login Flow
──────────────────────────────────────────────────────────────

  β†’ go to https://example.com
    βœ“ Passed

  β†’ click the signin button
    βœ— Failed: Failed to click: signin button

    πŸ’‘ SUGGESTIONS:

    1. Update selector to "Login" (confidence: 70%)
       Original: click the signin button
       Fixed:    click "Login"
       Reason:   Found button with text "Login" on page

    2. Update selector to "Sign In" (confidence: 60%)
       Original: click the signin button
       Fixed:    click "Sign In"
       Reason:   Found link with text "Sign In" on page

    3. Add wait before click (confidence: 50%)
       Original: click the signin button
       Fixed:    wait 2 seconds
                 click the signin button
       Reason:   Element may not be loaded yet

  β†’ type "[email protected]" in email
    βœ“ Passed (skipped - previous step failed)

──────────────────────────────────────────────────────────────
TEST: Search
──────────────────────────────────────────────────────────────

  β†’ verify page contains "10 results"
    βœ— Failed: Expected "10 results", found "12 results"

    πŸ’‘ SUGGESTIONS:

    1. Update assertion (confidence: 90%)
       Original: verify page contains "10 results"
       Fixed:    verify page contains "12 results"
       Reason:   Page now shows "12 results"

    2. Use flexible assertion (confidence: 85%)
       Original: verify page contains "10 results"
       Fixed:    verify page contains "results"
       Reason:   Makes test resilient to count changes

══════════════════════════════════════════════════════════════
πŸ“Š SUMMARY
══════════════════════════════════════════════════════════════

  Tests analyzed: 2
  Failed steps: 2
  Suggestions generated: 5
  Auto-applicable: 3

  Run with --auto-apply to fix automatically

Auto-Apply Mode

When you use --auto-apply, CBrowser:

  1. Selects the highest-confidence suggestion for each failure
  2. Applies the fix to your test
  3. Shows what changed
npx cbrowser repair-tests tests.txt --auto-apply
πŸ”§ AUTO-APPLYING REPAIRS
══════════════════════════════════════════════════════════════

TEST: Login Flow

  Step 2: click the signin button
  ─────────────────────────────────
  - click the signin button
  + click "Login"

  βœ“ Applied (confidence: 70%)

TEST: Search

  Step 1: verify page contains "10 results"
  ─────────────────────────────────
  - verify page contains "10 results"
  + verify page contains "results"

  βœ“ Applied (confidence: 85%)

══════════════════════════════════════════════════════════════
πŸ“Š REPAIRS APPLIED: 2

Repaired test saved to: tests.txt.repaired

Verify Repairs

Add --verify to run the repaired tests and confirm they pass:

npx cbrowser repair-tests tests.txt --auto-apply --verify
πŸ”§ VERIFYING REPAIRS
══════════════════════════════════════════════════════════════

Running repaired tests...

TEST: Login Flow
  βœ“ go to https://example.com
  βœ“ click "Login"              ← REPAIRED
  βœ“ type "[email protected]" in email
  βœ“ click submit
  βœ… PASSED

TEST: Search
  βœ“ go to https://example.com/search
  βœ“ type "query" in search box
  βœ“ verify page contains "results"  ← REPAIRED
  βœ… PASSED

══════════════════════════════════════════════════════════════
πŸ“Š VERIFICATION: 2/2 tests now passing

API Usage

import {
  parseNLTestSuite,
  repairTestSuite,
  formatRepairReport,
  exportRepairedTest
} from 'cbrowser';

// Parse test content
const suite = parseNLTestSuite(testContent, "My Tests");

// Run repair analysis
const result = await repairTestSuite(suite, {
  autoApply: true,
  verifyRepairs: true,
  headless: true,
});

// Show report
console.log(formatRepairReport(result));

// Access repaired tests
for (const testResult of result.testResults) {
  if (testResult.wasRepaired) {
    console.log(`Repaired: ${testResult.testName}`);
    console.log(exportRepairedTest(testResult));
  }
}

// Get repair statistics
console.log(`Repair success rate: ${result.summary.repairSuccessRate}%`);

How the AI Works

  1. Failure Analysis: Parses error messages to understand what went wrong
  2. Page Inspection: Examines current page to find alternatives
  3. Pattern Matching: Compares failed selector against available elements
  4. Confidence Scoring: Ranks suggestions by likelihood of success
  5. Validation: Optionally runs repaired test to confirm fix

The AI doesn't just guessβ€”it examines the actual page state and makes informed suggestions.


Configuration

{
  "repair": {
    "autoApplyThreshold": 70,
    "maxSuggestionsPerStep": 3,
    "preferFlexibleAssertions": true,
    "addWaitsForTimeouts": true
  }
}
Option Default Description
autoApplyThreshold 70 Minimum confidence to auto-apply
maxSuggestionsPerStep 3 How many suggestions to show
preferFlexibleAssertions true Prefer resilient assertions
addWaitsForTimeouts true Add waits for timeout failures

Best Practices

  1. Review before applying - Run without --auto-apply first
  2. Use --verify - Confirm repairs actually work
  3. Commit repaired tests - Keep your test suite up to date
  4. Check confidence scores - Low confidence = review manually
  5. Combine with flaky detection - Fix flaky tests first

Related


Copyright: (c) 2026 Alexa Eden.

License: MIT License

Contact: [email protected]

From the Blog