Self-healing test scripts are designed to automatically detect and correct failures caused by minor changes in the UI (like a changed ID, class name, or CSS path) without human intervention.
While Mocha (the test runner) and Chai (the assertion library) don’t have built-in self-healing capabilities, you can implement a self-healing logic layer within your test suite using a “Selector Registry” or “Multi-Locator” strategy.
The core idea is to move away from a single hardcoded selector. Instead, you provide a prioritized list of attributes for an element. If the primary selector fails, the script “heals” itself by finding the element via secondary attributes and updating the registry.
Instead of just using id="submit-btn", you store a metadata object for the element:
const loginButton = {
primary: '#submit-btn',
fallbacks: [
"//button[contains(text(), 'Login')]",
".btn-primary",
"[data-testid='login-button']"
]
};
You can create a helper function that wraps your element finding logic. If the first selector fails, it loops through the fallbacks until it finds a match.
// A conceptual wrapper for Playwright/Puppeteer/Selenium used with Mocha
async function smartFind(page, elementMetadata) {
try {
return await page.waitForSelector(elementMetadata.primary, { timeout: 2000 });
} catch (error) {
console.warn(`Primary selector ${elementMetadata.primary} failed. Attempting self-healing...`);
for (let fallback of elementMetadata.fallbacks) {
const element = await page.$(fallback);
if (element) {
console.log(`Successfully healed using: ${fallback}`);
// Optional: Trigger a webhook to update your selector database
return element;
}
}
throw new Error("Self-healing failed: No backup selectors found.");
}
}
In your actual test file, the code remains clean. Mocha handles the execution flow, while Chai asserts the final state.
describe('Login Flow with Self-Healing', () => {
it('should click the login button even if ID changed', async () => {
const btn = await smartFind(page, loginButton);
await btn.click();
const title = await page.title();
// Chai assertion
expect(title).to.equal('Dashboard');
});
});
If you don’t want to build the logic yourself, there are plugins and tools that integrate with the Mocha/Chai ecosystem:
| Feature | Benefit | Risk |
|---|---|---|
| Resilience | Tests don’t break over minor CSS changes. | Tests might pass on the “wrong” element if selectors are too vague. |
| Maintenance | Reduces “false positive” failures. | Can hide technical debt or unintentional UI regressions. |
| Efficiency | DevOps pipelines keep running. | Debugging why it healed can sometimes be time-consuming. |