uiautomator2 是一个Android UI自动化框架,底层是基于Google uiautomator实现的,功能较uiautomator有相当大的提升,大大提高了自动化代码别那些的效率
原理是在手机上运行了一个http服务器,将uiautomator中的功能开放出来,然后再将这些http接口,封装成Python库。github开源地址:https://github.com/openatx/uiautomator2
工作机制
python 端:运行脚本,并向移动端发送http请求
移动端:运行封装了uiautomator2的http服务,解析收到的请求,并转换成uiautomator2的代码
整个过程:
- 再移动设备上安装 atx-agent(守护进程),随后atx-agent 启动uiautomator2服务(默认7912)进行监听
- 在PC上编写测试脚本并执行(相当于发送HTTP请求到移动设备的server端)
- 移动设备通过WiFi或者usb接收到PC上发来的HTTP请求,执行指定的操作
安装及使用
使用pip进行安装:
pip install -U uiautomator2
安装完验证(必须提前安装好adb-tool)
安装有问题可以到 issue 列表查询:https://github.com/openatx/uiautomator2/wiki/Common-issues
安装weditor
weditor 是一款基于浏览器的 UI 查看器,用来帮助我们查看 UI 元素定位。
因为 uiautomator 是独占资源,所以当 atx 运行的时候 uiautomatorviewer 是不能用的,为了减少 atx 频繁的启停,就需要用到此工具
使用pip安装:
pip install -U weditor
查看安装是否成功
weditor --help
启动方式:
python -m weditor
# 或者直接在命令行运行
weditor
Windows系统可以使用命令在桌面创建一个快捷方式
python -m weditor --shortcut
除此之外pillow也是需要的,因为截图功能需要这个模块
pip install pillow
装本库所需要的设备端程序:uiautomator-server、atx-agent、openstf/minicap、openstf/minitouch,通过以下命令:
python -m uiautomator2 init
命令行启动 python -m weditor 会自动打开浏览器,输入设备的ip或者序列号,点击Connect即可。
