# web_auto_test **Repository Path**: caoweinan/web_auto_test ## Basic Information - **Project Name**: web_auto_test - **Description**: Python+Playwright+关键字+Excel数据驱动,Web UI 自动化测试框架。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2024-12-17 - **Last Updated**: 2025-09-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Playwright Python 测试框架 基于 Playwright 的 Python Web UI 自动化测试框架,采用关键字驱动设计模式,提供了一套完整的页面操作和测试用例管理方案。 ## 特点 - 🎯 关键字驱动设计,降低维护成本 - 🚀 基于 Playwright 实现,性能优越 - 🔍 支持多种元素定位策略 - 📝 内置日志记录功能 - 🛡️ 完善的异常处理机制 - ⚙️ 灵活的配置文件管理 - 📊 Allure 测试报告支持 ## 快速开始 ### 1. 环境要求 - Python 3.7+ - Playwright - pytest ### 2. 安装 ```bash # 克隆项目 git clone https://gitee.com/your-username/playwright-python-framework.git cd playwright-python-framework # 安装依赖 pip install -r requirements.txt # 安装浏览器 playwright install ``` ### 3. 配置 修改 `config/config.yaml` 文件: ```yaml browser: type: "chromium" # 支持: chromium, firefox, webkit headless: false # 是否启用无头模式 slow_mo: 50 # 操作延迟,单位:毫秒 viewport: width: 1920 # 浏览器窗口宽度 height: 1080 # 浏览器窗口高度 base_url: "https://example.com" # 测试基础URL timeout: default: 30000 # 默认超时时间 navigation: 30000 # 页面加载超时时间 ``` ### 4. 运行测试 ```bash # 运行所有测试 pytest tests/ # 生成Allure报告 pytest tests/ --alluredir=./reports allure serve ./reports ``` ## 项目结构 ``` playwright-python-framework/ ├── pages/ # 页面对象 │ ├── __init__.py │ └── base_page.py # 页面基类 ├── tests/ # 测试用例 │ ├── __init__.py │ └── test_keyword_driven.py ├── utils/ # 工具类 │ ├── __init__.py │ └── logger.py # 日志工具 ├── config/ # 配置文件 │ ├── __init__.py │ └── config.yaml # 主配置文件 ├── reports/ # 测试报告 ├── logs/ # 日志文件 ├── requirements.txt # 依赖包 ├── conftest.py # Pytest配置 └── README.md # 项目说明 ``` ## 核心功能 ### 1. 元素定位 支持多种定位方式: - css - id - xpath - text - class_name - name - link_text - partial_link_text ### 2. 页面操作 BasePage 类提供以下核心方法: ```python # 导航到指定URL navigate_to(url) # 点击元素 click_element(locator_type, locator_value) # 输入文本 enter_text(locator_type, locator_value, text) ``` ### 3. 使用示例 ```python def test_login(page): base_page = BasePage() base_page.set_page(page) # 导航到登录页 base_page.navigate_to("https://example.com/login") # 输入用户名和密码 base_page.enter_text("id", "username", "test_user") base_page.enter_text("id", "password", "test_pass") # 点击登录按钮 base_page.click_element("id", "login-button") ``` ## 最佳实践 ### 1. 页面对象模式 - 为每个页面创建独立的 Page 类 - 继承 BasePage 基类 - 封装页面特定的操作方法 ### 2. 元素定位 - 优先使用稳定的定位方式(id、css) - 避免使用不稳定的定位方式(文本内容) - 合理使用 xpath 处理复杂场景 ### 3. 测试用例编写 - 遵循单一职责原则 - 保持用例独立性 - 合理使用夹具(fixtures) - 添加详细的测试描述 ## 常见问题 1. 元素无法定位 - 检查定位方式是否正确 - 确认元素是否在页面中可见 - 考虑添加等待机制 2. 测试执行失败 - 查看日志文件获取详细信息 - 检查网络连接 - 验证测试环境配置 ## 贡献指南 1. Fork 本仓库 2. 创建特性分支 3. 提交更改 4. 发起 Pull Request ## 许可证 MIT License