定位健壮性原则: 语义文本 > 组件库稳定 class > 层级结构 > nth-child/绝对路径。
Web (freight_accounting_page):
- 新增 BasePage.first_matching(*候选): 文本锚点优先命中, 旧结构路径兜底
- 13 处定位器改造: 核算/审核通过/确认打款按钮(get_by_role 文本)、
车队长/收款模式表单(.el-form-item label 锚定)、光大单选(label:has-text)、
获取验证码/验证码输入(按钮同行锚定)、message-box 确定等
- 手机号单选保留旧结构路径优先(弹窗含多组电话单选, 文本有歧义)
- 已实况验证: 获取验证码/确认打款/光大锚点唯一命中
App (freight_flow_page):
- 货源 tab: text("货源") 文本锚点优先(登录流程持续验证), 位置序号兜底
- 货源单列表项: RecyclerView(lRrcyclerView) 第一个子项, 全局 instance(3) 兜底
- 选图按钮(ImageView instance(2))保留: 需在途订单才能探测, 当前可用
失败诊断:
- Web 失败时 attach 页面 DOM 快照, App 失败时 attach page_source 到 Allure
验收: 实况锚点解析全部唯一命中; 两条 E2E 通过且 DB 终态均为全流程完成
(status=5, 已结算, 双支付完成)。
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
"""Allure 集成辅助 — 截图附件 + 报告环境信息。
|
|
|
|
allure-pytest 未安装时所有函数静默降级(截图仍保存到本地文件)。
|
|
"""
|
|
|
|
from pathlib import Path
|
|
|
|
try:
|
|
import allure
|
|
except ImportError: # pragma: no cover
|
|
allure = None
|
|
|
|
|
|
def attach_png(path: str, name: str = "screenshot") -> None:
|
|
"""把 PNG 文件附加到 Allure 报告当前用例/步骤上。"""
|
|
if allure is None:
|
|
return
|
|
try:
|
|
allure.attach.file(path, name=name, attachment_type=allure.attachment_type.PNG)
|
|
except Exception:
|
|
pass # 附件失败不影响用例主流程
|
|
|
|
|
|
def attach_text(content: str, name: str, attachment_type=None) -> None:
|
|
"""把文本内容(DOM 快照/page_source 等)附加到 Allure 当前用例上。"""
|
|
if allure is None:
|
|
return
|
|
try:
|
|
allure.attach(
|
|
content, name=name,
|
|
attachment_type=attachment_type or allure.attachment_type.TEXT,
|
|
)
|
|
except Exception:
|
|
pass # 附件失败不影响用例主流程
|
|
|
|
|
|
def write_environment_properties(results_dir: str, config: dict) -> None:
|
|
"""向 allure-results 目录写入 environment.properties(报告环境页展示)。"""
|
|
if not results_dir:
|
|
return
|
|
admin = config.get("admin", {})
|
|
lines = [
|
|
f"base_url={admin.get('base_url', '')}",
|
|
f"browsers={','.join(admin.get('browsers', []))}",
|
|
f"skip_login={admin.get('skip_login', '')}",
|
|
"platform=Windows",
|
|
"project=运八网络货运平台",
|
|
]
|
|
# Allure 按 ISO-8859-1 解析 properties,非 ASCII 内容转 \uXXXX 防乱码
|
|
content = "\n".join(lines) + "\n"
|
|
content = content.encode("ascii", "backslashreplace").decode("ascii")
|
|
out_dir = Path(results_dir)
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
|
(out_dir / "environment.properties").write_text(content, encoding="ascii")
|