Appium测试Galaxy Watch 5时socket hang up问题的代码级解决咨询
解决方案:在Robot Framework中自动卸载Appium UIAutomator2服务
针对你遇到的socket hang up错误,无需手动执行命令行,可通过以下两种方式在Robot文件中实现自动卸载io.appium.uiautomator2.server和io.appium.uiautomator2.server.test:
方法一:使用Robot Framework内置Process库
直接调用系统命令执行adb卸载操作,无需额外编写Python代码:
步骤1:导入Process库
在Robot文件的Settings段添加:
*** Settings *** Library Process
步骤2:定义卸载关键字
*** Keywords *** Uninstall Appium UIAutomator2 Servers # 卸载主服务 ${result}= Run Process adb uninstall io.appium.uiautomator2.server shell=True Log 卸载uiautomator2主服务结果:${result.stdout} ${result.stderr} # 卸载测试服务 ${result}= Run Process adb uninstall io.appium.uiautomator2.server.test shell=True Log 卸载uiautomator2测试服务结果:${result.stdout} ${result.stderr}
步骤3:在测试流程中调用
可以在测试用例的Teardown阶段执行,或者捕获到目标错误时触发:
*** Test Cases *** Galaxy Watch 功能测试 [Setup] Open Application ${APPIUM_URL} platformName=Android deviceName=GalaxyWatch5 appPackage=你的包名 appActivity=你的启动Activity # 执行测试步骤 Run Keyword And Continue On Failure 执行手表测试逻辑 # 测试结束后自动卸载服务 [Teardown] Uninstall Appium UIAutomator2 Servers Close Application
如果需要在错误发生时立即处理,可结合异常捕获:
*** Keywords *** 执行手表测试逻辑 ${status}= Run Keyword And Return Status 你的核心测试步骤 Run Keyword If ${status} == False Uninstall Appium UIAutomator2 Servers Run Keyword If ${status} == False Reopen Application ${APPIUM_URL} platformName=Android deviceName=GalaxyWatch5 appPackage=你的包名 appActivity=你的启动Activity Run Keyword If ${status} == False 你的核心测试步骤
方法二:自定义Python关键字封装
如果需要更灵活的逻辑(比如指定设备ID),可以编写Python函数封装adb命令:
步骤1:创建Python工具文件
新建appium_helper.py,内容如下:
import subprocess def uninstall_uiautomator2_servers(device_id=None): adb_cmd = ["adb"] if device_id: adb_cmd.extend(["-s", device_id]) # 卸载主服务 subprocess.run(adb_cmd + ["uninstall", "io.appium.uiautomator2.server"], check=False, capture_output=True) # 卸载测试服务 subprocess.run(adb_cmd + ["uninstall", "io.appium.uiautomator2.server.test"], check=False, capture_output=True)
步骤2:在Robot文件中导入自定义库
*** Settings *** Library appium_helper.py
步骤3:调用自定义关键字
# 无指定设备时直接调用 Uninstall Uiautomator2 Servers # 指定设备ID调用(多设备场景) Uninstall Uiautomator2 Servers device_id=你的GalaxyWatch设备ID
注意事项
- 确保环境变量中已配置adb路径,Robot进程能直接调用adb命令;
- 若设备未授权,需先完成adb授权操作;
- 卸载服务后,Appium会在下次启动测试时自动重新安装最新版本的uiautomator2服务。
内容的提问来源于stack exchange,提问作者user22747995
相关产品推荐
相关产品推荐

