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秒
    触摸和拨动(不支持百分比位置定位)
    d.touch.down(10,10)#模拟按下
    time.sleep(0.01)#向下和移动之间的延迟,自己控制
    d.touch.move(15,15)#模拟移动
    d.touch.up ()#模拟抬起
    备注:
    除了触摸和拨动以外都支持百分比定位
    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(“home.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对象是否存在
    d.exists()
    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对象中心点
    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).fling.vert.toBeginning(max_swipes=1000)#垂直滚动直到开始
        d(scrollable=True).fling.toEnd()#垂直(默认)滚动到最后
    在选定对象上执行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).scroll.horiz.toBeginning(steps=100, max_swipes=1000)
        d(scrollable=True).scroll.toEnd()
        d(scrollable=True).scroll.to(text=“Security”)#垂直向前滚动,直到出现特定的ui对象

其他

    输入字符
    self.d.send_keys(“你好123abcEFG ”)
    等待
    import time
    time.sleep(10) #等待10s
————————————————
版权声明:本文为CSDN博主「夏丹莹」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiadanying/article/details/90720343

posted @ 2020-03-10 19:05  谢谢再见  阅读(530)  评论(0编辑  收藏  举报