当前位置: 首页 > news >正文

Selenium + Python + Pytest + Yaml + POM

以下是基于 Selenium + Python + Pytest + Yaml + POM 搭建 WebUI 自动化测试框架,并通过 Jenkins 实现持续集成、自动部署和定时任务的具体操作步骤:

一、搭建 WebUI 自动化测试框架

1. 项目结构

创建以下项目目录结构:

WebUITestFramework/
├── config/                # 配置文件目录
├── page/                  # 页面对象目录
├── page_element/          # 页面元素定位文件目录
├── scripts/               # 测试脚本目录
├── utils/                 # 工具类目录
├── conftest.py            # Pytest 配置文件
├── pytest.ini             # Pytest 配置文件
├── test_case.py           # 测试用例文件
2. 配置文件管理

config 目录下创建 conf.pyconfig.ini 文件。

conf.py

import os
from selenium.webdriver.common.by import By
from utils.times import dt_strftimeclass ConfigManager(object):BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))ELEMENT_PATH = os.path.join(BASE_DIR, 'page_element')REPORT_FILE = os.path.join(BASE_DIR, 'report.html')LOCATE_MODE = {'css': By.CSS_SELECTOR,'xpath': By.XPATH,'name': By.NAME,'id': By.ID,'class': By.CLASS_NAME}EMAIL_INFO = {'username': 'your_email@example.com','password': 'your_password','smtp_host': 'smtp.example.com','smtp_port': 465}ADDRESSEE = ['recipient@example.com']@propertydef log_file(self):log_dir = os.path.join(self.BASE_DIR, 'logs')if not os.path.exists(log_dir):os.makedirs(log_dir)return os.path.join(log_dir, '{}.log'.format(dt_strftime()))@propertydef ini_file(self):ini_file = os.path.join(self.BASE_DIR, 'config', 'config.ini')if not os.path.exists(ini_file):raise FileNotFoundError("配置文件%s不存在!" % ini_file)return ini_filecm = ConfigManager()

config.ini

ini复制

[HOST]
HOST = https://www.example.com
3. 页面对象管理

page_element 目录下创建 search.yaml 文件:

yaml复制

搜索框: id==kw
搜索按钮: id==su
4. 封装 Selenium 基类

page 目录下创建 webpage.py 文件:

Python复制

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from config.conf import cm
from utils.logger import logclass WebPage(object):def __init__(self, driver):self.driver = driverself.timeout = 20self.wait = WebDriverWait(self.driver, self.timeout)def find_element(self, locator):return self.wait.until(EC.presence_of_element_located(locator))def input_text(self, locator, txt):ele = self.find_element(locator)ele.clear()ele.send_keys(txt)log.info(f"输入文本:{txt}")def click(self, locator):self.find_element(locator).click()log.info(f"点击元素:{locator}")
5. 编写测试用例

在项目根目录下创建 test_case.py 文件:

Python复制

import pytest
from selenium import webdriver
from page.webpage import WebPage
from page_object.searchpage import SearchPage@pytest.fixture(scope='session')
def driver():driver = webdriver.Chrome()yield driverdriver.quit()def test_search(driver):page = SearchPage(driver)page.search("Moonshot AI")assert "Moonshot AI" in driver.title

二、集成 Jenkins 实现持续集成

1. 安装 Jenkins
  • 下载并安装 Jenkins。

  • 启动 Jenkins 并完成初始配置。

2. 创建 Jenkins 任务
  • 在 Jenkins 中创建一个新的自由风格项目。

  • 配置源码管理(如 Git),指定项目代码仓库地址。

  • 配置构建触发器(如定时任务或代码提交触发)。

3. 配置构建环境
  • 在 Jenkins 任务的构建步骤中,添加执行 Shell 脚本:

bash复制

pip install -r requirements.txt
pytest --html=report.html
4. 配置邮件通知
  • 安装 Jenkins 的邮件扩展插件。

  • 在项目配置中设置邮件通知,指定收件人和邮件内容。

5. 设置定时任务
  • 在 Jenkins 任务的构建触发器中,选择“定时构建”。

  • 配置定时任务的 Cron 表达式,例如:

    0 0 * * * # 每天凌晨 0 点触发

三、适配企业需求

  1. 多环境适配

    • config.ini 中添加不同环境的配置。

    • 通过 Jenkins 参数化构建,动态选择测试环境。

  2. 测试报告

    • 使用 pytest-html 插件生成测试报告。

    • 将报告上传到 Jenkins 的工作空间,方便查看。

  3. 自动部署

    • 在 Jenkins 中配置部署脚本,将测试结果或相关文件部署到指定服务器。

通过以上步骤,你可以完成一个完整的 WebUI 自动化测试框架搭建,并通过 Jenkins 实现持续集成、自动部署和定时任务,满足企业级测试需求。

http://www.lryc.cn/news/614124.html

相关文章:

  • ISL9V3040D3ST-F085C一款安森美 ON生产的汽车点火IGBT模块,绝缘栅双极型晶体管ISL9V3040D3ST汽车点火电路中的线圈驱动器
  • 【量子计算】量子计算驱动AI跃迁:2025年算法革命的曙光
  • 行业速览:中国新能源汽车市场格局与关键趋势
  • 时序数据库-涛思数据库
  • 实现一个进程池(精讲)
  • ​​Vue3 + Element Plus 构建的现代化即时通讯在线客服系统​
  • STM32学习笔记5-TIM定时器-1
  • 线程池基础知识
  • wstool和catkin_tools工具介绍
  • 智慧社区(十)——声明式日志记录与小区地图功能实现
  • Python实现点云PCA配准——粗配准
  • Ubuntu安装 L20显卡驱动
  • Linux网络--2、Socket编程
  • 中国电信清华:大模型驱动的具身智能发展与挑战综述
  • 动漫软件集合分享
  • Pytest项目_day08(setup、teardown前置后置操作)
  • 144.二叉树的前序遍历
  • 鲸签云解决互联网行业合同管理难题​
  • 【Rust】多级目录模块化集成测试——以Cucumber为例
  • 线程组和线程池的基本用法
  • 【Spring Boot 快速入门】八、登录认证
  • duxapp 2025-05-29 更新 兼容鸿蒙C-API方案,现在鸿蒙端可以用于生产
  • React SSR 水合问题
  • 《告别Bug!GDB/CGDB调试实战指南》
  • TF 上架全流程实战,从构建到 TestFlight 分发
  • UniApp 跳转外部链接实现
  • Elasticsearch LTR(Learning To Rank)从训练到检索与重排
  • Elasticsearch:在向量搜索中使用 Direct IO
  • 力扣-438.找到字符串中所有字母异位词
  • ctfshow_萌新web9-web13-----rce