Selenium+Pytest+Allure自动化测试实战指南

发布时间:2026/11/2 21:41:24
Selenium+Pytest+Allure自动化测试实战指南 1. 项目概述SeleniumPytestAllure测试组合的价值在Web应用开发中UI自动化测试是保证产品质量的重要环节。这套技术组合中Selenium负责浏览器自动化操作Pytest作为测试执行框架Allure则提供可视化报告三者协同工作形成完整的测试解决方案。我曾在电商项目中用这套组合将回归测试时间从8小时压缩到25分钟关键页面测试覆盖率提升至98%。这种组合特别适合需要频繁回归测试的敏捷开发团队以及需要向非技术人员展示测试结果的场景。2. 环境搭建与工具配置2.1 基础环境安装Python环境建议使用3.7版本避免兼容性问题。使用virtualenv创建隔离环境python -m venv test_env source test_env/bin/activate # Linux/Mac test_env\Scripts\activate # Windows核心组件安装命令pip install selenium pytest allure-pytest注意Allure需要额外安装命令行工具从GitHub Releases下载对应版本解压后将bin目录加入PATH环境变量2.2 浏览器驱动配置以Chrome为例需下载与浏览器版本匹配的chromedriver# 查看Chrome版本 google-chrome --version # 下载对应驱动 wget https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip unzip chromedriver_linux64.zip mv chromedriver /usr/local/bin/常见问题处理版本不匹配通过Chrome菜单→帮助→关于Google Chrome查看精确版本号权限问题执行chmod x chromedriver路径问题建议将驱动放在系统PATH包含的目录3. 测试框架设计与实现3.1 项目目录结构推荐采用分层设计project/ ├── pages/ # 页面对象类 │ ├── login_page.py │ └── home_page.py ├── tests/ # 测试用例 │ ├── test_login.py │ └── test_search.py ├── conftest.py # pytest fixtures ├── pytest.ini # 配置文件 └── reports/ # 测试报告3.2 页面对象模式实现示例登录页面封装# pages/login_page.py from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class LoginPage: def __init__(self, driver): self.driver driver self.username_locator (By.ID, username) self.password_locator (By.ID, password) self.submit_locator (By.CSS_SELECTOR, button[typesubmit]) def enter_credentials(self, username, password): WebDriverWait(self.driver, 10).until( EC.presence_of_element_located(self.username_locator) ).send_keys(username) self.driver.find_element(*self.password_locator).send_keys(password) def submit(self): self.driver.find_element(*self.submit_locator).click()3.3 测试用例编写结合Pytest参数化实现数据驱动测试# tests/test_login.py import pytest from pages.login_page import LoginPage pytest.mark.parametrize(username,password,expected, [ (admin, correct_pwd, True), (guest, wrong_pwd, False), (, , False) ]) def test_login(driver, username, password, expected): 测试不同账号密码组合的登录结果 login_page LoginPage(driver) login_page.enter_credentials(username, password) login_page.submit() if expected: assert dashboard in driver.current_url else: assert error in driver.page_source4. Allure报告高级应用4.1 测试步骤注解使用装饰器增强报告可读性import allure allure.feature(用户认证) allure.story(登录功能) def test_login_with_allure(driver): with allure.step(打开登录页面): driver.get(https://example.com/login) with allure.step(输入凭证): LoginPage(driver).enter_credentials(admin, admin123) with allure.step(提交表单): LoginPage(driver).submit() allure.attach(driver.get_screenshot_as_png(), name登录后页面, attachment_typeallure.attachment_type.PNG)4.2 报告生成与查看执行测试并生成报告pytest tests/ --alluredir./results allure serve ./results # 本地查看 allure generate ./results -o ./report --clean # 生成静态报告关键参数说明--alluredir指定结果文件存储目录--clean清除历史结果-o指定报告输出目录5. 常见问题解决方案5.1 元素定位问题处理典型定位失败场景处理方案问题现象解决方案代码示例元素未加载完成使用显式等待WebDriverWait(driver,10).until(EC.presence_of_element_located(locator))元素在iframe中切换iframe上下文driver.switch_to.frame(frame_name)动态ID元素使用XPath或CSS选择器//div[contains(class,login-form)]5.2 测试稳定性提升技巧智能等待策略def wait_for_ajax(driver): WebDriverWait(driver, 10).until( lambda d: d.execute_script(return jQuery.active 0) )失败自动重试 在pytest.ini中添加[pytest] reruns 2 reruns_delay 1跨浏览器支持pytest.fixture(params[chrome, firefox]) def driver(request): if request.param chrome: options webdriver.ChromeOptions() return webdriver.Chrome(optionsoptions) else: return webdriver.Firefox()6. 高级应用场景6.1 持续集成集成Jenkins Pipeline示例pipeline { agent any stages { stage(Test) { steps { sh pytest tests/ --alluredir./results } } stage(Report) { steps { allure includeProperties: false, jdk: , results: [[path: results]] } } } }6.2 移动端响应式测试通过设置浏览器窗口尺寸测试响应式布局allure.story(响应式布局测试) pytest.mark.parametrize(width,height, [(320,568), (768,1024), (1920,1080)]) def test_responsive(driver, width, height): driver.set_window_size(width, height) # 执行布局测试逻辑 assert element.is_displayed()这套技术组合经过多个百万级用户项目的验证在保持测试代码可维护性的同时能有效提升测试效率。关键在于合理设计页面对象模型配合Allure的丰富报告功能使自动化测试真正成为质量保障的利器。

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询