Spaces:
Sleeping
Sleeping
File size: 6,877 Bytes
6f38c76 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
"""
Main Entry Point - LangGraph UI Regression Testing System
Hybrid Approach: Screenshots + HF Vision + CSS Extraction
"""
import os
import sys
import argparse
from datetime import datetime
# Load environment variables from .env file manually
def load_env_file(env_path: str = ".env"):
"""Load environment variables from .env file."""
if os.path.exists(env_path):
with open(env_path, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key.strip()] = value.strip()
load_env_file()
from state_schema import create_initial_state, WorkflowState
from agents.agent_0_super_agent import agent_0_node
from agents.agent_1_design_inspector import agent_1_node
from agents.agent_2_website_inspector import agent_2_node
from agents.agent_3_difference_analyzer import agent_3_node
from screenshot_annotator import annotate_all_screenshots
from report_generator import ReportGenerator
def get_credentials():
"""Get credentials from environment variables."""
figma_key = os.getenv("FIGMA_FILE_KEY", "")
figma_token = os.getenv("FIGMA_ACCESS_TOKEN", "")
website_url = os.getenv("WEBSITE_URL", "")
hf_token = os.getenv("HUGGINGFACE_API_TOKEN", "")
if not figma_key:
print("β Error: FIGMA_FILE_KEY not set in .env")
sys.exit(1)
if not figma_token:
print("β Error: FIGMA_ACCESS_TOKEN not set in .env")
sys.exit(1)
if not website_url:
print("β Error: WEBSITE_URL not set in .env")
sys.exit(1)
return figma_key, figma_token, website_url, hf_token
def run_workflow(figma_file_key: str, figma_access_token: str, website_url: str, hf_token: str, execution_id: str) -> WorkflowState:
"""Run the complete workflow."""
# Create initial state
state = create_initial_state(
figma_file_key=figma_file_key,
figma_access_token=figma_access_token,
website_url=website_url,
hf_token=hf_token,
execution_id=execution_id
)
print("\n" + "="*60)
print("π Starting UI Regression Testing Workflow")
print("="*60)
# Agent 0: Super Agent (Test Plan)
print("\nπ€ Agent 0: Super Agent - Generating Test Plan...")
agent_0_result = agent_0_node(state)
state.test_plan = agent_0_result.get("test_plan", {})
state.test_categories = agent_0_result.get("test_categories", state.test_categories)
print(f" β Test plan generated")
# Agent 1: Design Inspector (Figma Screenshots)
print("\nπ¨ Agent 1: Design Inspector - Capturing Figma Screenshots...")
agent_1_result = agent_1_node(state.__dict__)
state.design_screenshots = agent_1_result.get("design_screenshots", {})
state.status = agent_1_result.get("status", state.status)
if state.status == "design_inspection_failed":
print(f" β Agent 1 failed")
return state
# Agent 2: Website Inspector (Website Screenshots)
print("\nπ Agent 2: Website Inspector - Capturing Website Screenshots...")
agent_2_result = agent_2_node(state.__dict__)
state.website_screenshots = agent_2_result.get("website_screenshots", {})
state.status = agent_2_result.get("status", state.status)
if state.status == "website_inspection_failed":
print(f" β Agent 2 failed")
return state
# Agent 3: Difference Analyzer (Screenshot Comparison)
print("\nπ Agent 3: Difference Analyzer - Analyzing Visual Differences...")
agent_3_result = agent_3_node(state.__dict__)
state.visual_differences = agent_3_result.get("visual_differences", [])
state.similarity_score = agent_3_result.get("similarity_score", 0)
state.status = agent_3_result.get("status", state.status)
if state.status == "analysis_failed":
print(f" β Agent 3 failed")
return state
# Annotate screenshots
print("\nπΈ Annotating Screenshots...")
state = annotate_all_screenshots(state)
print("\n" + "="*60)
print("β
Workflow Completed")
print("="*60)
return state
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(description="UI Regression Testing System")
parser.add_argument("--execution-id", default="", help="Execution ID")
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
parser.add_argument("--output-dir", default="reports", help="Output directory for reports")
args = parser.parse_args()
# Print header
print("\n" + "="*70)
print("π UI REGRESSION TESTING SYSTEM (Hybrid Approach)")
print("="*70)
# Get credentials
print("\nπ Configuration:")
figma_key, figma_token, website_url, hf_token = get_credentials()
print(f" Figma File Key: {figma_key[:20]}...")
print(f" Website URL: {website_url}")
print(f" Output Directory: {args.output_dir}")
print(f" Debug Mode: {args.debug}")
print(f" HF Token: {'β Enabled' if hf_token else 'β Disabled'}")
print("\n" + "="*70)
# Generate execution ID if not provided
execution_id = args.execution_id or f"exec_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
# Run workflow
try:
final_state = run_workflow(
figma_file_key=figma_key,
figma_access_token=figma_token,
website_url=website_url,
hf_token=hf_token,
execution_id=execution_id
)
# Generate reports
if final_state.status == "analysis_complete":
ReportGenerator.generate_all_reports(final_state, args.output_dir)
# Print summary
print("\n" + "="*70)
print("β
EXECUTION COMPLETED SUCCESSFULLY")
print("="*70)
print(f"\nπ Results:")
print(f" Total Differences: {len(final_state.visual_differences)}")
print(f" High Severity: {len([d for d in final_state.visual_differences if d.severity == 'High'])}")
print(f" Medium Severity: {len([d for d in final_state.visual_differences if d.severity == 'Medium'])}")
print(f" Low Severity: {len([d for d in final_state.visual_differences if d.severity == 'Low'])}")
print(f" Similarity Score: {final_state.similarity_score:.1f}/100")
print(f"\nπ Reports saved to: {args.output_dir}/")
print("\n" + "="*70)
else:
print(f"\nβ Workflow failed: {final_state.error_message}")
sys.exit(1)
except Exception as e:
print(f"\nβ Error: {str(e)}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()
|