AI-powered self-healing pytest plugin for Playwright โ automatically fixes broken selectors using LLM reasoning
Project description
๐ฉบ Self-Healer
AI-powered self-healing pytest plugin for Playwright โ automatically detects broken selectors in your tests and uses LLM reasoning to suggest (and optionally apply) fixes.
โจ What It Does
When a Playwright test fails because a CSS selector or XPath no longer matches an element:
- Detects the broken selector and extracts the relevant DOM context
- Reasons about what the selector was supposed to do (using Groq/LLM)
- Suggests a corrected selector with confidence score
- Shows a rich TUI panel with the suggestion
- Applies the fix directly to your source code (with your approval!)
All of this happens automatically โ just install and run your tests.
๐ Quick Start
Step 1: Install
pip install self-healer
Step 2: Set Environment Variables
# Required
API_KEY="sk_..."
LLM_MODEL="openai/gpt-4.1-mini"
# Optional (these have defaults)
BASE_URL="https://api.openai.com"
TEMPERATURE="0.4"
Step 3: Add to your conftest.py
# conftest.py
import pytest
from playwright.sync_api import sync_playwright
from dotenv import load_dotenv
load_dotenv()
from self_healer import enable_healing
@pytest.fixture(scope="session")
def browser_instance():
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
yield browser
browser.close()
@pytest.fixture(scope="function")
def page(browser_instance):
pg = browser_instance.new_page()
enable_healing(pg) # โ Enable self-healing
pg.goto("https://your-app.com")
yield pg
pg.close()
Step 4: Write Tests (normal Playwright)
# tests/test_login.py
from playwright.sync_api import Page
def test_login(page: Page):
page.fill("#user-name", "admin")
page.fill("#password", "secret")
page.click("#login-button")
assert "dashboard" in page.url
Step 5: Run
pytest -v
If #login-button changes to #btn-login, the agent will:
- Detect the failure
- Analyze the live DOM
- Suggest
#btn-loginwith high confidence - Ask you to Accept, Reject, or Copy the fix
โ๏ธ Configuration
All configuration is via environment variables โ no config files needed.
| Variable | Required | Default | Description |
|---|---|---|---|
API_KEY |
โ | โ | Your API key |
LLM_MODEL |
โ | โ | LLM model name |
BASE_URL |
โ | https://api.openai.com |
API endpoint URL |
TEMPERATURE |
โ | 0.4 |
LLM temperature (0.0โ1.0) |
๐๏ธ How It Works
Test Fails (broken selector)
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Detect Failure โ pytest hook intercepts the error
โโโโโโโโโโฌโโโโโโโโโ
โโโโโโโโโโดโโโโโโโโโโ
โผ โผ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ
โ DOM Extractor โ โ XPath Builder โ (for dynamic sites)
โโโโโโโโโโฌโโโโโโโโโ โโโโโโโโโโฌโโโโโโโโโโ
โโโโโโโโโโโโโฌโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโ
โ LLM Reasoning โ Groq/LLaMA analyzes intent + DOM
โโโโโโโโโโฌโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโ
โ File Locator โ Finds exact file:line of selector
โโโโโโโโโโฌโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโ
โ Human Approval โ Rich TUI: Accept / Reject / Copy
โโโโโโโโโโฌโโโโโโโโโโ
โโโโโโโโโโดโโโโโโโโโโ
Yes No
โ โ
โผ โผ
โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโ
โ Apply Fix โ โ Reject Fix โ
โโโโโโฌโโโโโโโโโโโโ โโโโโโโโโฌโโโโโโโโ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โผ
โโโโโโโโโโโ
โ END โ
โโโโโโโโโโโ
๐ฆ For Package Developers
Building from source
git clone https://github.com/ankan01-cbnits/self_healing_agent.git
cd self-healing_agent
uv sync
uv build
Publishing to PyPI
# Test on TestPyPI first
pip install twine
twine upload --repository testpypi dist/*
# Then publish to real PyPI
twine upload dist/*
๐ License
MIT License โ see LICENSE for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file self_healer-1.0.0.tar.gz.
File metadata
- Download URL: self_healer-1.0.0.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9ee7935872ecaf13ebaa606ca25a926fca711741568f80d42e564a0e6dcc5f0
|
|
| MD5 |
3a2c4115520d6fe1ad328928c84703fb
|
|
| BLAKE2b-256 |
ee127bb99362ffc5f7322975441c34be69cfd2ca847ac281ec966f51fad1d5f4
|
File details
Details for the file self_healer-1.0.0-py3-none-any.whl.
File metadata
- Download URL: self_healer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 31.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55f7e93afc0f4f0187d594363faff91445279fb94c41c9020f75fed0d6b1bb7e
|
|
| MD5 |
8af705894e412427269a7170497e402a
|
|
| BLAKE2b-256 |
3edb227f774eff61b0dd37fb3d42c41f80948c1dce4faca37dcef9c3495674e4
|