Skip to main content
L3gitUI · API · MOBILE TEST AUTOMATION
[ PRODUCT ][ PRICING ][ DOCS ][ VERIFIED ][ REGULATED ][ COMPARE ][ ACCOUNT ] Start Free Trial

/// Resources / Self-healing

Self-healing Selenium locators in Java: how the scorer picks a replacement and logs the heal

Published 2026-11-117 min readL3git Development

A locator breaks when the page changes and the test does not. “Self-healing” is the promise that the test keeps running anyway, but most explanations stop at the word AI. This is the actual algorithm in the framework L3git generates: what it remembers, how it scores candidates, when it refuses to guess, and exactly what it writes down when it heals.

Step 1: remember what the element looked like

Healing needs something to compare against, so every successful find captures a Fingerprint of the element (once per logical key per run, to keep the happy path cheap) and stores it in Postgres keyed by a stable name such as login.submitButton, not by the locator:

public record Fingerprint(
        String tag, String id, String name,
        List<String> classList, String visibleText,
        Map<String, String> attrs,          // type, placeholder, aria-label, data-testid, role, href, alt, title
        String cssPath, String xpath,
        double centerX, double centerY, int domDepth) {}

The page object pairs that key with a primary locator. This is the one line that makes a page self-healing; the rest of the step code is unchanged:

driver.find(ByKey.of("login.submitButton"), By.id("submit")).click();

Step 2: try the cheap candidates first

When the primary locator matches nothing, the resolver does not scan the whole DOM straight away. A CandidateGenerator produces an ordered list: first every locator that has ever worked for this key (including previous heal winners, so a heal that happened last week is tried first this week), then locators synthesised from the stored fingerprint: the id, a [data-testid] selector, the name, and an XPath on the exact visible text. Each candidate is run with findElements, the first five matches are fingerprinted, and each is scored. A perfect 1.0 stops the search early.

Step 3: score each candidate against the fingerprint

This is the part that is usually hand-waved. The score is a weighted sum in 0..1 over the signals the stored fingerprint actually has, so an element that never had an aria-label is not penalised for lacking one:

W_ID = 0.30   (stable id; also data-testid)   W_TEXT     = 0.18 (Levenshtein ratio)
W_NAME = 0.12                                W_CLASSES  = 0.10 (Jaccard on class list)
W_TAG = 0.10                                 W_ATTRS    = 0.10 (exact match on tracked attrs)
W_POSITION = 0.05 (distance falloff)         W_DEPTH    = 0.05 (DOM depth, 6 levels = 0)

score = weighted / weightPresent;
if (!tagMatch) score = Math.min(score, 0.5);   // a <div> can never fully heal to a <button>

Two details matter in practice. Ids that look auto-generated (a six-character hex run containing a digit, configurable with healing.volatile.id.regex) are ignored, so a React build hash does not count for or against a candidate. And a tag mismatch is a hard cap at 0.5, which sits below the default threshold on purpose.

Step 4: fall back to a DOM scan, then apply the threshold

If no targeted candidate clears healing.threshold (default 0.72), the resolver fetches every element with the stored tag, capped at healing.domscan.limit (250), and scores them all. A DOM-scan winner is persisted as its concrete CSS path so it becomes reusable history next time. If the best score is still under the threshold the framework gives up and throws the ordinary NoSuchElementException with the best score in the message, which is what you want: a wrong click that passes is worse than a failed step.

Step 5: log what happened, with evidence

A heal is never silent. The winner is promoted to the top of the locator history, the page is screenshotted with the healed element outlined in red and scrolled into view, and a row is written to heal_event linked to the Cucumber step that was running:

CREATE TABLE heal_event (
    step_result_id  BIGINT REFERENCES step_result(id),
    logical_key     TEXT NOT NULL,      -- login.submitButton
    failed_by       TEXT NOT NULL,      -- id=submit
    healed_by_type  TEXT NOT NULL,      -- cssSelector
    healed_by_value TEXT NOT NULL,      -- [data-testid="login-submit"]
    score           NUMERIC(4,3),       -- 0.874
    strategy        TEXT NOT NULL,      -- HISTORY / FINGERPRINT / DOM_SCAN
    changes         TEXT,               -- id 'submit'->''; class +[btn-primary] -[btn]
    screenshot_path TEXT,
    healed_at       TIMESTAMPTZ DEFAULT now());

The changes column is a FingerprintDiff: tag, id, name, class additions and removals, text (“~similar” above 0.85, otherwise old to new), tracked attributes, and a position shift when it is over 20px. That is the line a developer reads in the morning to decide whether to update the locator or leave the heal in place.

What this does not do

If you already have a Selenium suite, new SelfHealingWebDriver(new ChromeDriver()) wraps your driver and heals every findElement keyed by the locator string, with no other code change. Projects recorded with L3git get the ByKey form generated into every page object. See the no-code Selenium page for what the recorder writes, and the proof page for a real heal with its screenshot.

Questions

Why 0.72 as the threshold?

It is above the 0.5 tag-mismatch cap and high enough that id-plus-text agreement alone passes while position-plus-class agreement alone does not. Raise healing.threshold for pages with many look-alike elements, lower it for sparse markup; it is a property, so you can set it per environment.

Does healing need internet access or an AI service?

No. Fingerprints, scoring and the heal log are all local Java and a Postgres database you own. It runs on air-gapped agents.

How do I see which locators healed in a CI run?

Query heal_event joined to step_result for the run id; each row carries the score, strategy, diff and screenshot path. The same facts are also written to the console log at INFO level as "Healed 'key' via STRATEGY (type=value) score=0.874", so a grep of the CI log works without the database. Repeated heals on the same logical_key are the signal to update the primary locator in the page object.

Try it: 14-day trial, no card More articles