如何用Robot Framework多设备执行测试及Set Serial关键字使用求助
解决双Android设备蓝牙配对自动化中的
Set Serial问题 一、先搞懂Set Serial到底是什么
Set Serial是Robot Framework结合UIAutomator库时的核心关键字之一,作用很直白:当你连接了多台Android设备时,用它来指定后续所有UIAutomator操作的目标设备。因为ADB是通过设备唯一的序列号(Serial Number)来区分不同设备的,这个关键字就是帮你给当前测试会话绑定目标设备的序列号,避免操作串到其他设备上。
二、Set Serial的正确使用示例
首先你得先获取两台设备的序列号,在命令行执行adb devices,会得到类似这样的输出:
List of devices attached emulator-5554 device 89ABC123 device
假设emulator-5554是设备A,89ABC123是设备B,那么蓝牙配对的脚本可以这么写:
*** Settings *** Library UIAutomator *** Test Cases *** Pair Two Devices Via Bluetooth # 切换到设备A,打开蓝牙 Set Serial emulator-5554 Click text="Settings" Click text="Bluetooth" Click id="com.android.settings:id/switch_widget" # 实际控件ID需根据设备调整 Wait Until Element Is Visible text="Bluetooth is on" # 切换到设备B,搜索并配对设备A Set Serial 89ABC123 Click text="Settings" Click text="Bluetooth" Click id="com.android.settings:id/switch_widget" Wait Until Element Is Visible text="设备A的蓝牙名称" # 替换为你设备A的实际蓝牙名称 Click text="设备A的蓝牙名称" Click text="Pair" # 切回设备A,确认配对请求 Set Serial emulator-5554 Wait Until Element Is Visible text="Pair request" Click text="Pair" Wait Until Element Is Visible text="Connected"
提示:脚本里的控件定位(比如ID、文本)需要根据你测试的Android版本和设备型号调整,建议用UIAutomatorViewer抓取准确的控件信息。
三、如果Set Serial还是报错,试试这些替代方案
由于你用的是比较旧的环境(Python2.7 + RIDE1.5.2.1),可能存在库兼容性问题,这时候可以换两种思路:
方案1:直接用ADB命令结合Robot Framework
ADB本身支持指定序列号执行命令,你可以把蓝牙操作封装成关键字:
*** Keywords *** Turn On Bluetooth On Device [Arguments] ${serial} Run adb -s ${serial} shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE Run adb -s ${serial} shell input keyevent KEYCODE_BACK # 关闭授权弹窗(如果有) Pair Bluetooth Devices [Arguments] ${serial_target} ${device_mac} Run adb -s ${serial_target} shell bluetoothctl scan on Run adb -s ${serial_target} shell bluetoothctl pair ${device_mac} # 替换为设备A的蓝牙MAC地址 Run adb -s ${serial_target} shell bluetoothctl connect ${device_mac}
方案2:使用robotframework-adb库管理多设备
这个库对多设备的支持更灵活,兼容Python2.7,先通过pip install robotframework-adb安装,然后脚本可以这么写:
*** Settings *** Library ADB *** Test Cases *** Pair Devices With ADB Library # 初始化两台设备 ${device_a}= Get Device emulator-5554 ${device_b}= Get Device 89ABC123 # 设备A打开蓝牙 ${device_a}.Turn Bluetooth On # 设备B搜索并配对 ${device_b}.Turn Bluetooth On ${device_b}.Start Bluetooth Scan ${device_b}.Pair Bluetooth Device 设备A的MAC地址 ${device_b}.Connect Bluetooth Device 设备A的MAC地址 # 设备A确认配对(如果需要) ${device_a}.Accept Bluetooth Pairing Request
四、常见报错排查
如果你用Set Serial时报错,大概率是这几个原因:
- 序列号写错了:仔细核对
adb devices输出的序列号,别多打少打字符 - 设备未正确连接:执行
adb -s ${serial} shell试试能不能进入设备shell,不行就重启ADB服务(adb kill-server && adb start-server) - UIAutomator库版本问题:旧版本对多设备支持可能有bug,试试升级到该库支持Python2.7的最新版本
内容的提问来源于stack exchange,提问作者Vedhika
相关产品推荐
相关产品推荐

