Automation tests often fail when locators break due to changes in the application. To make your tests more stable, you can build a custom fallback rule in Selenium that automatically recovers from such failures. This helps reduce flaky tests and maintenance effort.
In this blog, we’ll build a simple and custom fallback mechanism in Java + Selenium that automatically tries alternate locators if the main one fails — this is what we call Self-Healing Test Automation.
This blog is ideal for automation testers who want to build basic to intermediate-level fallback logic from scratch using only open-source tools.
Prerequisites
Make sure the following tools are installed on your system (Ubuntu/Linux):
- Java (17 or later)
- Maven
- Git
- IntelliJ IDEA (Community or Ultimate version)
Create a Maven Project
We’ll use Maven to manage our dependencies.
Open your terminal and run the following command:
mvn archetype:generate -DgroupId=com.selfhealing -DartifactId=SelfHealingTest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Go to the project folder:
cd SelfHealingTest
Project Structure
Once created, your basic folder structure will look like this:
SelfHealingTest/
├── src/
│ └── main/java/com/selfhealing/
│ ├── FallbackLocatorEngine.java
│ ├── LocatorRule.java
│ └── SampleTest.java
└── pom.xml
Add Selenium Dependency
Open the pom.xml file and add the following inside the <dependencies> tag:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.18.1</version>
</dependency>
</dependencies>
Step 1: Define a Rule Model
Create a new file: src/main/java/com/selfhealing/LocatorRule.java
package com.selfhealing;
public class LocatorRule {
private String type; // css, xpath, id
private String value;
public LocatorRule(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() { return type; }
public String getValue() { return value; }
}
This class represents a locator rule like (“id”, “username”) or (“xpath”, “//input[@name=’user’]”).
Step 2: Create the Fallback Locator Engine
Create the file: src/main/java/com/selfhealing/FallbackLocatorEngine.java
package com.selfhealing;
import org.openqa.selenium.*;
import java.util.List;
public class FallbackLocatorEngine {
WebDriver driver;
public FallbackLocatorEngine(WebDriver driver) {
this.driver = driver;
}
public WebElement findElementWithFallback(List<LocatorRule> rules) {
for (LocatorRule rule : rules) {
try {
switch (rule.getType()) {
case "id":
return driver.findElement(By.id(rule.getValue()));
case "css":
return driver.findElement(By.cssSelector(rule.getValue()));
case "xpath":
return driver.findElement(By.xpath(rule.getValue()));
case "name":
return driver.findElement(By.name(rule.getValue()));
}
} catch (NoSuchElementException e) {
// Try next rule
}
}
throw new NoSuchElementException("Element not found with fallback rules");
}
}
This is the core engine that checks one locator after another until it finds a valid one.
Step 3: Sample Usage
Let’s create a test that uses our fallback engine on the Google homepage.
Create the file: src/main/java/com/selfhealing/SampleTest.java
package com.selfhealing;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Arrays;
public class SampleTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
FallbackLocatorEngine engine = new FallbackLocatorEngine(driver);
WebElement searchBox = engine.findElementWithFallback(Arrays.asList(
new LocatorRule("id", "fake_id"), // broken
new LocatorRule("name", "q"), // correct
new LocatorRule("css", "input[title='Search']")
));
searchBox.sendKeys("Self-Healing in Selenium\n");
driver.quit();
}
}
Replace “/path/to/chromedriver” with the actual path to your ChromeDriver.
This code tries an invalid ID first, then falls back to a working name=”q” locator.
Step 4: Run the Project
You can run your test using IntelliJ or directly using Maven CLI:
mvn compile exec:java -Dexec.mainClass="com.selfhealing.SampleTest"

Make sure you have the exec-maven-plugin added in your pom.xml if not already.
Conclusion
In this blog, we learned how to build a simple and custom fallback rule library in Java using Selenium. We covered:
- What are fallback rules, and why do we need them
- Creating a custom locator model
- Writing a fallback locator engine
- Using it in a test scenario to recover from broken selectors
This can be a great addition to your test automation framework and can help in making your tests less flaky and more robust.
🔗 Source Code Reference:
You can access the full working implementation on GitHub here:
👉 https://github.com/ShivamSinghVaranasi/selenium-self-healing-demo