ui自动化python pytest selenium
时间: 2025-07-15 09:31:55 浏览: 9
<think>我们正在讨论使用Python的Pytest框架结合Selenium进行UI自动化测试。根据用户的问题,我们需要提供一个教程性质的回答,介绍如何搭建和使用这个测试框架。
参考引用:
[^1]提到了使用pytest+selenium编写网页UI自动化脚本和用例。
[^2]介绍了Python Selenium搭建UI自动化测试框架的环境准备。
[^3]提到了UI自动化测试采用PO模式、Pytest和Allure整合。
[^4]详细解读了Pytest+selenium UI自动化测试实战实例,包括环境配置和所需安装的库。
[^5]展示了构建脚本的例子,包括安装依赖、执行测试和生成报告。
因此,我们的回答应该包括以下步骤:
1. 环境准备:安装Python、配置开发环境、安装必要的库。
2. 项目结构规划:包括测试脚本、页面对象(PO模式)、配置文件、测试数据、测试报告等目录。
3. 编写基础测试:使用Selenium编写一个简单的测试用例,并用Pytest运行。
4. 引入PO模式:将页面元素和操作封装成页面对象,提高代码可维护性。
5. 使用Pytest特性:如fixture、参数化等。
6. 生成测试报告:使用pytest-html生成报告,也可以整合Allure生成更美观的报告。
7. 持续集成:如何将自动化测试集成到CI/CD流程中(可选)。
由于用户要求生成相关问题,我们将在最后提出几个相关问题。
现在,我们开始编写回答。</think>### Python UI自动化测试:Pytest + Selenium 完整教程
#### 一、环境准备
1. **安装Python**
- 推荐Python 3.8+版本,官网下载安装包
- 验证安装:终端执行 `python --version`
2. **安装依赖库**
在终端执行以下命令:
```bash
pip install selenium pytest pytest-html allure-pytest openpyxl webdriver-manager
```
- `selenium`:浏览器自动化核心库
- `pytest`:测试框架
- `pytest-html`:生成HTML测试报告
- `webdriver-manager`:自动管理浏览器驱动[^4]
3. **配置浏览器驱动**
```python
# 示例:自动下载Chrome驱动
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
driver = webdriver.Chrome(ChromeDriverManager().install())
```
#### 二、项目结构规划(PO模式)
```bash
project/
├── pages/ # 页面对象封装
│ ├── login_page.py
│ └── home_page.py
├── tests/ # 测试用例
│ └── test_login.py
├── conftest.py # Pytest全局配置
├── pytest.ini # Pytest配置文件
└── reports/ # 测试报告目录
```
#### 三、编写测试用例(PO模式示例)
1. **页面对象封装** (`pages/login_page.py`)
```python
from selenium.webdriver.common.by import By
class LoginPage:
def __init__(self, driver):
self.driver = driver
self.username = (By.ID, "username")
self.password = (By.ID, "password")
self.submit_btn = (By.ID, "login-btn")
def enter_credentials(self, user, pwd):
self.driver.find_element(*self.username).send_keys(user)
self.driver.find_element(*self.password).send_keys(pwd)
def click_login(self):
self.driver.find_element(*self.submit_btn).click()
```
2. **测试用例** (`tests/test_login.py`)
```python
import pytest
from pages.login_page import LoginPage
@pytest.mark.usefixtures("setup") # 使用预置的driver
class TestLogin:
def test_valid_login(self, setup):
driver = setup
login_page = LoginPage(driver)
login_page.enter_credentials("admin", "admin123")
login_page.click_login()
assert "Dashboard" in driver.title
```
3. **全局配置** (`conftest.py`)
```python
import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
@pytest.fixture(scope="class")
def setup(request):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://example.com/login")
request.cls.driver = driver
yield
driver.quit()
```
#### 四、执行测试与报告生成
1. **运行测试**
终端执行:
```bash
pytest tests/ --html=reports/report.html
```
2. **Allure高级报告**
```bash
pytest tests/ --alluredir=reports/allure
allure serve reports/allure
```
#### 五、最佳实践
1. **元素等待策略**
在 `conftest.py` 添加隐式等待:
```python
driver.implicitly_wait(10) # 全局等待10秒
```
2. **数据驱动测试**
使用 `pytest.mark.parametrize`:
```python
@pytest.mark.parametrize("user,pwd", [("admin", "pass1"), ("test", "pass2")])
def test_login(user, pwd):
# 测试逻辑
```
3. **失败自动截图**
在 `conftest.py` 添加:
```python
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == "call" and report.failed:
driver = item.cls.driver
driver.save_screenshot(f"reports/{item.name}_fail.png")
```
#### 六、持续集成(Jenkins示例)
在Jenkins构建脚本中添加:
```bash
#!/bin/bash
pip install -r requirements.txt
pytest tests/ --alluredir=reports
allure generate reports -o allure-report --clean
```
阅读全文
相关推荐


















