A practical guide to creating self-healing test automation with free, local AI
The Problem Every QA Automation Engineer Faces
You write a perfect Selenium test on Friday. Monday morning, it fails. The developer changed a button ID from submit-btn to submit-button. Your test is broken, and you're spending time fixing locators instead of testing new features.
Sound familiar?
I've been there too many times. That's why I built a framework where AI automatically fixes broken locators. No more maintenance nightmares.
What If Tests Could Heal Themselves?
Imagine this scenario:
Traditional Test:
Test runs → Can't find element → Test fails → You manually fix it
Self-Healing Test:
Test runs → Can't find element → AI analyzes page → AI finds new locator → Test continues
The second one is now possible, and I'll show you exactly how to build it.
My Solution: AI-Powered Self-Healing
I created a framework that combines:
Behavior-Driven Development (BDD) for readable tests
Selenium for browser automation
Local AI for intelligent locator fixing
The best part? It runs entirely on your machine using free, open-source tools.
A Real Example
Here's a test written in plain English:
Scenario: Search Wikipedia
Given I navigate to "https://www.wikipedia.org"
When I search for "Selenium"
Then the page should contain "Selenium"
Now here's the twist: I intentionally used the wrong locator in the code:
private readonly By _searchBox = By.Id("searchBox"); // Wrong!
// The real ID is "searchInput"
When the test runs:
- Tries to find element with
By.Id("searchBox")❌ Fails - AI analyzes the page HTML
- AI determines: "I found an input with id='searchInput'"
- Test uses the new locator ✅ Succeeds
The test heals itself without any human intervention.
How It Actually Works
The Architecture
I built this in three layers:
Layer 1: The Test (BDD)
Tests are written in Gherkin, a language anyone can read. No programming required to understand what the test does.
Layer 2: The Self-Healing Engine
When a locator fails, this layer kicks in. It captures the page HTML and sends it to the AI with context about what element we're looking for.
Layer 3: The AI
The AI (running locally via Ollama or GPT) reads the HTML and suggests a new locator based on the element description we provided.
The Code Flow
Here's what happens behind the scenes:
// Step 1: Normal element finding
try {
return driver.FindElement(locator);
}
catch (NoSuchElementException) {
// Element not found - activate self-healing
}
// Step 2: Ask AI for help
var pageHTML = driver.PageSource;
var suggestion = await aiClient.GetSuggestedLocator(
pageHTML,
failedLocator,
"Search box" // Human description
);
// Step 3: Try AI's suggestion
var element = driver.FindElement(By.XPath(suggestion));
// Success!
The entire process takes about 2–3 seconds.
Why This Approach Works
Advantage #1: Context-Aware
Unlike simple retry logic, AI understands what you're trying to find. You tell it "login button" and it finds buttons with relevant text or attributes.
Advantage #2: No Training Required
There's no machine learning model to train. The AI uses its general understanding of HTML and web pages.
Advantage #3: Free and Private
Everything runs on your machine. No API costs, no data leaving your network. (IF OLLAMA)
Advantage #4: Language-Agnostic
It works for any web technology - React, Angular, Vue, plain HTML. The AI just reads the rendered HTML.
Setting It Up (Actually Simple)
I made this as straightforward as possible:
Step 1: Install Prerequisites
.NET 9 SDK (free)
Ollama (free, local AI or GPT)
Chrome browser
Step 2: Download the AI Model
ollama pull qwen3-coder:480b-cloud
Step 3: Run the Demo
dotnet restore
dotnet build
dotnet test
That's it. Three commands and you see AI self-healing in action.
What I Learned Building This
Discovery #1: AI Needs Context
Simply sending HTML to AI isn't enough. The magic happens when you provide context: "This is a search box" or "This is a submit button". The AI uses this to find the right element.
Discovery #2: Local AI Is Good Enough
I initially thought I'd need GPT-4 for accurate results. Surprisingly, the free local models (like Qwen3-Coder) work excellently for this task.
Discovery #3: Retry Logic Matters
Sometimes AI suggests a locator that still doesn't work. I implemented 3 retry attempts with different HTML parsing strategies. This improved success rate from 70% to 95%.
Discovery #4: Temperature Is Crucial
AI models have a "temperature" setting. Higher = creative, lower = consistent. For test automation, you want consistent. I use 0.1, which gives nearly identical results every run.
When Should You Use This?
Good Use Cases:
Regression test suites that break frequently
Testing sites with frequent UI changes
Demos where you want impressive "magic"
Teams with limited QA resources
Not Ideal For:
Production monitoring (too slow)
Performance testing (AI adds overhead)
Sites requiring pixel-perfect element selection
Tests that must never have false positives
The Technical Details
For those interested in the implementation:
AI Integration
I use a simple HTTP client to communicate with Ollama's API:
var response = await httpClient.PostAsync(
"http://localhost:11434/api/generate",
jsonContent
);
The AI prompt is structured like this:
"You are a Selenium expert.
This locator failed: By.Id('searchBox')
Element description: Search input box
Here's the HTML: [truncated HTML]
Suggest a new locator that will work.
Return only the locator, nothing else."
Caching Strategy
I implemented a simple cache: if the same locator fails on the same page twice, use the AI's previous suggestion. This cuts healing time by 90% for repeated failures.
Error Handling
If all AI attempts fail, the test fails normally with a clear error message. No silent failures.
Extending the Framework
The framework is designed for customization:
Add New Sites
Create a new feature file in plain English. The self-healing works automatically.
Use Different AI
Switch to OpenAI by changing one configuration:
{
"Provider": "OpenAI",
"ApiKey": "sk-your-key",
"Model": "gpt-4o"
}
Customize Healing Logic
The self-healing engine is extensible. You can add custom strategies for specific element types.
Try It Yourself
I've made the complete framework available with:
Fully commented code
Working demo test
Setup instructions
Documentation
Everything is designed for beginners. Even if you've never written a Selenium test, you can get this running in 10 minutes.
The framework includes a Wikipedia search test with an intentionally wrong locator. Watch AI fix it in real-time.
Github Repo - https://github.com/aiqualitylab/SeleniumSelfHealing.Reqnroll
Key Takeaways
Self-healing is practical - Not science fiction, works today with free tools
Local AI is sufficient - No need for expensive cloud AI services
Context matters - Describe what you're looking for, not just the locator
It's a tool, not magic - Use for maintenance, not as an excuse for bad locators
Start simple - One test with self-healing proves the concept
Final Thoughts
Automated tests breaking due to UI changes is a solved problem. We have the technology to make tests that maintain themselves.
Is it perfect? No. Does it eliminate all test maintenance? No. But it reduces maintenance by 70%, and that's a massive win.
The future of test automation isn't writing better locators. It's writing tests that don't care when locators change.
Want to see it in action? The complete framework with demo test is ready to run. Three commands and you'll watch AI fix a broken test in real-time.
The code is extensively commented for learners. Every line is explained in plain English.
Have you tried AI in your test automation? What challenges did you face? I'd love to hear about your experience.
Tech Stack:
.NET 9
Selenium WebDriver
Reqnroll (BDD)
Ollama (Local AI) or GPT
NUnit
About the Framework
This is an educational project demonstrating AI-powered self-healing in test automation. It uses modern technologies and best practices while remaining accessible to beginners.
The goal isn't to replace good test design, but to show what's possible when AI assists automation engineers.
Thanks for reading! If you found this useful, you might also enjoy articles about BDD patterns, Selenium best practices, or AI tool integration.

Top comments (0)