https://github.com/openatx/uiautomator2(官方文档)
第一步:先准备一台开启了开发者选项的安卓手机,连接上电脑,确保执行adb devices可以看到连接上的设备,不要开启charles,否则会导致下载失败。
第二步:运行pip3 install -U uiautomator2 (在adb中进行安装)安装uiautomator2。运行python -m uiautomator2 init安装包含httprpc服务的apk到手机+atx-agent, minicap, minitouch。
第三步:运行pip install --upgrade --pre uiautomator2,安装uiautomator2,执行uiautomator2 --help,查看是否安装成功uiautomator2。
第四步:pip install -U weditor,安装weditor UI查看器,因为uiautomator是独占资源,所以当atx运行的时候uiautomatorviewer是不能用的,为了减少atx频繁的启停,我们开发了基于浏览器技术的weditor UI查看器。https://github.com/openatx/weditor
第五步:安装好之后,就可以在命令行运行weditor --help 确认是否安装成功了,安装好之后,执行python -m weditor(如果在黑窗口中打不开,则在pycharm中执行),打开Ui查看器,通过adb devices,查看自己设备的序列号,在web查看器里通过序列号进行连接,点击connect,进行连接连接后,左侧界面无法显示手机页面时,查看cmd窗口,查看连接是否成功,可以重新连接后尝试。以上步骤完成就可以进行编写代码,获取元素进行执行ui自动化了。
第六步:编写代码执行自动化:
uiautomator2底层原理:原理是在手机上运行了一个http rpc服务,将uiautomator中的功能开放出来,然后再将这些http接口封装成Python库。Python脚本执行每个操作都会转换成一个对http rpc服务的请求,http rpc服务接受这些请求,并调用原生uiautomator的api实现在手机上的操作