1.前言关于uiautomator2:
python-uiautomator2封装了谷歌自带的uiautomator2测试框架,提供便利的python接口。他允许测试人员直接在PC上编写Python的测试代码,操作手机应用,完成自动化,大大提高了自动化代码编写的效率。
工作原理
以下图片使用Windows画图软件
如图所示,python-uiautomator2主要分为两个部分,python客户端,移动设备
- python端: 运行脚本,并向移动设备发送HTTP请求
- 移动设备:移动设备上运行了封装了uiautomator2的HTTP服务,解析收到的请求,并转化成uiautomator2的代码。
版本要求:
本案例以genymotion模拟器为例sdk api version 5.0 :
Requirements uiautomator2
- Android版本 4.4+
- Python 3.6+ (社区反馈3.8.0不支持, 但是3.8.2支持)
如果用python2的pip安装,会安装本库的老版本0.2.3;如果用python3.5的pip安装,会安装本库的老版本0.3.3;两者均已经不会再维护;PYPI上的最近版本是这个:https://pypi.org/project/uiautomator2/
一.环境搭建:
adb connect ip:port genymotion 默认端口5555,ip为安卓机器ip)这里我以wifi连接为案例:实际usb,wifi,自己决定
python -m uiautomator2 init
QUICK START
先准备一台(不要两台)开启了开发者选项
的安卓手机,连接上电脑,确保执行adb devices
可以看到连接上的设备。
运行pip3 install -U uiautomator2
安装uiautomator2
运行python3 -m uiautomator2 init
安装包含httprpc服务的apk到手机+atx-agent, minicap, minitouch
(在过去的版本中,这一步是必须执行的,但是从1.3.0之后的版本,当运行python代码u2.connect()
时就会自动推送这些文件了)
命令行运行python
打开python交互窗口。然后将下面的命令输入到窗口中。
这时看到类似下面的输出,就可以正式开始用我们这个库了。因为这个库功能太多,后面还有很多的内容,需要慢慢去看 ....
调试工具:weditor:
what is weditor ?:用来抓取手机上应用的控件,坐标以及元素属性id,text,description etc;
虽然很想用Android SDK内置工具uiautomatorviewer.bat
,但是运行uiautomator2的时候,uiautomatorviewer.bat
运行不起来,两者之间冲突太严重。
于是参考着uiautomatorviewer
的界面,我又写了一个weditor
,调用python-uiautomator2的两个接口screenshot
和dump_hierarchy
这样就不会有冲突问题了
Install weditor (UI Inspector)
因为uiautomator是独占资源,所以当atx运行的时候uiautomatorviewer是不能用的,为了减少atx频繁的启停,我们开发了基于浏览器技术的weditor UI查看器。https://github.com/openatx/weditor
安装方法(备注: 目前最新的稳定版为 0.1.0)
安装好之后,就可以在命令行运行weditor --help
确认是否安装成功了。
Windows系统可以使用命令在桌面创建一个快捷方式
weditor --shortcut
命令行直接输入python -m weditor
会自动打开浏览器,输入设备的ip或者序列号,点击Connect即可。
定位方式
- ResourceId定位:
d(resourceId="com.smartisanos.clock:id/text_stopwatch").click()
- Text定位
d(text="秒表").click()
- Description定位
d(description="..").click()
- ClassName定位
d(className="android.widget.TextView").click()
xpath定位并不支持,一开始打算做支持的,但是发现不用也能搞定。就是代码写的长一点而已。
操作控件
九宫格解锁:以前文章写过 https://testerhome.com/topics/11034
中文字符的输入
如果可以定位到元素,直接通过set_text就可以输入中文
如果定位不到元素需要使用send_keys
方法,以及切换输入法
截图:d.screenshot("home.jpg")
获取图层信息:xml = d.dump_hierarchy()
Connect to a device
There are two ways to connect to the device.
- Through WiFi
Suppose device IP is 10.0.0.1
and your PC is in the same network.
- Through USB
Suppose the device serial is 123456f
(seen from adb devices
)
- Through ADB WiFi
Calling u2.connect()
with no argument, uiautomator2
will obtain device IP from the environment variable ANDROID_DEVICE_IP
or ANDROID_SERIAL
. If this environment variable is empty, uiautomator will fall back to connect_usb
and you need to make sure that there is only one device connected to the computer.
Command line
其中的$device_ip
代表设备的ip地址
如需指定设备需要传入--serial
如 python3 -m uiautomator2 --serial bff1234 <SubCommand>
, SubCommand为子命令(init,或者screenshot等)
1.0.3 Added:
python3 -m uiautomator2
可以简写为uiautomator2
- screenshot: 截图
- current: 获取当前包名和activity
- uninstall: 卸载
- stop: 停止应用
- install: 安装apk,apk通过URL给出 (暂时不能用)
- healthcheck: 健康检查 (暂不能用)
还有太多太多的操作,我就不写了。更多更详细的API接口文档,直接去Github上看文档吧, 记得留下你的Starhttps://github.com/openatx/uiautomator2
文章最后送上一个网易云音乐的demo: