Google uiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作,但有两个缺点:
1、测试脚本只能使用Java语言。
2、测试脚本必须每次被上传到设备上运行。 我们希望测试能够用一个更脚本化的语言,例如Python编写,同时可以每次所见即所得地修改测试、运行测试。
uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库。其底层基于Google uiautomator
实现原理简介
第一步还是需要手机连接到电脑的。电脑使用adb向手机推送了一个二进制文件: atx-agent. 然后启动这个程序,程序自身屏蔽掉SIGHUP信号,这样数据线断开之后,程序就不会被杀掉了。
第二步拔掉数据线,保持手机的Wifi开启,并且一直有电的状态。
第三步 atx-agent实际上还是一个http服务器,运行在电脑上的python程序使用HTTP协议跟手机上atx-agent通信,atx-agent随后启动uiautomator.apk这个应用,将收到的请求转换成UiAutomator的UI自动化操作。
安装uiautomator2
pip install –pre SomePackage # 安装预发行和安装的版本,默认安装的是稳定版本
pip install pillow
pip install --pre uiautomator2
或者
git clone https://github.com/openatx/uiautomator2
pip install -e uiautomator2
pip install pillow
部署守护进程
python -m uiautomator2 init
电脑连接上一个手机或多个手机, 确保adb已经添加到环境变量中,执行下面的命令会自动安装本库所需要的设备端程序:uiautomator-server 、atx-agent、openstf/minicap、openstf/minitouch
安装完成,设备上会多一个uiautomator的应用。
连接设备
通过wifi连
要求设备IP和pc在同一网络中
import uiautomator2 as u2
d = u2.connect(‘设备ip’)
print(d.info)
通过usb连
import uiautomator2 as u2
d = u2.connect(‘设备序列号’)
print(d.info)
u2.connect()不带参数调用,uiautomator2将从环境变量中获取设备ip(ANDROID_DEVICE_IP),如果此环境变量为空,则uiautomator将退回到connect_usb,需要保证只有一台设备连接到电脑
WiFi连接更方便一点,需要保持PC和手机使用的一个WIFI,查看手机连接WIFI的IP地址。
测试
编写以下脚本进行验证:
import uiautomator2 as u2
d = u2.connect('192.168.2.106')
print(d.info)
USB连接
import uiautomator2 as u2
d = u2.connect('dd3ffde6')
print(d.info)
定位元素:
我们可以借助Android SDK自的uiautomatorviewer查看元素,这就要求手机必须以USB的方式连接PC。
前面使用的是WIFI连接进行连接的。openatx提供了另外一个工具weditor 来解决这个问题。
GitHub地址:https://github.com/openatx/weditor
安装weditor UI查看器
pip install -U weditor
运行 python -m weditor
备注:每次启动时运行python -m uiautomator2 init 和 python -m weditor就可以了
运行 python -m weditor
默认会通过浏览器打开页面:http://localhost:17310/ (IE存在兼容问题,Chrom可以正确打开)
在页面左上角选择Android,输入设备IP(192.168.31.234),点击Connect按钮。
当我们操作完手机后,可以点击“Reload”按钮进行刷新,从而保持与设备上的界面保持同步。weditor 还可以帮我们生成代码。
总之,你研究一下就会用了。
编写测试脚本
app_start()和app_stop()用于启动和停止应用。
常用的定位方式:
ResourceId定位:
d(resourceId="com.meizu.mzbbs:id/tp").click()
Text定位:
d(text="精选").click()
Description定位:
d(description="..").click()
ClassName定位:
d(className="android.widget.TextView").click()
获取所选UI对象状态及其信息
self.d(resourceId=“com.kwai.global.video.social.kwaigo:id/item_icon”).exists(timeout=3)
self.d(resourceId=“com.kwai.global.video.social.kwaigo:id/name”, instance=2).get_text()
d(resourceId="com.bocommlife.healthywalk:id/tv_head_title").exists(timeout=10)
Title=d(resourceId="com.bocommlife.healthywalk:id/tv_head_title").get_text()
print Title
部署守护进程的过程
就是往手机了装了4个东西minicap minitouch uiautomator_apk atx-agent ,其实是5个东西 uiautomator_apk安装了app-uiautomator.apk和app-uiautomator-test.apk两个apk
执行过程中出错
原因:未启动adb
摘自
uiautomator2 使用Python测试 Android应用 https://www.cnblogs.com/fnng/p/8486863.html
Uiautomator2基础 https://blog.csdn.net/xiadanying/article/details/90703023
浅谈自动化测试工具 python-uiautomator2 https://testerhome.com/topics/11357
ATX 给大家介绍一下 uiautomator2 这个项目 https://testerhome.com/topics/10881
ATX uiautomator2 init 失败及其他问题的解决办法 https://testerhome.com/topics/17257
Uiautomator2命令 https://blog.csdn.net/xiadanying/article/details/90720343