feat: 基于 UI 自动化最佳实践的测试框架骨架搭建

- tests/config: 环境配置 (.yaml) + 配置加载器
- tests/utils: 日志工具 + 测试数据生成器
- tests/web: Playwright conftest + Page Object 基类 + 登录页 + 导航栏组件 + 登录用例(6条)
- tests/app: Appium conftest + Page Object 基类 + 司机端首页 + 登录用例(3条)
- pytest.ini: markers(p0/p1/p2/smoke/web/app) + 运行配置
- 所有用例按最佳实践采用 Page Object 模式 + data-testid 优先定位 + 失败自动截图
This commit is contained in:
xst
2026-07-15 10:24:22 +08:00
parent 26ef99f7e7
commit aade9e39d0
17 changed files with 1034 additions and 0 deletions
+98
View File
@@ -0,0 +1,98 @@
"""
App 端根级 fixtureAppium Driver 实例 + 失败自动截图。
提供:
app_config — session 级 App 配置
driver — Appium WebDriver(每个用例独立)
platform — 当前平台 android / ios
Usage:
def test_app_login(driver, platform):
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "login_btn").click()
"""
from datetime import datetime
from pathlib import Path
import pytest
SCREENSHOTS_DIR = Path("output/screenshots")
@pytest.fixture(scope="session")
def app_config(config):
"""session 级:App 配置段。"""
return config["app"]
@pytest.fixture
def platform(app_config) -> str:
"""当前执行的平台(默认 android,后续可参数化)。"""
return app_config.get("platforms", ["android"])[0]
@pytest.fixture
def driver(app_config, platform, request):
"""每个用例独立的 Appium driver,失败自动截图。
注意:运行前需确保 Appium Server 已启动(appium --log-level info)。
"""
try:
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.options.ios import XCUITestOptions
except ImportError:
pytest.skip("Appium-Python-Client 未安装,跳过 App 测试。")
appium_host = app_config.get("appium_host", "http://localhost:4723")
screenshot_on_failure = app_config.get("screenshot_on_failure", True)
driver = None
try:
if platform == "android":
android_cfg = app_config["android"]
options = UiAutomator2Options()
options.device_name = android_cfg["device_name"]
options.app_package = android_cfg["app_package"]
options.app_activity = android_cfg["app_activity"]
options.no_reset = android_cfg.get("no_reset", True)
options.new_command_timeout = android_cfg.get("new_command_timeout", 120)
options.automation_name = "UiAutomator2"
else:
ios_cfg = app_config["ios"]
options = XCUITestOptions()
options.device_name = ios_cfg["device_name"]
options.bundle_id = ios_cfg["bundle_id"]
options.no_reset = ios_cfg.get("no_reset", True)
options.new_command_timeout = ios_cfg.get("new_command_timeout", 120)
options.automation_name = "XCUITest"
driver = webdriver.Remote(appium_host, options=options)
yield driver
# ── 失败自动截图 ──
if (
screenshot_on_failure
and hasattr(request.node, "rep_call")
and request.node.rep_call.failed
and driver is not None
):
scenario_dir = SCREENSHOTS_DIR / request.node.name
scenario_dir.mkdir(parents=True, exist_ok=True)
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
path = str(scenario_dir / f"FAIL_{platform}_{ts}.png")
driver.save_screenshot(path)
finally:
if driver is not None:
driver.quit()
# ── pytest 报告 hook ──
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
"""记录用例执行结果,供失败截图使用。"""
outcome = yield
rep = outcome.get_result()
setattr(item, f"rep_{rep.when}", rep)