Skip to main content

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.

PyPI version Python 3.10+ License: MIT


โœจ What It Does

When a Playwright test fails because a CSS selector or XPath no longer matches an element:

  1. Detects the broken selector and extracts the relevant DOM context
  2. Reasons about what the selector was supposed to do (using Groq/LLM)
  3. Suggests a corrected selector with confidence score
  4. Shows a rich TUI panel with the suggestion
  5. 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 page():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_page()

        enable_healing(page)                    # โ† Enable self-healing
        
        page.goto("https://www.your-app.com/")
        yield page
        browser.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-login with 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

self_healer-1.0.1.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

self_healer-1.0.1-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file self_healer-1.0.1.tar.gz.

File metadata

  • Download URL: self_healer-1.0.1.tar.gz
  • Upload date:
  • Size: 28.2 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

Hashes for self_healer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bf97624b086f26a0dfc001884c296169bb9850709c1fa7f17b1543fdda508054
MD5 ea3c60e4d53e948e2535f8f3e6f682c8
BLAKE2b-256 ec5c8c4c3e14233a492cb2b869f7c9e53eb3cc171391db9faa84a5faf9a6cbe2

See more details on using hashes here.

File details

Details for the file self_healer-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: self_healer-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 37.8 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

Hashes for self_healer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c0b9604ff55915a1842c750b43c41dfd26f5d180ce8824da64c668c23b63c608
MD5 1cb81f6bcdc013b555eaba30a7c5adfb
BLAKE2b-256 6040b547d41740e4d856c229653a91e94516556a7b3ab6ebbc946edf5647ef25

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page