获课:http://www.bcwit.top/3299/
获取ZY↑↑方打开链接↑↑
一、技术栈深度解析
Selenium3核心定位
浏览器操控引擎:通过WebDriver协议实现Chrome/Firefox等主流浏览器的自动化控制,支持元素定位(XPath/CSS Selector)、表单提交、文件上传等原生操作。
跨平台兼容性:一套代码可在Windows/Linux/macOS系统无缝运行,适配不同浏览器的驱动(如ChromeDriver需与浏览器版本严格匹配)。
Pytest框架优势
测试组织:通过conftest.py定义全局Fixture(如浏览器初始化),利用@pytest.mark.parametrize实现数据驱动测试。
插件生态:集成pytest-html生成基础报告,与Allure深度整合输出可视化结果。
Allure报告价值
多维度呈现:按功能模块(Feature)和测试场景(Story)分类展示用例,失败用例自动关联截图与日志。
持续集成:与Jenkins无缝对接,实现构建后自动触发测试并生成趋势图表。
二、项目实战全流程
1. 需求分析与框架设计
功能筛选:优先选择高频核心功能(如登录、下单),避开动态广告等非确定性场景。
PO模式分层:
BasePage:封装通用方法(如wait_for_element)。
LoginPage:继承BasePage,定义用户名/密码输入框及提交按钮的定位器。
TestCases:调用页面对象执行业务逻辑,保持脚本与UI解耦。
2. 环境搭建与配置
依赖安装:bash
pip install selenium pytest allure-pytest cryptography python-dotenv
驱动配置:将ChromeDriver加入PATH或指定路径:python
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
3. 测试脚本开发
参数化登录测试:python
@pytest.mark.parametrize('username, password, expected', [
('valid_user', 'correct_pwd', True),
('invalid_user', 'wrong_pwd', False)
])
def test_login(login_page, username, password, expected):
login_page.enter_credentials(username, password)
assert login_page.is_logged_in() == expected
4. 无密化处理实践
敏感信息加密:python
from cryptography.fernet import Fernet
# 生成密钥并加密数据库密码
key = Fernet.generate_key()
cipher = Fernet(key)
encrypted_pwd = cipher.encrypt(b'db_password123')
环境变量注入:python
# .env文件
DB_HOST=localhost
DB_PORT=3306
python
# 测试脚本中读取
import os
from dotenv import load_dotenv
load_dotenv()
db_config = {
'host': os.getenv('DB_HOST'),
'port': os.getenv('DB_PORT')
}
5. 测试执行与报告生成
命令行触发:bash
pytest --alluredir=./allure-results --env=prod
报告生成:bash
allure serve ./allure-results # 实时预览
allure generate ./allure-results -o ./allure-report --clean # 生成静态HTML
三、高级优化策略
显式等待机制
python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "dynamic-content"))
)
并行测试加速
安装插件:pip install pytest-xdist
执行命令:pytest -n 4 --alluredir=./results(4进程并行)
失败用例自动重试
python
@pytest.mark.flaky(reruns=2, reruns_delay=5)
def test_flaky_operation():
# 不稳定操作(如网络请求)
Allure报告增强
步骤描述:python
@allure.step("验证购物车商品数量")
def verify_cart_items(expected_count):
actual_count = get_cart_item_count()
assert actual_count == expected_count
附件关联:python
allure.attach.file('./screenshots/error.png', name='failure_screenshot', attachment_type=allure.attachment_type.PNG)
四、持续集成流水线(以Jenkins为例)
环境准备:
安装Allure插件与命令行工具。
配置全局工具:JDK、Python、Allure。
Pipeline脚本:groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo.git'
}
}
stage('Test') {
steps {
sh 'pip install -r requirements.txt'
sh 'pytest --alluredir=allure-results'
}
}
stage('Report') {
steps {
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
}
}
}
}
五、典型项目收益
效率提升:回归测试执行时间从人工2小时缩短至自动化15分钟。
质量保障:关键用户路径覆盖率从60%提升至95%。
成本降低:减少30%手动测试人力投入,缺陷漏检率下降40%。
获取ZY↑↑方打开链接↑↑
一、技术栈深度解析
Selenium3核心定位
浏览器操控引擎:通过WebDriver协议实现Chrome/Firefox等主流浏览器的自动化控制,支持元素定位(XPath/CSS Selector)、表单提交、文件上传等原生操作。
跨平台兼容性:一套代码可在Windows/Linux/macOS系统无缝运行,适配不同浏览器的驱动(如ChromeDriver需与浏览器版本严格匹配)。
Pytest框架优势
测试组织:通过conftest.py定义全局Fixture(如浏览器初始化),利用@pytest.mark.parametrize实现数据驱动测试。
插件生态:集成pytest-html生成基础报告,与Allure深度整合输出可视化结果。
Allure报告价值
多维度呈现:按功能模块(Feature)和测试场景(Story)分类展示用例,失败用例自动关联截图与日志。
持续集成:与Jenkins无缝对接,实现构建后自动触发测试并生成趋势图表。
二、项目实战全流程
1. 需求分析与框架设计
功能筛选:优先选择高频核心功能(如登录、下单),避开动态广告等非确定性场景。
PO模式分层:
BasePage:封装通用方法(如wait_for_element)。
LoginPage:继承BasePage,定义用户名/密码输入框及提交按钮的定位器。
TestCases:调用页面对象执行业务逻辑,保持脚本与UI解耦。
2. 环境搭建与配置
依赖安装:bash
pip install selenium pytest allure-pytest cryptography python-dotenv
驱动配置:将ChromeDriver加入PATH或指定路径:python
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
3. 测试脚本开发
参数化登录测试:python
@pytest.mark.parametrize('username, password, expected', [
('valid_user', 'correct_pwd', True),
('invalid_user', 'wrong_pwd', False)
])
def test_login(login_page, username, password, expected):
login_page.enter_credentials(username, password)
assert login_page.is_logged_in() == expected
4. 无密化处理实践
敏感信息加密:python
from cryptography.fernet import Fernet
# 生成密钥并加密数据库密码
key = Fernet.generate_key()
cipher = Fernet(key)
encrypted_pwd = cipher.encrypt(b'db_password123')
环境变量注入:python
# .env文件
DB_HOST=localhost
DB_PORT=3306
python
# 测试脚本中读取
import os
from dotenv import load_dotenv
load_dotenv()
db_config = {
'host': os.getenv('DB_HOST'),
'port': os.getenv('DB_PORT')
}
5. 测试执行与报告生成
命令行触发:bash
pytest --alluredir=./allure-results --env=prod
报告生成:bash
allure serve ./allure-results # 实时预览
allure generate ./allure-results -o ./allure-report --clean # 生成静态HTML
三、高级优化策略
显式等待机制
python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "dynamic-content"))
)
并行测试加速
安装插件:pip install pytest-xdist
执行命令:pytest -n 4 --alluredir=./results(4进程并行)
失败用例自动重试
python
@pytest.mark.flaky(reruns=2, reruns_delay=5)
def test_flaky_operation():
# 不稳定操作(如网络请求)
Allure报告增强
步骤描述:python
@allure.step("验证购物车商品数量")
def verify_cart_items(expected_count):
actual_count = get_cart_item_count()
assert actual_count == expected_count
附件关联:python
allure.attach.file('./screenshots/error.png', name='failure_screenshot', attachment_type=allure.attachment_type.PNG)
四、持续集成流水线(以Jenkins为例)
环境准备:
安装Allure插件与命令行工具。
配置全局工具:JDK、Python、Allure。
Pipeline脚本:groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git 'https://github.com/your-repo.git'
}
}
stage('Test') {
steps {
sh 'pip install -r requirements.txt'
sh 'pytest --alluredir=allure-results'
}
}
stage('Report') {
steps {
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
}
}
}
}
五、典型项目收益
效率提升:回归测试执行时间从人工2小时缩短至自动化15分钟。
质量保障:关键用户路径覆盖率从60%提升至95%。
成本降低:减少30%手动测试人力投入,缺陷漏检率下降40%。