介绍:
uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点:
1、测试脚本只能使用Java语言。
2、测试脚本必须每次被上传到设备上运行。 我们希望测试能够用一个更脚本化的语言,例如Python编写,同时可以每次所见即所得地修改测试、运行测试。
安装:
pip install --pre uiautomator2
pip install pillow
初始化:
部署相关的守护进程。
电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch
python -m uiautomator2 init
安装完成,设备上会多一个uiautomator的应用。
weditor安装:
我们可以借助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