问题描述
场景: 我正在尝试通过在 os.popen() 中使用 shell 命令将 ADB 连接到 android 手机,作为自动化框架测试的一部分,我能够连接但“adb devices”将设备返回为已连接但未经授权,示例代码:
def test_connect_phone(self,phone):
connect_stream = os.popen(f'adb connect {phone.host}:{phone.port}')
print(connect_stream.read())
install_stream = os.popen('adb devices')
output = install_stream.read()
print(output)
assert output
# output
connected to <HOST>:<PORT>
List of devices attached
<HOST>:<PORT> unauthorized
直接从终端运行相同的命令即可,设备已获得授权:
adb connect <HOST>:<PORT>
adb devices
#output
connected to <HOST>:<PORT>
List of devices attached
<HOST>:<PORT> device
注意:我在 pytest 中使用了 tox,这让我认为这可能是环境问题,但在测试中,我在运行时看到我的主目录保持不变。 任何帮助表示赞赏。
解决方法
找到解决方案,显然我需要在“adb connect”之后添加 sleep(1),因为设备需要一点时间才能获得授权