混合自动化框架
一个完整的、模块化的、生产就绪的测试自动化框架,由以下部分构建:
- 语言:Java 11+
- 浏览器自动化:Java剧作家
- 测试框架:测试
- 报告:诱惑报告
- 生成工具:Maven
- 可选的:MCP服务器支持
🎯 框架功能
✅ 核心能力
- 页面对象模型(POM) -有组织、可维护的页面类
- 无硬编码定位器 -所有定位器
objectRep.properties - 线程安全 -支持并行测试执行
- MCP集成 -可选的模型上下文协议服务器支持
- 数据驱动测试 -通过Apache POI获取基于Excel的测试数据
- 综合录井 -SLF4J+Logback集成
- 诱惑报告 -漂亮、详细的测试报告和截图
📊 配置管理
config.properties-环境和执行设置objectRep.properties-将所有UI定位器放在一个地方TestConfiguration.java-具有环境变量覆盖的集中配置访问logback.xml-日志记录配置
🔄 测试生命周期
- BeforeSuite -初始化浏览器一次
- 事前方法 -每次测试创建新的上下文+页面
- AfterMethod -清理资源
- AfterSuite -关闭浏览器
📁 项目结构
hybrid-automation-framework/
├── pom.xml # Maven configuration
├── src/
│ ├── main/java/com/automation/
│ │ ├── config/
│ │ │ └── TestConfiguration.java # Config management
│ │ ├── constants/
│ │ │ └── AppConstant.java # Locator key constants
│ │ ├── core/
│ │ │ └── Operation.java # Playwright wrapper methods
│ │ ├── driver/
│ │ │ └── DriverFactory.java # Browser lifecycle management
│ │ ├── mcp/
│ │ │ └── McpClient.java # MCP server communication
│ │ ├── listeners/
│ │ │ ├── AllureTestListener.java # Allure integration
│ │ │ └── AllureLifecycleConfig.java
│ │ └── utils/
│ │ ├── ExcelUtils.java # Excel data reading
│ │ └── RetryAnalyzer.java # Test retry logic
│ └── test/java/com/automation/
│ ├── base/
│ │ └── BaseTest.java # Base test class
│ ├── pages/
│ │ └── LoginPage.java # Sample page object
│ └── tests/
│ └── LoginTest.java # Sample test class
│ └── test/resources/
│ ├── config.properties # Test configuration
│ ├── objectRep.properties # Locators repository
│ ├── testng.xml # TestNG suite configuration
│ ├── logback.xml # Logging configuration
│ └── data/
│ └── testdata.xlsx # Test data file (to be created)
└── README.md # This file🚀 入门指南
先决条件
- Java 11或更高版本
- Maven 3.6+
- Chrome/Firefox/WebKit浏览器
安装
- 克隆存储库
git clone https://github.com/Sandeep850-bit/hybrid-automation-framework.git
cd hybrid-automation-framework- 安装依赖项
mvn clean install- 安装Playwright浏览器
mvn exec:java -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="install"✍️ 运行测试
运行所有测试
mvn clean test运行特定的测试类
mvn clean test -Dtest=LoginTest运行特定的测试方法
mvn clean test -Dtest=LoginTest#testLoginWithValidCredentials按组运行测试
mvn clean test -Dgroups=smoke使用特定浏览器运行
mvn clean test -Dbrowser=firefox无头运行(默认)
mvn clean test -Dheadless=true以头部模式运行
mvn clean test -Dheadless=false📊 诱惑报告
试运行后生成Allure报告
mvn allure:report
mvn allure:serve这将:
- 根据测试结果生成报告
- 启动本地Allure服务器(通常http://localhost:4040)
- 在浏览器中自动打开报告
报告内容
- 测试执行总结
- 故障截图
- 日志和堆栈跟踪
- 环境信息
- 测试时间表
🔧 配置指南
config.properties
# Browser: chromium, firefox, webkit
browser=chromium
headless=true
# Base URL for tests
baseUrl=https://example.com
# Timeouts in milliseconds
implicit.wait=5000
explicit.wait=10000
page.load.timeout=30000
# MCP Configuration (optional)
mcp.enabled=false
mcp.endpoint=http://localhost:8080
mcp.timeout=5000
# Screenshots
screenshot.on.failure=true
screenshot.on.success=false
screenshot.dir=target/screenshots启用MCP模式
要使用MCP服务器而不是本地Playwright:
- 更新config.properties
mcp.enabled=true
mcp.endpoint=http://your-mcp-server:8080- 在Operation.java中,操作将自动委托给MCP服务器:
// Instead of executing locally, sends to MCP server
Operation.click(AppConstant.SIGN_IN_BTN, "Click Sign In");📝 添加新测试
1.创建定位器
添加到 src/test/resources/objectRep.properties:
myElement=xpath://button[@id='myBtn']
myInput=id:myInput2.添加常量
添加到 src/main/java/com/automation/constants/AppConstant.java:
public static final String MY_ELEMENT = "myElement";
public static final String MY_INPUT = "myInput";3.创建页面对象
创建 src/test/java/com/automation/pages/MyPage.java:
public class MyPage {
public static void clickMyElement() {
Operation.click(AppConstant.MY_ELEMENT, "Click my element");
}
public static void enterText(String text) {
Operation.type(AppConstant.MY_INPUT, text, "Enter text");
}
}4.写测试
创建 src/test/java/com/automation/tests/MyTest.java:
public class MyTest extends BaseTest {
@Test
public void myTest() {
MyPage.enterText("Hello");
MyPage.clickMyElement();
Assert.assertTrue(condition, "Verification message");
}
}📊 数据驱动测试
创建Excel文件
路径: src/test/resources/data/testdata.xlsx
工作表: LoginTestData
| 用户名 | 密码 | 预期结果 |
|---|---|---|
| user1 | pass1 | 成功 |
| 无效 | 错误 | 错误 |
在测试中使用
@Test(dataProvider = "loginData")
public void testLogin(String[] testData) {
String username = testData[0];
String password = testData[1];
// ... test logic
}
@DataProvider(name = "loginData")
public Object[][] getLoginData() {
return ExcelUtils.readExcelData(
"src/test/resources/data/testdata.xlsx",
"LoginTestData"
);
}🏷️ 诱惑注释
@Epic("Feature Name")
@Feature("Sub-feature")
@Story("User story description")
@Severity(SeverityLevel.CRITICAL)
@Description("Test description")
@Test
public void myTest() {
Allure.step("Step 1 - Do something");
// test code
Allure.step("Step 2 - Verify something");
// verification
}🔄 并行执行
在中配置线程数 testng.xml:
或在 pom.xml:
tests
4📸 屏幕截图和日志记录
自动截图
// On test failure - automatic
// On success - if enabled in config
// Manual screenshot
Operation.takeScreenshot("custom-screenshot-name");日志记录
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(MyTest.class);
logger.info("Info message");
logger.error("Error message", exception);日志将写入:
- 控制台 -实时输出
- 文件 -
target/logs/test-automation.log - 诱惑报告 -自动附加
🐛 测试重试
使用RetryAnalyzer
@Test(retryAnalyzer = RetryAnalyzer.class)
public void flakyTest() {
// Will retry up to 2 times on failure
}在中配置重试计数 src/main/java/com/automation/utils/RetryAnalyzer.java:
private static final int MAX_RETRY_COUNT = 2;🔐 线程安全
框架使用ThreadLocal进行线程安全的页面管理:
// Each thread gets its own page instance
DriverFactory.initContextAndPage(); // In @BeforeMethod
Page page = DriverFactory.getPage(); // Get current thread's page
DriverFactory.closeContextAndPage(); // In @AfterMethod安全地支持并行测试执行 parallel="tests" 在testng.xml中。
📦 依赖项
核心依赖关系包括:
playwright:1.40.1-浏览器自动化testng:7.8.1-测试框架allure-testng:2.21.0-报告poi:5.2.3-Excel支持slf4j:2.0.9+logback:1.4.11-日志记录rest-assured:5.4.0-API测试(MCP支持)gson:2.10.1-JSON处理
🆘 故障排除
浏览器未启动
mvn exec:java -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="install"编剧超时
增加超时时间 config.properties:
explicit.wait=20000
page.load.timeout=40000诱惑报告未生成
mvn clean test
mvn allure:reportMCP连接问题
验证MCP服务器是否正在运行:
curl http://localhost:8080/health📚 框架组件深度学习
操作.java
具有自动MCP超控功能的中央操作类:
Operation.click(key, message); // Click element
Operation.type(key, text, message); // Type text
Operation.select(key, value, message); // Select dropdown
Operation.waitForVisible(key, message); // Wait for element
Operation.isDisplayed(key); // Check visibility
Operation.takeScreenshot(filename); // Capture screenshotDriverFactory.java
浏览器生命周期管理:
DriverFactory.initBrowser(browserType, headless);
DriverFactory.initContextAndPage();
DriverFactory.getPage();
DriverFactory.closeContextAndPage();
DriverFactory.closeBrowser();测试配置.java
具有环境覆盖的配置管理:
config.getBrowser(); // Get browser type
config.isHeadless(); // Headless mode
config.getBaseUrl(); // Base URL
config.getValueFromObjectRep(key); // Get locator
config.isMcpEnabled(); // MCP status🚀 性能提示
- 使用并行执行 -
thread-count="4"在testng.xml中 - 尽量减少等待时间 -根据应用程序性能调整超时
- 重用浏览器上下文 -使用共享浏览器实例
- 禁用屏幕截图 -设置
screenshot.on.success=false如果不需要 - 使用无头模式 -执行速度提高约30%
📄 许可证
此框架按原样提供,用于自动化测试目的。
👥 贡献
要扩展此框架:
- 向添加新定位器
objectRep.properties - 将常量添加到
AppConstant.java - 创建新的页面类
- 按照模式添加测试方法
- 运行测试并生成Allure报告
📞 支持
对于问题或疑问:
- 检查现有测试示例
- 查看Allure报告中的故障
- 检查登录
target/logs/ - 验证
config.properties设置
______________________________________________________________________
测试愉快! 🎉
有关最新更新,请访问:https://github.com/Sandeep850-bit/hybrid-automation-framework
