Skip to content

BetterWorks/playwright-ai-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– Playwright AI Self-Healing Agent

An autonomous AI agent that automatically detects, analyzes, and fixes failing Playwright tests in GitHub Actions workflows. This agent uses OpenAI's GPT models to understand test failures, generate targeted fixes, verify them through CI, and create draft pull requestsβ€”all without human intervention.

πŸš€ Quick Start

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
export GITHUB_TOKEN="ghp_your_github_token"
export OPENAI_API_KEY="sk_your_openai_key"

# Run the agent
python main.py https://github.com/owner/repo/actions/runs/12345678

✨ Features

  • πŸ” Automatic Failure Detection: Monitors GitHub Actions workflows and identifies all test failures
  • 🧠 AI-Powered Analysis: Uses OpenAI to analyze failure logs and understand root causes
  • πŸ› οΈ Surgical Code Fixes: Generates precise, targeted fixes for failing tests
  • πŸ”„ Self-Healing Loop: Automatically retries fixes up to 3 times, learning from previous failures
  • βœ… CI Verification: Creates branches and validates fixes through GitHub Actions
  • πŸ“‹ Draft PR Creation: Automatically creates draft PRs with verified fixes
  • 🏒 Monorepo Support: Handles complex monorepo structures with path normalization
  • 🎯 Multi-Issue Resolution: Processes multiple distinct failures independently

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  GitHub Actions β”‚
β”‚   (Failed Run)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  GitHubClient   │◄─── Fetches logs, metadata, diffs
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  LogProcessor   │◄─── Extracts relevant failure info
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    AIAgent      │◄─── Analyzes & generates fixes
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Self-Healing   β”‚
β”‚      Loop       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1. Generate Fix β”‚
β”‚ 2. Create Branchβ”‚
β”‚ 3. Push Changes β”‚
β”‚ 4. Run CI       β”‚
β”‚ 5. Verify       β”‚
β”‚ 6. Retry if ❌  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Draft PR      β”‚
β”‚   βœ… Success    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Getting Started

Prerequisites

  • Python 3.8+
  • GitHub Personal Access Token with appropriate permissions:
    • repo (full control)
    • workflow (to trigger workflows)
  • OpenAI API Key

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd playwright-ai-agent
  2. Install dependencies:

    pip install -r requirements.txt
  3. Configure environment variables: Create a .env file in the root directory:

    GITHUB_TOKEN=ghp_your_github_token_here
    OPENAI_API_KEY=sk-your_openai_api_key_here

Usage

  1. Run the agent with a failed workflow URL:

    python main.py https://github.com/owner/repo/actions/runs/12345678
  2. Monitor the process: The agent will:

    • πŸ” Identify all distinct failures
    • πŸ› οΈ Process each issue independently
    • πŸ”„ Retry failed fixes up to 3 times
    • βœ… Create draft PRs for successful fixes

πŸ“¦ Project Structure

playwright-ai-agent/
β”œβ”€β”€ main.py                 # Entry point and orchestration logic
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ .env                    # Environment variables (create this)
└── agent/
    β”œβ”€β”€ __init__.py
    β”œβ”€β”€ ai_agent.py        # OpenAI integration for analysis & fix generation
    β”œβ”€β”€ github_client.py   # GitHub API wrapper
    β”œβ”€β”€ log_processor.py   # Log parsing and cleanup
    └── models.py          # Pydantic models for structured data

πŸ”§ Configuration

Retry Settings

Adjust the maximum retry attempts in main.py:

MAX_RETRIES = 3  # Number of fix attempts per issue

AI Model

Configure the OpenAI model in agent/ai_agent.py:

self.model = "gpt-5-mini"  # or "gpt-4", "gpt-4-turbo", etc.

CI Timeout

Modify the CI verification timeout in main.py:

timeout = 900  # 15 minutes (in seconds)

πŸ§ͺ How It Works

1. Failure Identification

The agent parses GitHub Actions logs and identifies distinct test failures with:

  • File path and line number
  • Error category (timeout, assertion, etc.)
  • Stack traces and error messages

2. Intelligent Fix Generation

Using OpenAI, the agent:

  • Analyzes the failure context
  • Reviews the original code
  • Generates a surgical fix following best practices
  • Avoids generic solutions (e.g., blanket waitForTimeout calls)

3. Verification Loop

For each fix:

Generate Fix β†’ Create Branch β†’ Push Code β†’ Trigger CI β†’ Wait
                                                          β”‚
                                                          β–Ό
                                                      Success?
                                                      β”‚     β”‚
                                                     Yes    No
                                                      β”‚     β”‚
                                                Draft PR  Retry
                                                          (with error context)

4. Monorepo Handling

The agent automatically normalizes file paths:

  • Log path: tests/login.spec.ts
  • Actual path: packages/playwright/tests/login.spec.ts

πŸ“Š Example Output

πŸ€– Starting Agent for Run: 21857019114 (push)
πŸ” Identifying all distinct failures...
πŸ“Š Found 2 issues to address.

πŸ› οΈ  Processing Issue in: tests/auth/login.spec.ts
   🧠 Attempt 1/3: Generating surgical fix...
   πŸ§ͺ Waiting for CI verification on branch: ai-fix-a3b92f...
   ⏳ CI still in progress... (30s)
   ⏳ CI still in progress... (60s)
   βœ… SUCCESS: Fix verified by CI.
   πŸš€ Draft PR Created: https://github.com/owner/repo/pull/123

πŸ› οΈ  Processing Issue in: tests/checkout/payment.spec.ts
   🧠 Attempt 1/3: Generating surgical fix...
   πŸ§ͺ Waiting for CI verification on branch: ai-fix-c7d45e...
   ❌ FAIL: Attempt 1 did not resolve the issue.
   πŸ” Fetching new logs for re-analysis...
   🧠 Attempt 2/3: Generating surgical fix...
   ...

πŸ›‘οΈ Best Practices & Safety

  • Draft PRs: All PRs are created as drafts to allow human review before merging
  • Branch Isolation: Each fix gets a unique branch to avoid conflicts
  • CI Gating: Fixes are only proposed after passing CI verification
  • Retry Logic: Failed fixes are retried with additional context from new logs
  • Monorepo Aware: Handles complex repository structures

πŸ” Security Considerations

  • Never commit .env file to version control
  • Use GitHub tokens with minimal required permissions
  • Rotate API keys regularly
  • Review all draft PRs before merging
  • Consider rate limiting for API calls

🀝 Contributing

Contributions are welcome! Areas for improvement:

  • Support for additional test frameworks (Jest, Cypress, etc.)
  • Parallel processing of multiple failures
  • Cost tracking for OpenAI API calls
  • Webhook-based automatic triggering
  • Enhanced diff analysis for regression detection
  • Support for flaky test identification

πŸ“ License

[Add your license here]

πŸ™ Acknowledgments

  • Built on OpenAI's GPT models
  • Utilizes GitHub REST API
  • Designed for Playwright test automation

Note: This agent uses AI to generate code fixes. Always review generated changes before merging to production.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages