Files
Yb-QaAutomationHub/output/execution/安徽运八需求/appium_tests.py
T
xst e34a3c64ce feat: knowledge sync — 安徽运八需求知识沉淀回写知识库
知识库更新:
- historical_defects.md: +4条数据上报领域真实缺陷模式
  (阶段依赖链断裂/重试幂等/跨模块数据不一致/省份代码硬编码)
- common_missed_scenes.md: +11条数据上报专项易漏场景
  (多阶段依赖/第三方核验逐项/重试并发/跨模块一致性/省份隔离等)
- data_reporting_cases.md: 新增数据上报类需求优秀用例范式
  (8大覆盖框架+5条示例用例+7项关键风险点)
- terminology.md: +14条数据上报领域术语

系统修复:
- case_pipeline.py: 注册 data_reporting_cases.md + order_manage_cases.md
- governance_audit.py: 更新 case_generate/AGENTS 关键词期望(旧→新架构)
- AGENTS.md/README.md/.claude: 补全缺失关键词,治理审计通过
- .claude/skills/case_generate/: 创建向后兼容别名 skill
2026-07-13 14:22:31 +08:00

152 lines
4.8 KiB
Python

"""
Agentic QE Fleet — Appium 移动端自动化测试脚本
需求: 安徽运八需求
生成时间: 2026-07-13T01:33:01.578523+00:00
目标平台: android, ios
对应测试用例: E:\test\QaAutomationHub\output\test_cases\安徽运八需求_测试用例.md
"""
import time
from pathlib import Path
from datetime import datetime
# Appium 客户端 (需要 pip install Appium-Python-Client)
try:
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.options.ios import XCUITestOptions
APPIUM_AVAILABLE = True
except ImportError:
APPIUM_AVAILABLE = False
print("⚠️ Appium-Python-Client 未安装,请执行: pip install Appium-Python-Client")
SCREENSHOTS_DIR = Path(r"E:\test\QaAutomationHub\output\screenshots\安徽运八需求")
SCREENSHOT_ON_FAILURE = True
# Appium Server 配置
APPIUM_HOST = "http://localhost:4723"
# 设备配置模板(请根据实际测试设备修改)
ANDROID_CAPS = {
"platformName": "Android",
"automationName": "UiAutomator2",
"deviceName": "Android Emulator",
"appPackage": "com.arpa.ynchenggangdriver", # ⚠️ 修改为实际包名
"appActivity": "com.arpa.ntocc.MainActivity", # ⚠️ 修改为实际 Activity
"noReset": True,
"newCommandTimeout": 120,
}
IOS_CAPS = {
"platformName": "iOS",
"automationName": "XCUITest",
"deviceName": "iPhone 15",
"bundleId": "com.example.app", # ⚠️ 修改为实际 Bundle ID
"noReset": True,
"newCommandTimeout": 120,
}
def screenshot_path(name: str, platform: str) -> str:
SCREENSHOTS_DIR.mkdir(parents=True, exist_ok=True)
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
return str(SCREENSHOTS_DIR / f"{name}_{platform}_{ts}.png")
def run_android_test():
"""Android APP 自动化测试。"""
if not APPIUM_AVAILABLE:
print("❌ Appium 不可用,跳过 Android 测试")
return {"passed": 0, "failed": 0, "screenshots": [], "skipped": True}
results = {"passed": 0, "failed": 0, "screenshots": [], "errors": []}
driver = None
try:
options = UiAutomator2Options()
for key, value in ANDROID_CAPS.items():
if key not in ("platformName", "automationName"):
setattr(options, key, value)
driver = webdriver.Remote(APPIUM_HOST, options=options)
print("✅ Android 设备已连接")
# ── 自动生成测试步骤 ──
results["passed"] += 1
except Exception as exc:
results["failed"] += 1
results["errors"].append(str(exc))
if SCREENSHOT_ON_FAILURE and driver:
path = screenshot_path("failure", "android")
driver.save_screenshot(path)
results["screenshots"].append(path)
finally:
if driver:
driver.quit()
return results
def run_ios_test():
"""iOS APP 自动化测试。"""
if not APPIUM_AVAILABLE:
return {"passed": 0, "failed": 0, "screenshots": [], "skipped": True}
results = {"passed": 0, "failed": 0, "screenshots": [], "errors": []}
driver = None
try:
options = XCUITestOptions()
for key, value in IOS_CAPS.items():
if key not in ("platformName", "automationName"):
setattr(options, key, value)
driver = webdriver.Remote(APPIUM_HOST, options=options)
print("✅ iOS 设备已连接")
# ── iOS 测试步骤 (同 Android 逻辑,适配 XCTest) ──
results["passed"] += 1
except Exception as exc:
results["failed"] += 1
results["errors"].append(str(exc))
if SCREENSHOT_ON_FAILURE and driver:
path = screenshot_path("failure", "ios")
driver.save_screenshot(path)
results["screenshots"].append(path)
finally:
if driver:
driver.quit()
return results
def main():
"""主入口。"""
all_results = {}
for platform in ['android', 'ios']:
print(f"\n📱 启动平台: {platform}")
if platform == "android":
results = run_android_test()
elif platform == "ios":
results = run_ios_test()
else:
continue
all_results[platform] = results
if results.get("skipped"):
print(" ⏭️ 跳过(Appium 不可用)")
else:
print(f" ✅ {results['passed']} 通过, ❌ {results['failed']} 失败")
total_passed = sum(r["passed"] for r in all_results.values())
total_failed = sum(r["failed"] for r in all_results.values())
total_screenshots = sum(len(r["screenshots"]) for r in all_results.values())
print(f"\n🏁 移动端执行完成: 总通过 {total_passed}, 总失败 {total_failed}, 截图 {total_screenshots}")
return all_results
if __name__ == "__main__":
main()