173 lines
5.9 KiB
Python
173 lines
5.9 KiB
Python
"""
|
|
Agentic QE Fleet — Appium 移动端自动化测试脚本
|
|
需求: 安徽运八需求
|
|
生成时间: 2026-07-14T03:40:32.593268+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.example.app", # ⚠️ 修改为实际包名
|
|
"appActivity": ".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 设备已连接")
|
|
|
|
# ── 自动生成测试步骤 ──
|
|
# [P0] AH_REPORT_DASH_001: 验证看板按完整运单号精确搜索运单
|
|
# driver.save_screenshot(screenshot_path('AH_REPORT_DASH_001', 'android'))
|
|
|
|
# [P0] AH_REPORT_DASH_002: 验证看板按运单号模糊搜索匹配多条记录
|
|
# driver.save_screenshot(screenshot_path('AH_REPORT_DASH_002', 'android'))
|
|
|
|
# [P1] AH_REPORT_DASH_003: 验证看板按不存在的单号搜索显示空结果
|
|
# driver.save_screenshot(screenshot_path('AH_REPORT_DASH_003', 'android'))
|
|
|
|
# [P1] AH_REPORT_DASH_004: 验证看板按"第一次上报"阶段筛选运单
|
|
# driver.save_screenshot(screenshot_path('AH_REPORT_DASH_004', 'android'))
|
|
|
|
# [P1] AH_REPORT_DASH_005: 验证看板按"异常"核验状态筛选运单
|
|
# driver.save_screenshot(screenshot_path('AH_REPORT_DASH_005', 'android'))
|
|
|
|
# [P1] AH_REPORT_DASH_006: 验证看板按"申诉中"申诉状态筛选运单
|
|
# driver.save_screenshot(screenshot_path('AH_REPORT_DASH_006', 'android'))
|
|
|
|
# [P1] AH_REPORT_DASH_007: 验证看板组合筛选—第二次上报+异常+申诉中+运单号模糊搜索
|
|
# driver.save_screenshot(screenshot_path('AH_REPORT_DASH_007', '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()
|