# playwright_ui_framework **Repository Path**: rainaReal/playwright_ui_framework ## Basic Information - **Project Name**: playwright_ui_framework - **Description**: UI自动化框架 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-06-18 - **Last Updated**: 2025-07-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UI 自动化测试框架 基于 Python + Playwright 的 UI 自动化测试框架,采用页面对象模式(POM)设计,支持多浏览器、多环境、并行执行等功能。 ## 🚀 特性 - **多浏览器支持**: Chromium、Firefox、WebKit - **页面对象模式**: 提高代码复用性和维护性 - **数据驱动测试**: 支持 JSON、CSV 等格式的测试数据 - **并行执行**: 支持多进程并行运行测试 - **丰富的报告**: HTML报告、Allure报告 - **自动截图**: 测试失败时自动截图 - **日志记录**: 详细的测试执行日志 - **环境配置**: 支持多环境配置切换 ## 📁 项目结构 ``` ui_test/ ├── config/ # 配置文件 │ └── config.py # 主配置文件 ├── pages/ # 页面对象 │ ├── login_page.py # 登录页面 │ └── home_page.py # 首页 ├── tests/ # 测试用例 │ └── test_login.py # 登录测试 ├── utils/ # 工具类 │ ├── base_page.py # 基础页面类 │ ├── browser_manager.py # 浏览器管理器 │ ├── logger.py # 日志工具 │ └── test_data_manager.py # 测试数据管理器 ├── test_data/ # 测试数据 ├── screenshots/ # 截图文件 ├── reports/ # 测试报告 ├── conftest.py # pytest 配置 ├── pytest.ini # pytest 配置文件 ├── requirements.txt # 依赖包 ├── run_tests.py # 测试运行脚本 └── README.md # 项目说明 ``` ## 🛠️ 安装和设置 ### 1. 克隆项目 ```bash git clone cd ui_test ``` ### 2. 安装依赖 ```bash pip install -r requirements.txt ``` ### 3. 安装浏览器 ```bash python -m playwright install ``` 或者使用项目脚本: ```bash python run_tests.py --setup ``` ### 4. 配置环境变量(可选) 复制 `.env.example` 为 `.env` 并修改配置: ```bash cp .env.example .env ``` ## 🎯 快速开始 ### 运行所有测试 ```bash python run_tests.py ``` ### 运行特定测试 ```bash # 运行登录测试 python run_tests.py --path tests/test_login.py # 运行冒烟测试 python run_tests.py --markers smoke # 运行回归测试 python run_tests.py --markers regression ``` ### 指定浏览器运行 ```bash # Chrome 浏览器 python run_tests.py --browser chromium # Firefox 浏览器 python run_tests.py --browser firefox # Safari 浏览器 python run_tests.py --browser webkit ``` ### 有头模式运行(显示浏览器界面) ```bash python run_tests.py --headed ``` ### 并行执行 ```bash # 使用 4 个进程并行执行 python run_tests.py --workers 4 ``` ## 📊 测试报告 ### HTML 报告 测试完成后会自动生成 HTML 报告: ``` reports/report_YYYYMMDD_HHMMSS.html ``` ### Allure 报告 生成 Allure 报告: ```bash python run_tests.py --allure ``` ## 📝 编写测试用例 ### 1. 创建页面对象 ```python from utils.base_page import BasePage class MyPage(BasePage): def __init__(self, page): super().__init__(page) self.url = "/my-page" # 元素定位器 self.button = "button[data-testid='my-button']" async def click_button(self): await self.click(self.button) ``` ### 2. 编写测试用例 ```python import pytest from pages.my_page import MyPage class TestMyPage: @pytest.mark.smoke async def test_button_click(self, page): my_page = MyPage(page) await my_page.navigate() await my_page.click_button() # 断言 assert await my_page.is_visible(".success-message") ``` ## 🎨 页面对象模式 框架采用页面对象模式,每个页面对应一个页面类: ```python class LoginPage(BasePage): def __init__(self, page): super().__init__(page) self.url = "/login" # 元素定位器 self.username_input = "input[name='username']" self.password_input = "input[name='password']" self.login_button = "button[type='submit']" async def login(self, username, password): await self.fill(self.username_input, username) await self.fill(self.password_input, password) await self.click(self.login_button) ``` ## 🔧 配置说明 ### 环境变量配置 ```python # config/config.py BASE_URL = "https://www.example.com" TIMEOUT = 30000 HEADLESS = True BROWSER = "chromium" ``` ### 测试数据管理 ```python # 获取测试数据 user_data = test_data.get_user_data('valid') scenario_data = test_data.get_test_data_by_scenario('login_success') ``` ## 📋 测试标记 框架支持以下测试标记: - `@pytest.mark.smoke`: 冒烟测试 - `@pytest.mark.regression`: 回归测试 - `@pytest.mark.slow`: 慢速测试 - `@pytest.mark.login`: 登录相关测试 - `@pytest.mark.ui`: UI界面测试 ## 🐛 调试技巧 ### 1. 查看浏览器执行过程 ```bash python run_tests.py --headed --verbose ``` ### 2. 单步执行测试 在测试代码中添加断点: ```python import pdb; pdb.set_trace() ``` ### 3. 保存页面截图 ```python await page.screenshot(path="debug.png") ``` ## 📚 常用命令 ```bash # 安装环境 python run_tests.py --setup # 运行所有测试 python run_tests.py # 运行指定标记的测试 python run_tests.py -m smoke # 并行运行测试 python run_tests.py -w 4 # 生成 Allure 报告 python run_tests.py --allure # 有头模式运行 python run_tests.py --headed # 指定浏览器 python run_tests.py -b firefox ``` ## 🤝 贡献指南 1. Fork 项目 2. 创建特性分支 (`git checkout -b feature/amazing-feature`) 3. 提交更改 (`git commit -m 'Add some amazing feature'`) 4. 推送到分支 (`git push origin feature/amazing-feature`) 5. 开启 Pull Request ## 📄 许可证 本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。 ## 🆘 问题和支持 如果遇到问题,请: 1. 查看项目文档 2. 搜索已知问题 3. 创建新的 Issue --- **Happy Testing! 🎉**