""" 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()