Uiautomator2命令
⼿势与设备交互
单击
d.click()
self.d(resourceId=“com.kwai.global.video.social.kwaigo:id/search_view”).click()
双击
d.double_click(x,y,0.1)
默认两次点击间隔0.1秒
长按
d.long_click(x,y,0.5)
默认长按0.5秒
滑动
d.swipe(sx, sy, ex, ey)
从sx, sy滑动到 ex, ey
拖动
d.drag(sx, sy, ex, ey)
从sx, sy拖动到 ex, ey
滑动点(多⽤于九宫格解锁)
d.swipe((X0,Y0),(X1,Y1),( x2,y2), 0.2)
从(X0,Y0)到点(X1,Y1),然后到点(X2,Y2)
中间间隔为0.2秒
触摸和拨动(不⽀持百分⽐位置定位)
time.sleep(0.01)#向下和移动之间的延迟,⾃⼰控制
备注:
除了触摸和拨动以外都⽀持百分⽐定位
d.long_click(0.5, 0.5) 表⽰长按屏幕中⼼
屏幕相关
检索/设置设备⽅向
可能的⽅向
natural or n#⾃然⽅向
left or l
right or r
upsidedown or u (can not be set)#倒置的⽅向
举例
d.set_orientation(“left”)
d.set_orientation(“n”)
冻结/解冻旋转
d.freeze_rotation()#冻结
freeze_rotation(False)#解冻
截图
⽀持安卓4.2以上系统
d.screenshot(“hom
e.jpg”)
获取PIL.Image格式化图像,需要安装pillow
image = d.screenshot() # default format=“pillow”
image.save(“home.jpg”)
获取opencv格式的图像,需要安装cv2
import cv2
image = d.screenshot(format=‘opencv’)
cv2.imwrite(‘home.jpg’, image)
获取原始jpeg数据
imagebin = d.screenshot(format=‘raw’)
open(“some.jpg”, “wb”).write(imagebin)
转储UI层次结构
xml = d.dump_hierarchy()#获取UI层次结构转储内容(unicoded)
打开通知或快速设置
d.open_notification()
d.open_quick_settings()
获取所选UI对象状态及其信息
判断ui对象是否存在
self.d(resourceId=“com.kwai.global.video.social.kwaigo:id/item_icon”).exists(timeout=3)返回true或false
timeout=3 如果元素没有出现就延迟3秒
获取ui对象的信息
d(text=“Settings”).info
输出全部信息(dict类型)
获取/设置/清除可编辑字段的⽂本
获取ui对象的值
get_text()
self.d(resourceId=“com.kwai.global.video.social.kwaigo:id/name”, instance=2).get_text()设置ui对象的值
d(text=“Settings”).set_text(“My text…”) # set the text
清除ui对象的值
d(text=“Settings”).clear_text() # clear the text
获取ui对象中⼼点
安卓unicode输入法
x, y = d(text=“Settings”).center()
对选定的UI对象执⾏点击操作
单击选定的ui对象
d(text=“Settings”).click()#单击ui对象的中⼼
d(text=“Settings”).click(timeout=10)#延迟10秒点击
offset是点击ui对象上的偏移位置
d(text=“Settings”).click(offset=(0.5, 0.5)) # 点击对象的中⼼
d(text=“Settings”).click(offset=(0, 0)) # 点击对象的左上⾓
d(text=“Settings”).click(offset=(1, 1)) # 点击对象的右下⾓
ui对象元素存在后点击
clicked = d(text=‘Skip’).click_exists(timeout=10.0)#延迟10秒
click_exists(10) #等待10秒
直到ui对象元素消失返回布尔值
is_gone = d(text=“Skip”).click_gone(maxretry=10, interval=1.0)
maxretry最⼤尝试次数,interval间隔时长
长按选定的ui对象
d(text=“settings”).long_click()#长按特定UI对象的中⼼
选定UI对象的⼿势操作
将ui对象拖向⼀个点或另⼀个ui对象
安卓4.3以下版本不⽀持
拖向⼀个点(X,Y)
d(text=“Settings”).drag_to(x, y, duration=0.5)
拖向另⼀个元素
d(text=“Settings”).drag_to(text=“Clock”, duration=0.25)
duration为时间限制
从UI对象的中⼼划向其边缘
滑动⽀持4个⽅向
left、right、top、bottom
d(text=“Settings”).swipe(“right”, steps=20)#0.1s滑到对象右边
seps步数,⼀步约为5ms,20步为0.1s
从⼀个点到另⼀个点的两点⼿势
d(text = “Settings”).gesture((sx1,sy1),(sx2,sy2),(ex1,ey1),(ex2,ey2))
选定ui对象的两点⼿势
⽀持两种
in:从边缘到中间
d.princh_in()
out:从中间到边缘
d.princh_out()
安卓4.3以下不⽀持
d(text=“Settings”).pinch_in(percent=100, steps=10)
等待选定ui对象出现或消失
d(text=“Settings”).wait(timeout=3.0)
d(text=“Settings”).wait_gone(timeout=1.0)
返回布尔值
默认超时20s
在选定ui对象上执⾏fling(可滚动)
可能的属性
horiz or vert#⽔平或垂直
forward or backward or toBeginning or toEnd#向前、向后、开始、结尾
举例
d(scrollable=True).fling()#默认垂直向前
d(scrollable=True).fling.horiz.forward()#⽔平向前滚动
d(scrollable=True).Beginning(max_swipes=1000)#垂直滚动直到开始
d(scrollable=True).End()#垂直(默认)滚动到最后
在选定对象上执⾏scroll(可滚动)
可能的属性
horiz or vert#⽔平或垂直
forward or backward or toBeginning or toEnd or to#向前、向后、开始、结尾、直到…举例
d(scrollable=True).scroll(steps=10)#默认垂直向前
d(scrollable=True).scroll.horiz.forward(steps=100)
d(scrollable=True).scroll.vert.backward()
d(scrollable=True).Beginning(steps=100, max_swipes=1000)
d(scrollable=True).End()
d(scrollable=True).(text=“Security”)#垂直向前滚动,直到出现特定的ui对象
其他
输⼊字符
self.d.send_keys(“你好123abcEFG ”)
等待
import time
time.sleep(10) #等待10s