UiAutomator python-uiautomator2-atx 踩坑记录 (关于 python 与 uiautomator2 版本兼容)

jinglebell · 2019年04月04日 · 最后由 jinglebell 回复于 2019年08月15日 · 3080 次阅读

背景

最近组内在推 atx+uiautomator2+python 的 app ui 自动化测试框架,照着组内技术大佬的 readme 和社区相关帖子搭环境,总是在初始化 uiautomator2 时报错,报错都来 uiautomator2 内部方法,折腾了 1 天,没找到原因(也在社区相关帖提问),最后在大佬协助下发现踩了两个坑,发帖分享一下,避免后面的伙伴一起入坑。

python 部分

最开始使用的 python3.7,执行

python -m uiautomator2 init

报错 adbutils.Adb().devices 这个模块不存在,点进去看 devices 这个方法已经被注解了,
我在https://testerhome.com/topics/11357 第 106 楼有发帖提问。
把 devices 的注解去掉,再执行,adbutils.Adb().shell() 又报了一个 NoneType 错误!

adbutils 是 uiautomator2 自带的方法。为什么会返回 None? 一层一层追下去....

D:\Python37\Lib\site-packages\adb\command\__init__.py

方法就是返的 None,惊不惊喜?意不意外?

python3.7 shell() 的源码

def shell(self, cmd, handler=None, timeout=None):
    conn = self.create_connection(timeout=timeout)
    cmd = "shell:{}".format(cmd)
    conn.send(cmd)

python3.7 shell() 里调用的 create_connection() 源码

from adb.connection import Connection

class Command:
    def create_connection(self, *args, **kwargs):
        return None

解决方案:把 Python3.7 降到 python3.5,问题解决,devices() 没被注释,shell() 引用的
execute() 方法也没报错 (嗯,换成 3.5 就不是应用什么 create_connection() 了)。

python3.5 shell() 的相关源码

def shell(self, *args, **kwargs):
    args = ['shell'] + list(args)
    return self.execute(*args, **kwargs)

结论:uiautormator2 目前暂时不兼容 python3.7,请用 python3.5 入坑

uiautomator2 部分

刚开始用pip install --pre uiautomator2 安装的是 uiautomator2 v0.2.0 版本。
还是执行python -m uiautomator2 init 。报的错更离奇,说 adbutils 里的 Adb 类不存在,w t f !
这下是真懵了,找大佬把他本地的 adbutils.py 拷给我 Adb 类就是正常的。

解决方案:安装 uiautormator2 时指定 0.1.9 版本,不会有这个问题。

推测原因:去 github 上看了下,v0.2.0 把原来 adbutils 里的 Adb 类改了 (包括名字),
main.py里 Installer 类还是继承的 adbutils.Adb,估计是作者发版本太匆忙修改有遗漏吧。

总结

相比 appium,uiautomator2 的维护人数较少,多少存在些鬼畜坑,网上能查阅的资料偏少。
但它体量比 appium 小,响应速度也比 appium 快,根据项目自身情况,结合 atx 后作为 app ui
自动化框架也是个不错的选择。目前组内大佬还在试验它对 ios 是否足够友好?

共收到 2 条回复 时间 点赞

引用

uiautomator2 部分
刚开始用 pip install --pre uiautomator2 安装的是 uiautomator2 v0.2.0 版本。
还是执行 python -m uiautomator2 init 。报的错更离奇,说 adbutils 里的 Adb 类不存在,w t f !
这下是真懵了,找大佬把他本地的 adbutils.py 拷给我 Adb 类就是正常的。

解决方案:安装 uiautormator2 时指定 0.1.9 版本,不会有这个问题。

推测原因:去 github 上看了下,v0.2.0 把原来 adbutils 里的 Adb 类改了 (包括名字),

但 main.py 里 Installer 类还是继承的 adbutils.Adb,估计是作者发版本太匆忙修改有遗漏吧。

问题:
我现在是 python3.6 , init 时报错:
class Installer(adbutils.Adb):
AttributeError: module 'adbutils' has no attribute 'Adb' ,
请问怎么处理

萄小红 回复

应该不是 python 3.6 的问题,请检查下 ui2 的版本,这个报错与我文中提到的相同,都是缺 Adb 这个类,ui2 v0.2.0 一定有这个问题,建议安装 v0.2.1 或 v0.1.9 我看源码,应该已经修复了这个坑。如果还不行,请用 python 3.5 + ui2 v0.1.9 的组合(目前我们组在用,比较稳定,暂无其它鬼畜问题)

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