UI测试代理系统-模块化结构
📁 项目结构
ui-testing-agents/
├── models.py # Data models (TestStep, TestPlan, ExecutionResult)
├── ollama_client.py # Ollama LLM client
├── planner_agent.py # Planner Agent (creates test plans)
├── executor_agent.py # Executor Agent (runs tests with Vibium)
├── validator_agent.py # Validator Agent (validates results)
├── orchestrator.py # Orchestrator (coordinates all agents)
├── main.py # Main entry point
└── requirements.txt # Python dependencies🎯 文件描述
核心组件
models.py
- 包含所有数据结构
TestStep:单个测试步骤TestPlan:完整的测试计划,包括步骤ExecutionResult:执行步骤的结果
ollama_client.py
- 处理与Ollama LLM的通信
- 方法:
generate(),check_availability()
planner_agent.py
- 规划师代理人:从自然语言创建测试计划
- 接受用户提示并生成结构化测试计划
- 处理JSON解析和清理
- 关键方法:
create_plan(user_prompt)
executor_agent.py
- Vibium执行器代理:使用Vibium执行测试步骤
- 处理浏览器初始化和控制
- 执行操作:导航、单击、键入、验证、等待、滚动
- 关键方法:
execute_step(step)
validator_agent.py
- 验证代理:验证测试执行结果
- 分析结果并提供见解
- 生成建议
- 关键方法:
validate(test_plan, results)
orchestrator.py
- 振动测试编排器:协调所有三个代理
- 管理完整的测试工作流程
- 关键方法:
run_test(user_prompt)
main.py
- 运行测试的入口点
- 示例测试提示
- 自定义测试的辅助功能
🚀 快速开始
1.安装依赖项
pip install requests vibium
playwright install chromium2.启动Olama
ollama pull llama3.2:3b
ollama serve3.运行测试
python main.py💡 用法示例
基本用法
import asyncio
from orchestrator import VibiumUITestingOrchestrator
async def run_test():
orchestrator = VibiumUITestingOrchestrator(
ollama_model="llama3.2:3b",
headless=False,
debug=True
)
prompt = "Navigate to google.com and search for 'Python'"
result = await orchestrator.run_test(prompt)
await orchestrator.cleanup()
asyncio.run(run_test())使用个人代理
from ollama_client import OllamaClient
from planner_agent import PlannerAgent
# Create a test plan
llm = OllamaClient(model="llama3.2:3b")
planner = PlannerAgent(llm)
plan = planner.create_plan("Test Google search", debug=True)
print(f"Objective: {plan.objective}")
print(f"Steps: {len(plan.steps)}")具有辅助功能的自定义测试
from main import run_custom_test
result = run_custom_test(
prompt="Test Wikipedia homepage",
model="llama3.2:3b",
headless=True
)
print(f"Status: {result['validation']['overall_status']}")🔧 定制
添加新操作
编辑 executor_agent.py 并添加新方法:
async def _execute_your_action(self, step: TestStep) -> ExecutionResult:
"""Your custom action"""
try:
# Implementation here
return ExecutionResult(
step_number=step.step_number,
action=step.action,
success=True,
message="Action completed"
)
except Exception as e:
return ExecutionResult(
step_number=step.step_number,
action=step.action,
success=False,
message=f"Action failed: {e}"
)然后更新 execute_step():
async def execute_step(self, step: TestStep) -> ExecutionResult:
# ... existing code ...
elif step.action == "your_action":
return await self._execute_your_action(step)修改代理行为
每个代理都是独立的,可以单独修改:
- 规划师:编辑
planner_agent.py→system_prompt - 执行者:编辑
executor_agent.py→ 行动方法 - 验证器:编辑
validator_agent.py→system_prompt
📊 数据流
User Prompt
↓
PlannerAgent → TestPlan (with TestSteps)
↓
ExecutorAgent → List[ExecutionResult]
↓
ValidatorAgent → Validation Report
↓
Final Results🎨 模块化结构的优点
- 关注点分离:每个代理人都有一个责任
- 易于测试:独立测试试剂
- 可维护性:更改隔离到特定文件
- 可重用性:在不同的环境中使用代理
- 可扩展性:轻松添加新代理
🧪 测试单个组件
仅限测试计划员
from ollama_client import OllamaClient
from planner_agent import PlannerAgent
llm = OllamaClient()
planner = PlannerAgent(llm)
plan = planner.create_plan("Test login form")
for step in plan.steps:
print(f"{step.step_number}. {step.action} -> {step.target}")仅限测试执行者
import asyncio
from ollama_client import OllamaClient
from executor_agent import VibiumExecutorAgent
from models import TestStep
async def test_executor():
llm = OllamaClient()
executor = VibiumExecutorAgent(llm, headless=False)
step = TestStep(
step_number=1,
action="navigate",
target="https://google.com",
value=""
)
result = await executor.execute_step(step)
print(f"Result: {result.success} - {result.message}")
await executor.close_browser()
asyncio.run(test_executor())仅限测试验证器
from ollama_client import OllamaClient
from validator_agent import ValidatorAgent
from models import TestPlan, TestStep, ExecutionResult
llm = OllamaClient()
validator = ValidatorAgent(llm)
# Create mock data
plan = TestPlan(
objective="Test search",
steps=[TestStep(1, "navigate", "google.com")],
success_criteria=["Page loads"]
)
results = [
ExecutionResult(1, "navigate", True, "Success")
]
validation = validator.validate(plan, results)
validator.print_validation_report(validation)📝 配置
所有配置都是通过编排器完成的:
orchestrator = VibiumUITestingOrchestrator(
ollama_model="llama3.2:3b", # Change LLM model
headless=True, # Headless browser
debug=False # Debug output
)🐛 调试
启用调试模式以查看详细输出:
orchestrator = VibiumUITestingOrchestrator(debug=True)这表明:
- 原始LLM响应
- 提取的JSON
- 分步执行细节
🔄 与其他工具集成
与Pytest一起使用
import pytest
import asyncio
from orchestrator import VibiumUITestingOrchestrator
@pytest.mark.asyncio
async def test_google_search():
orchestrator = VibiumUITestingOrchestrator(headless=True)
result = await orchestrator.run_test("Test Google search")
await orchestrator.cleanup()
assert result['validation']['overall_status'] == 'PASS'在CI/CD中使用
import os
from main import main
if __name__ == "__main__":
if os.environ.get('CI'):
print("Skipping UI tests in CI")
else:
asyncio.run(main())📚 依赖项
requests>=2.31.0
vibium>=0.1.0
playwright>=1.40.0🎯 后续步骤
- ✅ 安装依赖项
- ✅ 启动Ollama
- ✅ 跑
python main.py - ✅ 根据您的需求定制代理
- ✅ 添加新操作或修改现有操作
💡 提示
- 从开始
debug=True了解流程 - 在完全集成之前独立测试代理
- 使用无头模式以加快执行速度
- 自定义系统提示以获得更好的结果
- 为特定用例添加自己的操作
测试愉快! 🚀
