测试自动化平台

企业级测试自动化平台,具备AI驱动的测试生成、自修复能力和全面的录制功能。

  • by keerthika-janardhanan
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 keerthika-janardhanan/mcp_integration

使用说明

集成MCP的测试自动化平台

企业级测试自动化平台,具有人工智能驱动的测试生成、自愈功能和全面的记录功能。

🚀 主要特点

核心能力

  • AI驱动的测试生成:根据记录的流程生成剧作家测试脚本
  • 自我修复测试:使用AI和网络搜索自动修复失败的定位器
  • 智能系统监控:使用丰富的元数据记录用户交互
  • 痕量:将捕获的操作与实际浏览器事件进行比较,以诊断缺失的步骤
  • 增强突变观察器:捕获程序化DOM更改(复选框、按钮、自定义组件)
  • 向量数据库:用于测试生成的智能上下文检索
  • 多框架支持:剧作家、柏树、硒
  • Excel集成:使用数据映射生成手动测试用例

MCP集成(新增!)

  • 微软文档MCP:官方剧作家文档和代码示例
  • GitHub MCP:在存储库中搜索测试模式
  • 文件系统MCP:通过自动备份实现安全的文件操作
  • 剧作家测试MCP:浏览器自动化和测试生成

自愈:使用剧作家测试治疗师代理方法(100%免费)

📋 先决条件

  • Python 3.10+
  • Node.js 18+ (适用于MCP服务器和前端)
  • Git
  • 剧作家浏览器 (通过脚本安装)

🛠️ 快速设置

1.克隆存储库

git clone <repository-url>
cd mcp_integration

2.设置Python环境

# Create virtual environment
python -m venv venv

# Activate (Windows)
.\\venv\\Scripts\\activate

# Install dependencies
pip install -r requirements.txt

3.设置MCP服务器

# Run the automated setup script
.\\setup_mcp.bat

此脚本将:

  • 验证Node.js安装
  • 安装所有MCP服务器
  • 创建 .env 从模板
  • 验证MCP配置

4.配置环境变量

编辑 .env 并添加您的API密钥:

# Required for GitHub operations
GITHUB_TOKEN=your_github_personal_access_token

# Required for AI test generation
AZURE_OPENAI_KEY=your_azure_openai_key
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/

获取API密钥:

  • GitHub令牌:https://github.com/settings/tokens(范围: repo, read:org)
  • Azure OpenAI:https://portal.azure.com/

注: Self-healing使用编剧-测试-交易代理方法-无需网络搜索API!

5.安装Playwright浏览器

playwright install chromium firefox webkit

6.设置前端

cd frontend
npm install
cd ..

🎯 运行应用程序

启动后端

cd app/api
uvicorn main:app --reload --port 8001

后端运行时间:http://localhost:8001

启动前端

cd frontend
npm run dev

前端运行于:http://localhost:5173

📖 使用示例

1.记录用户流量

python -m app.recorder.run_playwright_recorder_v2 --url "https://example.com" --capture-dom --timeout 60

按 Ctrl+C 停止录制。输出保存到 recordings/<session-name>/

2.生成测试脚本

通过用户界面:

  1. 导航到http://localhost:5173
  2. 点击“开始录制”
  3. 在目标网站上执行操作
  4. 停止录制
  5. 生成自动化脚本或手动测试用例

3.自愈测试(运行时)

试运行期间自动自愈 -当XPath/定位器失败时:

from app.self_healing_with_mcp import SelfHealingExecutor
from pathlib import Path

# Initialize executor
executor = SelfHealingExecutor(framework_root=Path("./framework_repos/my-framework"))

# Run with automatic self-healing (up to 2 retries)
success, logs, healing_attempts = executor.run_trial_with_real_time_healing(
    script_content=my_test_script,
    test_url="https://app.com/page",
    max_retries=2,
    headed=False
)

if success:
    print(f"✅ Test passed after {len(healing_attempts)} healing attempts")

运作原理:

  1. 测试失败 XPath/定位器不正确→ 自动检测
  2. 剧作家MCP捕捉 故障点的实际页面状态
  3. 副驾驶分析 使用Microsoft Docs MCP+生成的流进行失败与实际元素的比较 app/generated_flow
  4. 生成更好的定位器 (基于角色、testid、标签弹性模式)
  5. 自动检索 带有已修复的脚本→ 测试通过✅
  6. 保存已修复的脚本 以备后用

成本:100%免费(包含在VS Code Copilot订阅中)

看: docs/RUNTIME_SELF_HEALING.md 获取完整指南

4.跟踪分析(诊断缺失动作)

将录制的动作与Playwright跟踪进行比较 识别缺失的步骤:

# 1. Record with trace enabled (default)
python -m app.recorder.run_playwright_recorder_v2 --url "https://example.com" --timeout 60

# 2. Analyze the recording
python -m app.recorder.trace_analyzer recordings/<session_name>

# 3. View trace in Playwright Inspector
playwright show-trace recordings/<session_name>/trace.zip

样本输出:

📊 Summary:
  - Trace events: 45
  - Recorded actions: 42
  - Missing events: 3
  - Coverage: 93.3%

❌ Missing Events:
  1. Type: click → Selector: button[data-testid="submit"]
  2. Type: fill → Selector: input#email
  3. Type: check → Selector: input[type="checkbox"]#terms

看: docs/TRACE_ANALYSIS.md 获取完整指南

5.矢量数据库操作

# Query the vector DB
python -m app.core.vector_db query "Create Supplier" --top-k 5

# List stored documents
python -m app.core.vector_db list --limit 50

🔧 MCP集成详细信息

微软文档MCP

目的: 官方剧作家文档和代码示例

from app.core.mcp_client import get_microsoft_docs_mcp

docs_mcp = get_microsoft_docs_mcp()

# Search documentation
results = docs_mcp.search_docs("Playwright locators")

# Get code samples
samples = docs_mcp.search_code_samples("getByRole", language="typescript")

自我修复与剧作家测试治疗师

目的: 使用剧作家测试治疗师方法进行系统调试(免费)

该系统使用剧作家测试治疗师代理方法:

  1. 系统地分析错误日志
  2. 使用Microsoft Docs获取官方最佳实践
  3. 从UI爬网数据生成弹性定位器
  4. 应用经过验证的调试模式

无需网络搜索API-所有免费工具!

GitHub MCP

目的: 存储库管理和代码搜索

from app.core.mcp_client import get_github_mcp

github = get_github_mcp()

# Search code
code_results = github.search_code("playwright locators")

# Clone repository
github.clone_repository(repo_url, target_path)

看 MCP_INTEGRATION.md 以获取完整的文档。

🏗️ 项目结构

mcp_integration/
├── app/
│   ├── api/                    # FastAPI backend
│   ├── core/                   # Core utilities
│   │   ├── llm_client.py      # LLM integration (self-healing)
│   │   ├── mcp_client.py      # MCP server clients
│   │   ├── vector_db.py       # Vector database
│   │   └── browser_utils.py   # Browser utilities
│   ├── generators/             # Test generation
│   │   └── agentic_script_agent.py  # AI script generation
│   ├── recorder/               # Recording module
│   │   ├── run_playwright_recorder_v2.py
│   │   └── mcp_integration.py # Recorder MCP integration
│   ├── ingestion/              # Data ingestion
│   └── services/               # Business logic
├── frontend/                   # React UI
├── docs/                       # Documentation
│   └── MCP_INTEGRATION.md     # MCP integration guide
├── recordings/                 # Recording output
├── framework_repos/           # Cloned test frameworks
├── .vscode/
│   └── mcp.json               # MCP server configuration
├── .env.template              # Environment variables template
├── setup_mcp.bat              # MCP setup script
└── requirements.txt           # Python dependencies

🧪 运行测试

# Run all tests
python -m pytest -q

# Run specific test file
python -m pytest app/tests/test_vector_db.py -v

# Run with coverage
python -m pytest --cov=app --cov-report=html

📊 关键工作流程

工作流程1:录制→ 手动测试用例

  1. 使用UI或CLI启动记录器
  2. 对目标应用程序执行操作
  3. 停止录制(元数据已保存)
  4. 点击“生成手动测试用例”
  5. 下载带有测试用例的Excel

工作流程2:录制→ 自动化脚本

  1. 开始记录
  2. 执行操作
  3. 停止录制
  4. 提供Git存储库URL
  5. 系统生成:
    • 页面对象
    • 定位器文件
    • 测试脚本
  6. 查看预览
  7. 推送到存储库

工作流程3:自愈失败测试(100%免费)

  1. 测试执行失败
  2. 系统捕获错误日志
  3. 查阅Microsoft文档以获取最佳实践
  4. 分析UI爬网数据
  5. 使用剧作家测试治疗师方法
  6. 生成固定定位器
  7. 更新定位器缓存

无需付费API!

🔍 故障排除

未找到MCP服务器

Error: npx: command not found

解决方案: 从以下位置安装Node.jshttps://nodejs.org/

GitHub速率限制

Error: GitHub API rate limit exceeded

解决方案: 添加 GITHUB_TOKEN 到 .env 文件

导入错误

ModuleNotFoundError: No module named 'app.core.mcp_client'

解决方案: 确保您从项目根目录运行,并且虚拟环境已激活

📚 文档

核心文件

API与开发

记录与测试

🎯 快速参考

运行时自愈命令

# Basic self-healing trial
from app.self_healing_with_mcp import SelfHealingExecutor

executor = SelfHealingExecutor(framework_root=Path("./my-framework"))
success, logs, attempts = executor.run_trial_with_real_time_healing(
    script_content=test_script,
    test_url="https://app.com",
    max_retries=2
)

# Check if healing was successful
if success and attempts:
    print(f"✅ Healed after {len(attempts)} attempts")

手动自愈(无自动重试)

from app.self_healing_executor import run_trial_with_self_healing

success, logs, attempts = run_trial_with_self_healing(
    script_content=script,
    framework_root=Path("./framework"),
    max_retries=3,
    headed=True  # Show browser for debugging
)

MCP客户端使用情况

from app.core.mcp_client import (
    get_microsoft_docs_mcp,
    get_github_mcp,
    get_filesystem_mcp
)

# Search official Playwright docs
docs_mcp = get_microsoft_docs_mcp()
results = docs_mcp.search_docs("getByRole selector")

# Find test patterns in GitHub
github_mcp = get_github_mcp()
patterns = github_mcp.search_code("microsoft/playwright", "page.getByRole")

# Safe file operations
fs_mcp = get_filesystem_mcp()
fs_mcp.safe_write_file(Path("test.spec.ts"), content, backup=True)

🤝 贡献

  1. 分叉存储库
  2. 创建特征分支(git checkout -b feature/amazing-feature)
  3. 提交更改(git commit -m 'Add amazing feature')
  4. 推送到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

📄 许可证

该项目是企业使用的专有软件。

🙏 致谢

  • 剧作家 -浏览器自动化框架
  • 克劳德·艾 -人工智能驱动的测试生成
  • 模型上下文协议 -MCP集成
  • 快速API -后端框架
  • 反应 -前端框架