简洁而又强大的库-uiautomator2
uiautomator2是一个可以使用 Python 对 Android 设备进行 UI 自动化的库。其底层基于Google uiautomator,Google 提供的uiautomator库可以获取屏幕上任意一个 APP 的任意一个控件属性,并对其进行任意操作,目前仅支持 android 平台的原生应用测试。
uiautomator2有如下特点:
功能丰富 :设备和开发机可以脱离数据线,可通过 WiFi 互联;
得心应手 :集成了 openstf/minicap 加快截图速度 , 集成了 openstf/minitouch 达到精确实时控制设备 , 修复了 xiaocong/uiautomator 经常性退出的问题;
深度整合 :代码进行了重构和精简,方便维护;
安装
pip install --pre -U uiautomator2设备管理
atx-agent配置
uiautomator2 是通过atx-agent驱动手机的,因此需要将其安装在手机上
手机连接pc,输入adb命令adb devices发现设备后表明设备已连接成功
在终端输入如下初始化命令,以安装atx-agent至手机:
python -m uiautomator2 init设备连接
在进行通过adb devices获取设备的UUID,然后输进参数即可
import uiautomator2 as u2
d = u2.connect('123456')模拟按键
d.press("home")支持的按键分别为:
截图
d.screenshot('test.png')录屏
进行录屏之前,需要安装如下库:
pip install -U "uiautomator2[image]" -i https://pypi.doubanio.com/simple开始录屏
d.screenrecord('./test.mp4')结束录屏
d.screenrecord.stop()执行bash命令
d.bash("dumpsys wifi | grep mWifiInfo") # 检查wifi网络状态
d.bash("svc wifi disable") # 关闭wifi
d.bash("svc wifi enable") # 开启wifi
d.bash("svc data disable") # 关闭数据网络
d.bash("svc data enable") # 开启数据网络输入法切换
# 切换成ui2的输入法,这里会隐藏掉系统原本的输入法,默认是使用系统输入法
# 当传入False时会使用系统默认输入法,默认为Fasle
d.set_fastinput_ime(True)
# 查看当前输入法
d.current_ime()
#返回值
('com.github.uiautomator/.FastInputIME', True)运行应用
d.app_start("com.xueqiu.android")停止应用
d.app_stop("com.xueqiu.android")app操作
元素定位
可以通过设备的d对象,传入元素的属性来直接定位,元素的属性使用weditor或uiautodev来进行决定(更推荐uiautodev,weditor原作者已弃用)
d(text="Settings") # 通过text定位
d(resourceId="com.example:id/btn") # 通过resourceId定位
d(className="android.widget.Button") # 通过className定位
d(text="确定", className="Button", clickable=True) # 组合定位ui2支持 android 中 UiSelector 类中的所有定位方式可以进该网站查询:UiSelector | Android Developers (google.cn),列举如下:
xpath定位
uiautomator2通过如下方法进行xpath定位:
d.xpath("//*[contains(@text, '设置')]")点击
分长按和点击两种,如下:
# 通过text点击
d(text="Settings").click()
# 长按有Settings文本的元素
d(text="Settings").long_click()
# 点击坐标(相对坐标,适应不同分辨率)
w,h = d1.window_size()
d.click(0.5*w, 0.5*h)滑动
w,h = d.window_size()
d.swipe(0.1*w,0.5*h,0.8*w,0.5*h) # 从(0.1,0.5)滑动到(0.8,0.5)
d.swipe_ext("up", 0.5) # 向上滑动0.5,可以为up、down、left和right拖拽
# 从sx,sy坐标拖拽至ex,ey坐标
d.drag(sx, sy, ex, ey)滚动
# 垂直滚动到页面顶部/横向滚动到最左侧
d(scrollable=True).scroll.toBeginning()
d(scrollable=True).scroll.horiz.toBeginning()
# 垂直滚动到页面最底部/横向滚动到最右侧
d(scrollable=True).scroll.toEnd()
d(scrollable=True).scroll.horiz.toEnd()
# 垂直向后滚动到指定位置/横向向右滚动到指定位置
d(scrollable=True).scroll.to(description="指定位置")
d(scrollable=True).scroll.horiz.to(description="指定位置")
# 垂直向前滚动(横向同理)
d(scrollable=True).scroll.forward()
# 垂直向前滚动到指定位置(横向同理)
d(scrollable=True).scroll.forward.to(description="指定位置")获取元素文本
d.xpath('//*[@resource-id="subject"]/android.widget.EditText[1]').get().text
d(resourceId="com.netease.mobimail:id/fab_compose").get_text()元素等待
# 等待元素的出现
d(text="Settings").wait(timeout=10.0)
# 等待元素消失,返回True或者False,timout默认为全局设置的等待时间
d(text='Settings').wait_gone(timeout=20)发送文本
d(text="Settings").set_text("你好")
d.send_keys("你好", clear=True) # 清除文本框后输入你好弹窗处理
点击确认按钮
d.alert.accept()点击取消按钮
d.alert.dismiss()获取弹窗的文本
text = d.alert.text
print(text)判断弹窗是否存在
if(d.alert.exists):
pass与Airtest合作
因为uiautomator2的图像识别比Airtest差,可以用Airtest弥补这个缺陷,Airtest与uiautomator2并未冲突,这两个框架可以同时启动。
from airtest.core.api import *
import uiautomator2 as u2
dev = connect_device("Android://127.0.0.1:5037/xxxxx")
d = u2.connect('xxxxx')
d.app_start("com.taobao.taobao")
touch(Template(r"taobao.png"))