How AI Can Fix Your Broken Tests (And When It Should Not) blog banner image

How AI Can Fix Your Broken Tests (And When It Should Not)

AI Test Automation

Automated testing powers modern QA. But ask any tester what is more frustrating than flaky scripts — they will tell you: broken tests.

A tiny UI tweak, a renamed button, a slight HTML adjustment, or a reshuffled layout can make dozens of automated tests fail. These are not real bugs; they are false alarms caused by brittle locators. Fixing them manually eats up valuable time better spent on testing actual issues. Enter AI-driven self-healing: your safety net for fragile tests.

What Is Self-Healing in Test Automation?

Self-healing allows your automation framework to detect when a failure is caused by a UI change and automatically repair it. Instead of stopping with an error, AI can:

  • Suggest alternative selectors (CSS, XPath, or text) for the same element
  • Retry the action with the new selector
  • Continue the test without human intervention

Example:

  • Old locator: //button[@id='loginBtn']
  • UI change: button renamed to “Sign in”
  • AI detects context → updates locator: //button[contains(text(),'Sign in')]
  • Result: Test passes automatically, no manual work needed

Open-Source Ways to Experiment with AI Healing

Commercial tools like testRigor, Mabl, and Testim make AI healing easy, but you can experiment in open-source frameworks too:

  • Healenium (Selenium) – learns locators and replaces broken ones automatically
  • Playwright + AI Layer – capture failing DOM + selectors, send them to an LLM (like OpenAI or Hugging Face), and generate alternatives
  • CodeceptJS AI Plugin – integrates AI suggestions to fix test steps

AI Healing in Playwright: Example

Here is a minimal, practical example showing AI-driven healing in action:

// Conceptual AI-healing flow for a failing selector
async function runTest(page, oldSelector) {
try {
await page.click(oldSelector); // Try the old selector
} catch (error) {
const domSnapshot = await page.content();
const healedSelector = await aiHealSelector(domSnapshot, oldSelector, error.message);
console.log("AI healed selector:", healedSelector);
await page.click(healedSelector); // Retry with healed selector
}
}

Key idea: Capture the failing DOM, ask AI for a better locator, retry the action, and log the healed selector for review.

Benefits of AI-Driven Healing

  • Less maintenance: fewer hours updating fragile locators
  • Stronger CI/CD pipelines: fewer false failures blocking builds
  • Closer to real user behavior: tests adapt to UI changes like a real user

Disadvantages & Limitations

  • Masking real bugs: AI might “heal” a test even if the user flow broke intentionally
  • Wrong element selection: AI could pick a visually similar but functionally incorrect element
  • Execution slowdown: AI healing requires network calls and DOM snapshots
  • Lack of transparency: Without proper logging, you will not know what changed and why

Best practice: Log all AI healing attempts and let humans review or approve permanent updates. AI should assist, not silently take over.

Hybrid Approach for Efficiency

To reduce delays while keeping AI healing benefits:

  • Batch failures: Collect multiple failed selectors and call AI once to suggest fixes
  • Critical elements only: Apply AI healing for high-priority elements (login, checkout), let minor selectors fail normally
  • Cache healed selectors: Store AI-suggested selectors locally to avoid repeated API calls

This keeps tests robust against UI changes while minimizing execution delays.

The Balanced Approach

AI is a strong ally — not a replacement.

  • Let AI handle routine fixes: minor UI changes, locator updates
  • Keep human oversight for functional changes: workflows, logic, business rules

Think of AI healing as a safety net, reducing noise, so QA teams can focus on testing what truly matters: product quality.

Key Takeaway

AI can fix broken tests — but only the right kind of broken ones.

Used thoughtfully, AI healing:

  • Reduces maintenance costs
  • Stabilizes pipelines
  • Keeps automation suites healthy

Overused without oversight, it risks masking genuine issues.

The future of QA is not humans or AI — it is humans and AI working together to deliver smarter, faster, and safer testing.

Related Posts