Bybit Automation Testing Framework
专业的 Bybit 平台自动化测试框架 - 支持 Web、移动端和 API 测试
📋 目录
✨ 框架特性
🎯 核心功能
- 多平台支持: Web UI、移动端 App、API 接口全覆盖测试
- 多浏览器支持: Chromium、Firefox、WebKit,支持多版本配置
- 页面对象模型 (POM): 专业的页面对象设计模式,提高代码可维护性
- 智能重试机制: 自动重试失败的测试用例,提高测试稳定性
- 详细日志记录: 结构化的日志系统,支持文件和控制台输出
- 专业测试报告: 使用 pytest-testreport 生成美观的 HTML 报告
- 邮件通知: 自动发送测试报告到指定邮箱,包含可视化看板
🏗️ 架构设计
- 模块化设计: 清晰的模块划分,易于扩展和维护
- 配置管理: 基于 Pydantic 的配置管理,支持环境变量
- 工具类封装: 丰富的工具函数,提高开发效率
- Fixture 管理: 完善的 pytest fixtures,支持测试隔离
📁 项目结构
playwright_test/
├── api/ # API 测试模块
│ ├── __init__.py
│ └── client.py # Bybit API 客户端
├── config/ # 配置文件
│ ├── __init__.py
│ └── settings.py # 配置管理
├── pages/ # 页面对象模型 (POM)
│ ├── __init__.py
│ ├── base_page.py # 基础页面类
│ ├── bybit_home_page.py # 首页页面对象
│ └── bybit_login_page.py # 登录页面对象
├── tests/ # 测试用例
│ ├── web/ # Web UI 测试
│ │ ├── test_homepage.py
│ │ └── test_login.py
│ ├── mobile/ # 移动端测试
│ │ └── test_mobile_app.py
│ └── api/ # API 测试
│ ├── test_public_api.py
│ └── test_private_api.py
├── utils/ # 工具类
│ ├── __init__.py
│ ├── logger.py # 日志工具
│ ├── email_sender.py # 邮件发送工具
│ └── helpers.py # 辅助函数
├── reports/ # 测试报告目录
├── screenshots/ # 截图目录
├── videos/ # 视频录制目录
├── logs/ # 日志目录
├── conftest.py # Pytest 配置和 Fixtures
├── pytest.ini # Pytest 配置文件
├── playwright.config.js # Playwright 配置文件
├── pytest_report_hook.py # 报告生成 Hook
├── requirements.txt # Python 依赖
├── package.json # Node.js 依赖
└── README.md # 项目文档
🚀 快速开始
环境要求
- Python 3.8+
- Node.js 16+ (用于 Playwright)
- pip 和 npm
安装步骤
- 克隆项目
cd /Users/sh01728ml/code/playwright_test
- 安装 Python 依赖
pip install -r requirements.txt
- 安装 Playwright 浏览器
# 安装所有浏览器
playwright install
# 或安装特定浏览器
playwright install chromium
playwright install firefox
playwright install webkit
- 配置环境变量
复制 .env.example 并创建 .env 文件:
cp .env.example .env
编辑 .env 文件,配置必要的参数:
# 应用 URL
BASE_URL=https://www.bybit.com
API_BASE_URL=https://api.bybit.com
# 测试配置
HEADLESS=true
BROWSER=chromium
TIMEOUT=30000
# 用户凭证(可选)
TEST_USER_EMAIL=your_email@example.com
TEST_USER_PASSWORD=your_password
# API 凭证(可选)
API_KEY=your_api_key
API_SECRET=your_api_secret
# 邮件配置(可选)
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASSWORD=your_app_password
EMAIL_TO=recipient@example.com
⚙️ 配置说明
环境变量配置
框架使用 .env 文件管理配置,主要配置项包括:
| 配置项 | 说明 | 默认值 |
|---|---|---|
BASE_URL |
测试目标网站 URL | https://www.bybit.com |
API_BASE_URL |
API 基础 URL | https://api.bybit.com |
HEADLESS |
是否无头模式运行 | true |
BROWSER |
默认浏览器 | chromium |
TIMEOUT |
默认超时时间(毫秒) | 30000 |
RETRIES |
失败重试次数 | 2 |
SEND_EMAIL |
是否发送邮件报告 | true |
多版本浏览器配置
框架支持配置不同版本的浏览器:
CHROME_VERSION=latest
FIREFOX_VERSION=latest
SAFARI_VERSION=latest
在 playwright.config.js 中已配置多个浏览器项目,支持:
- Chromium (Chrome)
- Chromium Beta
- Firefox
- WebKit (Safari)
- Mobile Chrome (Pixel 5)
- Mobile Safari (iPhone 13)
- Mobile Android (Galaxy S21)
🧪 运行测试
基本命令
# 运行所有测试
pytest
# 运行 Web UI 测试
pytest tests/web -v
# 运行移动端测试
pytest tests/mobile -v
# 运行 API 测试
pytest tests/api -v
# 运行特定标记的测试
pytest -m smoke # 冒烟测试
pytest -m regression # 回归测试
pytest -m critical # 关键测试
高级用法
# 并行运行测试
pytest -n auto
# 生成 HTML 报告
pytest --html=reports/report.html --self-contained-html
# 运行失败时截图
pytest --screenshot=only-on-failure
# 运行失败时录制视频
pytest --video=retain-on-failure
# 指定浏览器运行
BROWSER=firefox pytest tests/web
# 非无头模式运行(显示浏览器)
HEADLESS=false pytest tests/web
使用 Playwright 运行
# 使用 Playwright 运行(支持多浏览器)
npx playwright test
# 运行特定浏览器
npx playwright test --project=chromium
npx playwright test --project=firefox
npx playwright test --project=webkit
# 运行移动端测试
npx playwright test --project=mobile-chrome
npx playwright test --project=mobile-safari
📊 测试报告
HTML 报告
运行测试后,会在 reports/ 目录生成 HTML 报告:
pytest --html=reports/report.html --self-contained-html
报告包含:
- 测试执行摘要
- 通过/失败/跳过的测试统计
- 详细的测试结果
- 失败测试的错误信息和截图
- 执行时间统计
查看报告
# 在浏览器中打开报告
open reports/report.html
# 或使用 Python 内置服务器
python -m http.server 8000
# 然后访问 http://localhost:8000/reports/report.html
📧 邮件通知
配置邮件
在 .env 文件中配置邮件相关参数:
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASSWORD=your_app_password # Gmail 需要使用应用专用密码
EMAIL_TO=recipient@example.com
EMAIL_CC=cc@example.com
SEND_EMAIL=true
Gmail 配置说明
如果使用 Gmail,需要:
- 启用两步验证
- 生成应用专用密码:https://myaccount.google.com/apppasswords
- 使用应用专用密码作为
SMTP_PASSWORD
邮件内容
邮件包含:
- HTML 看板: 可视化的测试结果大屏,包含:
- 测试状态徽章(通过/警告/失败)
- 测试统计卡片(总数、通过、失败、跳过)
- 通过率进度条
- 测试环境信息
- 附件: 完整的 HTML 测试报告
自动发送
测试完成后,如果 SEND_EMAIL=true,框架会自动发送邮件报告。
💡 最佳实践
1. 页面对象模型 (POM)
使用页面对象封装页面元素和操作:
from pages.bybit_home_page import BybitHomePage
def test_example(home_page: BybitHomePage):
home_page.open()
home_page.click_login()
assert "login" in home_page.get_current_url()
2. 使用 Fixtures
利用 pytest fixtures 管理测试资源:
def test_with_authenticated_user(authenticated_page):
# authenticated_page 已经登录
# 直接进行需要登录的测试
pass
3. 测试标记
使用 pytest markers 组织测试:
@pytest.mark.smoke
@pytest.mark.critical
def test_important_feature():
pass
4. 参数化测试
使用参数化减少重复代码:
@pytest.mark.parametrize("symbol", ["BTCUSDT", "ETHUSDT", "BNBUSDT"])
def test_multiple_symbols(api_client, symbol):
response = api_client.get_ticker(symbol)
assert response.get("retCode") == 0
5. 错误处理
合理使用异常处理和重试:
from utils.helpers import retry_on_failure
@retry_on_failure(max_retries=3, delay=1)
def flaky_operation():
# 可能不稳定的操作
pass
🔧 常见问题
Q: 如何切换浏览器?
A: 设置环境变量或修改 .env 文件:
BROWSER=firefox pytest tests/web
Q: 测试失败时如何查看截图?
A: 截图自动保存在 screenshots/ 目录,失败测试的截图命名格式为:{test_name}_failure.png
Q: 如何禁用邮件发送?
A: 在 .env 文件中设置:
SEND_EMAIL=false
Q: API 测试需要凭证吗?
A: 公共 API 测试不需要凭证,私有 API 测试需要配置 API_KEY 和 API_SECRET。
Q: 如何添加新的测试用例?
A: 在对应的测试目录下创建新的测试文件,遵循命名规范 test_*.py,使用页面对象模型编写测试。
Q: 移动端测试如何运行?
A: 移动端测试使用响应式设计测试,通过设置移动端 viewport 和 user agent 模拟移动设备。
📝 开发指南
添加新页面对象
- 在
pages/目录创建新文件 - 继承
BasePage类 - 定义页面元素选择器
- 实现页面操作方法
示例:
from pages.base_page import BasePage
class NewPage(BasePage):
BUTTON_SELECTOR = "button.submit"
def click_button(self):
self.click(self.BUTTON_SELECTOR)
添加新 API 端点
在 api/client.py 中添加新方法:
def new_endpoint(self, param: str) -> Dict[str, Any]:
response = self.get("v5/new/endpoint", params={"param": param})
return response.json()
📄 许可证
MIT License
👥 贡献
欢迎提交 Issue 和 Pull Request!
📞 支持
如有问题,请创建 Issue 或联系维护团队。
Happy Testing! 🚀
评论区