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
playwright install chromium

Step 2: Set Environment Variables

# Required
export GROQ_API_KEY="gsk_your_api_key_here"

# Optional (these have defaults)
export GROQ_BASE_URL="https://api.groq.com/openai/v1"
export SELF_HEAL_MODEL="llama-3.3-70b-versatile"
export SELF_HEAL_TEMPERATURE="0.4"

On Windows (PowerShell):

$env:GROQ_API_KEY = "gsk_your_api_key_here"

Step 3: Add to your conftest.py

# conftest.py
import pytest
from playwright.sync_api import sync_playwright
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 -s

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
GROQ_API_KEY โœ… โ€” Your Groq API key
GROQ_BASE_URL โŒ https://api.groq.com/openai/v1 API endpoint URL
SELF_HEAL_MODEL โŒ llama-3.3-70b-versatile LLM model name
SELF_HEAL_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 the exact file:line of the selector
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ–ผ
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Human Approval  โ”‚  Rich TUI: Accept / Reject / Copy
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ–ผ
         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
         โ”‚  Apply Fix       โ”‚  Edits the source file + opens editor
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ฆ For Package Developers

Building from source

git clone https://github.com/your-username/self-healer.git
cd self-healer
pip install build
python -m build

Publishing to PyPI

# Test on TestPyPI first
pip install twine
twine upload --repository testpypi dist/*

# Then publish to real PyPI
twine upload dist/*

Running the example project

pip install -e .
playwright install chromium
export GROQ_API_KEY="gsk_..."
pytest examples/saucedemo/tests/ -v -s

๐Ÿค Contributing

Contributions are welcome! Please open an issue or pull request.


๐Ÿ“„ 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-0.1.1.tar.gz (23.3 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-0.1.1-py3-none-any.whl (31.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: self_healer-0.1.1.tar.gz
  • Upload date:
  • Size: 23.3 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-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b7754455fc6776c8762ba5c7dbf68e59924ff44efe13a0e60ac561fd38737834
MD5 7abe2bdd3dc1520fbca229e162a9fbe6
BLAKE2b-256 34fbd73b7e426efe7c9d56ce4e94bf9d7869e99129cad850f363d3dc4ef3dd5d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: self_healer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 31.7 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-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f0059ce399832ed0f9beb0f8d842fe385fc5fdc003f3fc945c875d7171e5954
MD5 7636bb212c26429b8bc55413d62599f1
BLAKE2b-256 d7b0fbb3c49fd021873fc5c5572eca4bf2ce566ee8a635a32c6264411357c77d

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