-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
113 lines (98 loc) · 4.4 KB
/
Copy pathmain.py
File metadata and controls
113 lines (98 loc) · 4.4 KB
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
import argparse
import json
import logging
import os
import shutil
from dotenv import load_dotenv
import yaml
from pathlib import Path
from core.planner import generate_test_plan
from core.execute import run_ui_tests
from playwright.sync_api import sync_playwright
def load_config(config_path):
"""Load configuration from YAML file generated by the frontend."""
if not config_path or not Path(config_path).exists():
return {}
with open(config_path) as f:
return yaml.safe_load(f)
def create_github_summary(results):
"""Create a summary file for GitHub Action output."""
summary = {
"total_tests": len(results.get("tests", [])),
"tests_passed": sum(1 for test in results.get("tests", []) if test.get("status") == "passed"),
"tests_failed": sum(1 for test in results.get("tests", []) if test.get("status") == "failed"),
"failed_tests": [
{
"name": test.get("description", "Unknown test"),
"error": test.get("error", "Unknown error")
}
for test in results.get("tests", [])
if test.get("status") == "failed"
]
}
with open("results/summary.json", "w") as f:
json.dump(summary, f, indent=2)
return summary
def main():
# Load environment variables
load_dotenv()
parser = argparse.ArgumentParser(description="TestAgent - Automated UI Testing")
parser.add_argument("--config", type=str, help="Path to configuration YAML file generated by frontend")
parser.add_argument("--url", type=str, required=True, help="URL to test")
parser.add_argument("--browser", type=str, default="chromium", help="Browser to use (chromium, firefox, webkit)")
parser.add_argument("--headless", type=str, default="true", help="Run in headless mode")
parser.add_argument("--timeout", type=int, default=60, help="Test timeout in seconds")
parser.add_argument("--max-tests", type=int, default=2, help="Maximum number of tests to generate")
parser.add_argument("--output-format", type=str, choices=["console", "github-action"],
default="console", help="Output format")
args = parser.parse_args()
# Load configuration if provided
config = load_config(args.config)
# Determine URL - prioritize command line argument
url = args.url
print(f"✅ Running TestAgent on {url}")
# Set GitHub Action environment variables
if args.output_format == "github-action":
os.environ['GITHUB_ACTION_MODE'] = 'true'
os.environ['HEADLESS'] = args.headless
# Generate test plan - pass the config and command line args
test_config = {
'pages': config, # The frontend config is the pages directly
'browser': args.browser,
'timeout': args.timeout,
'max_tests': args.max_tests,
'headless': args.headless.lower() == 'true'
}
if os.path.exists("results"):
shutil.rmtree("results") # remove old directory and contents
os.makedirs("results", exist_ok=True)
# Configure logging
logging.basicConfig(
filename="./results/app.log", # Log file name
level=logging.INFO, # Minimum log level
format="%(asctime)s - %(levelname)s - %(message)s", # Log format
filemode='w'
)
with sync_playwright() as p:
headless = test_config['headless']
if test_config['browser'].lower() == 'firefox':
sync_browser = p.firefox.launch(headless=headless)
elif test_config['browser'].lower() == 'webkit':
sync_browser = p.webkit.launch(headless=headless)
else: # default to chromium
sync_browser = p.chromium.launch(headless=headless)
sync_context = sync_browser.new_context()
unit_tests = generate_test_plan(sync_context, url, config=test_config)
print("✅ Test plan generated")
# Run tests
test_results = run_ui_tests(sync_context, url, unit_tests, config=test_config)
print("✅ Tests executed")
sync_context.close()
sync_browser.close()
# Create GitHub Action summary if needed
if args.output_format == "github-action":
summary = create_github_summary(test_results)
print(f"✅ GitHub Action summary created: {summary['tests_passed']} passed, {summary['tests_failed']} failed")
print("🎉 TestAgent completed successfully")
if __name__ == "__main__":
main()