介绍:python
uiautomator2 是一个能够使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator,Google提供的uiautomator库能够获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操做,但有两个缺点:git
一、测试脚本只能使用Java语言。github
二、测试脚本必须每次被上传到设备上运行。 咱们但愿测试可以用一个更脚本化的语言,例如Python编写,同时能够每次所见即所得地修改测试、运行测试。浏览器
安装:工具
pip install --pre uiautomator2
pip install pillow
初始化:测试
部署相关的守护进程。ui
电脑链接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所须要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouchspa
python -m uiautomator2 init
安装完成,设备上会多一个uiautomator的应用。code
weditor安装:server
咱们能够借助Android SDK自的uiautomatorviewer查看元素,这就要求手机必须以USB的方式链接PC,我前面使用的是WIFI链接进行链接的。因此,openatx提供了另一个工具weditor 来解决这个问题。
weditor是一个能够让uiautomator显示Android UI界面的工具。
pip install --pre --upgrade weditor
使用:
python -m weditor
执行命令后,默认会经过浏览器打开页面
在页面左上角选择Android,输入设备IP(192.168.31.234),点击Connect按钮。
当咱们操做完手机后,能够点击“Reload”按钮进行刷新,从而保持与设备上的界面保持同步。weditor 还能够帮咱们生成代码。
***还能够无线连接,在同一WiFi下,手机连接IP地址。
GitHub地址:https://github.com/openatx/uiautomator2
GitHub地址:https://github.com/openatx/weditor