python 进入adb shell 交互模式

背景:写UI自动化case遇到的问题-> app有个截屏的功能,做断言时需要进入手机sd卡查看是否截屏成功,在网上搜到的这个方法亲测能用

# 执行shell 命令
os.system('adb shell')
cmds = [
"cd sdcard/Pictures",
"ls",
"exit",
# 这是是非常关键的,退出
]
obj = subprocess.Popen("adb shell", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
info = obj.communicate(("\n".join(cmds) + "\n").encode('utf-8'))
for item in info:
if item:
     #输出的是一个字符串,可以字符串切片获取到最新的截图名称
name = item.decode('gbk')
     print(name)


posted @ 2021-03-29 15:31  正气的割草机  阅读(563)  评论(0)    收藏  举报