Linux 解决自动化测试设备掉线:Python 方案

codeskyblue · 2017年07月02日 · 最后由 IAmTester 回复于 2022年03月14日 · 3175 次阅读
本帖已被设为精华帖!

起源

看了原文 解决设备掉线问题:软件方案激动不已。
以前每次都要跑过去插拔手机,现在在键盘上敲一个命令就搞定了。

那篇文章还有一个问题没有解决,就是如何根据设备的 Serial 去重置设备?
因为对 Python 比较熟悉,查找了下 libusb 相关的库,顺利找到一个看样子很鲁棒的库 https://github.com/pyusb/pyusb

使用方法

Linux 需要运行在 Root 权限下

# 列出所有设备
$ python adbusb.py list
bf755cab    Android
3f980000.usb    DWC OTG Controller
# 重置设备bf755cab的USB连接
$ python adbusb.py reset bf755cab

# 只填写前缀也可以
$ python adbusb.py reset bf

安装

$ pip install --pre pyusb
$ pip install fire # 命令行解析,用于下面的脚本

Mac 系统还需要安装brew install libusb, Linux 貌似自己就支持,反正我没装什么别的依赖

代码是针对 python2 写的,如需兼容 py3,需要自行修改

将下面的文件保存成 adbusb.py

#!/usr/bin/env python
# coding: utf-8
#

import usb
import fire


class AdbUSB(object):
    def list(self):
        for d in usb.core.find(find_all=True):
            if d.serial_number:
                print('%s\t%s' %(d.serial_number, d.product))

    def reset(self, serial):
        devices = {}
        for d in usb.core.find(find_all=True):
            if d.serial_number:
                devices[d.serial_number] = d

        ok_serials = []
        for sn in devices.keys():
            if sn.startswith(str(serial)):
                ok_serials.append(sn)

        if len(ok_serials) == 1:
            serial = ok_serials[0]
            d = devices.get(serial)
            try:
                d.reset()
            except usb.core.USBError:
                pass
        elif len(ok_serials) == 0:
            print('No device serial number is', serial)
        else:
            print('Too many device matched', serial)


if __name__ == '__main__':
    fire.Fire(AdbUSB)
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 13 条回复 时间 点赞
思寒_seveniruby 将本帖设为了精华贴 07月03日 05:51

python3.0 老提示 core.fire 这个有问题
File "E:\Python36\lib\site-packages\fire\core.py", line 120, in Fire
component_trace = _Fire(component, args, context, name)
File "E:\Python36\lib\site-packages\fire\core.py", line 358, in _Fire
component, remaining_args)
File "E:\Python36\lib\site-packages\fire\core.py", line 561, in _CallCallable
result = fn(*varargs, **kwargs)

路了个飞 回复

可以自己用 argpaser 改写下就好了

楼主是在 os x 下测试的?刚在 ubuntu 上测试了下,报以下错误:
Traceback (most recent call last):
File "adb_usb.py", line 40, in
fire.Fire(AdbUSB)
File "/home/tester/.local/lib/python2.7/site-packages/fire/core.py", line 120, in Fire
component_trace = _Fire(component, args, context, name)
File "/home/tester/.local/lib/python2.7/site-packages/fire/core.py", line 358, in _Fire
component, remaining_args)
File "/home/tester/.local/lib/python2.7/site-packages/fire/core.py", line 561, in _CallCallable
result = fn(*varargs, **kwargs)
File "adb_usb.py", line 12, in list
if d.serial_number:
File "/home/tester/.local/lib/python2.7/site-packages/usb/core.py", line 830, in serial_number
self._serial_number = util.get_string(self, self.iSerialNumber)
File "/home/tester/.local/lib/python2.7/site-packages/usb/util.py", line 314, in get_string
raise ValueError("The device has no langid")
ValueError: The device has no langid

甬力君 回复

用 root 就不会报错了

7楼 已删除

什么原理?

dengwei729 回复

该怎么解释呢

手机长时间连接后,device 找不到设备了。怎么处理呢?

所有的 offline 都有效吗?

超级实用啊,一番研究,在 python 虚拟环境里成功。(安装依赖折腾了很久,我的 pip 安装竟然也要 sudo)
改成了 Python3 的,确实有效。目前找到了最实用的方法。😀

幸好看是大神的方案,不然就放弃尝试了,配合 atxserver2 简直太爽了,再也不用去插拔 USB 线了

哈哈,这文章竟然还有人看。代码我更新了一下,改成了 python3 的了

windows 下是否可以使用?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册