Skip to content

aiqualitylab/SeleniumSelfHealing.Reqnroll

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Selenium Self-Healing Tests with AI

A simple BDD test framework that uses AI to automatically fix broken element locators.

selenium-self-healing

What Is This?

One working test that demonstrates how AI can fix broken Selenium tests automatically. When an element locator fails, AI analyzes the page and suggests a new one!

✨ Features

  • βœ… One Working Test - Wikipedia search demo
  • βœ… AI Self-Healing - Automatically fixes broken locators
  • βœ… BDD with Reqnroll - Tests written in plain English
  • βœ… Free AI (Ollama) - Runs on your computer
  • βœ… Fully Commented Code - Easy to understand

πŸš€ Quick Start

What You Need

  1. .NET 9 SDK - https://dotnet.microsoft.com/download/dotnet/9.0
  2. Google Chrome - https://www.google.com/chrome/
  3. Ollama - https://ollama.ai

Installation (5 Minutes)

# 1. Install Ollama AI model
ollama pull qwen3-coder:480b-cloud

# 2. Navigate to project
cd SeleniumSelfHealing.Reqnroll.Net9

# 3. Build
dotnet restore
dotnet build

# 4. Run the test
dotnet test --logger "console;verbosity=detailed"

That's it! Watch AI fix the broken locator automatically! ✨

πŸ“ What's Inside

SeleniumSelfHealing.Reqnroll.Net9/
β”‚
β”œβ”€β”€ Features/
β”‚   └── WikipediaDemo.feature        ← Test in plain English
β”‚
β”œβ”€β”€ StepDefinitions/
β”‚   └── WikipediaDemoSteps.cs        ← Step implementations
β”‚
β”œβ”€β”€ Pages/
β”‚   └── WikipediaPage.cs             ← Page with WRONG locator (on purpose!)
β”‚
β”œβ”€β”€ Utilities/
β”‚   β”œβ”€β”€ LlmClient.cs                 ← AI communication (fully commented)
β”‚   β”œβ”€β”€ LlmConfig.cs                 ← AI settings (fully commented)
β”‚   └── WebDriverExtensions.cs      ← Self-healing magic (fully commented)
β”‚
└── Support/
    └── Hooks.cs                     ← Test setup/teardown

🎯 The Test

Features/WikipediaDemo.feature:

Feature: Wikipedia Search Demo

@demo @working
Scenario: Search for Selenium on Wikipedia
    Given I navigate to "https://www.wikipedia.org"
    When I search for "Selenium"
    Then the page should contain "Selenium"

Simple! Written in plain English!

πŸ€– How Self-Healing Works

The Intentional Bug

The test uses a WRONG locator on purpose:

// Pages/WikipediaPage.cs
private readonly By _searchBox = By.Id("searchBox");  // WRONG!
// Real ID is: "searchInput"

What Happens

1. Test tries By.Id("searchBox")
   ❌ Element not found!

2. AI analyzes the page HTML
   πŸ€– "I see an input with id='searchInput'"

3. AI suggests new locator
   πŸ’‘ "Try: //input[@id='searchInput']"

4. Test tries AI's suggestion
   βœ… Found it! Test continues!

Test Output

▢️ Step: When I search for "Selenium"
Element not found with By.Id: searchBox
Attempting self-healing for: Wikipedia search box
Asking AI for help (attempt 1/3)...
AI suggested: //input[@id='searchInput']
Self-healing successful! New locator works!
βœ… Entered text in: Wikipedia search box
βœ… Test Passed!

This proves AI self-healing works! πŸŽ‰

πŸŽ“ Understanding the Code

All AI code is fully commented for beginners:

// STEP 1: Try finding element the normal way first
try
{
    return driver.FindElement(locator);  // Try original locator
}
catch (NoSuchElementException)
{
    // Element not found - time for self-healing!
}

// STEP 2: Ask AI for help
var suggestedLocator = await aiClient.GetSuggestedLocator(
    pageHTML,           // The webpage code
    failedLocator,      // What didn't work
    "Search box"        // What we're looking for
);

// STEP 3: Try AI's suggestion
var element = driver.FindElement(By.XPath(suggestedLocator));
// βœ… It works!

βš™οΈ Configuration

appsettings.json - AI settings:

{
  "Provider": "Local",
  "BaseUrl": "http://localhost:11434",
  "Model": "qwen3-coder:480b-cloud",
  "Temperature": 0.1,
  "MaxTokens": 1000
}

Use OpenAI instead? Change to:

{
  "Provider": "OpenAI",
  "ApiKey": "sk-your-api-key-here",
  "BaseUrl": "https://api.openai.com/v1",
  "Model": "gpt-4o"
}

πŸ“Š Expected Results

Starting test execution...

Passed!  - Failed: 0, Passed: 1, Total: 1

βœ… 1 test runs
βœ… AI self-healing activates
βœ… Test passes

πŸ”§ Building Your Own Tests

Step 1: Create Feature File

Feature: My Test
    
Scenario: Test my website
    Given I navigate to "https://mysite.com"
    When I click the login button
    Then I should see the login form

Step 2: Implement Steps

[Given(@"I navigate to ""(.*)""")]
public async Task GivenINavigateTo(string url)
{
    _driver.Navigate().GoToUrl(url);
}

[When(@"I click the login button")]
public async Task WhenIClickLogin()
{
    // Uses self-healing automatically!
    await _driver.Click(By.Id("login"), "Login button");
}

Step 3: Run It!

dotnet test

AI will fix broken locators automatically! ✨

πŸ› Troubleshooting

"Connection refused to localhost:11434"

Ollama isn't running. Start it:

# Mac/Linux
ollama serve

# Windows - Click Ollama icon in system tray

"dotnet: command not found"

Install .NET 9 SDK from https://dotnet.microsoft.com/download/dotnet/9.0

Build errors

dotnet clean
dotnet restore
dotnet build

πŸ“š Commands

# Run test
dotnet test

# See detailed output (watch AI heal!)
dotnet test --logger "console;verbosity=detailed"

# Clean and rebuild
dotnet clean && dotnet restore && dotnet build

πŸ’‘ Key Points

  1. One Working Test - Wikipedia search demo
  2. Intentional Wrong Locator - Proves self-healing works
  3. Free AI - Uses Ollama (or paid OpenAI)
  4. Fully Commented - 430+ lines explaining everything
  5. Ready to Customize - Build your own tests easily

🎯 What This Demonstrates

βœ… AI automatically fixing broken locators
βœ… No manual intervention needed
βœ… Tests that heal themselves

πŸš€ Next Steps

  1. Run the demo - See AI self-healing work
  2. Read the comments - Understand how it works
  3. Create your own test - Use as template
  4. Customize - Add your own pages and scenarios

πŸ“– Documentation

  • This file - Simple overview
  • Code comments - Every line explained

⚑ Quick Reference

# Everything in 3 commands:
ollama pull qwen3-coder:480b-cloud
dotnet restore && dotnet build
dotnet test --logger "console;verbosity=detailed"

Ready to see AI fix your tests automatically? Run it now! πŸš€

For detailed explanations, check the commented code in Utilities/ folder!

Releases

No releases published

Packages

No packages published