uiautomator2进阶
uiautomator2进阶
点击控件的具体位置
d(text="Settings").click(offset=(0.5, 0.5)) # 点击控件的中间位置 d(text="Settings").click(offset=(0, 0)) # 点击控件的左上方 d(text="Settings").click(offset=(1, 1)) # 点击控件的右下方
拖动控件
d(text="Settings").swipe("right") d(text="Settings").swipe("left", steps=10) d(text="Settings").swipe("up", steps=20) d(text="Settings").swipe("down", steps=20) # 1 steps is about 5ms, so 20 steps is about 0.1s
特定UI对象上的两点手势:从边缘到中心向外,从中心到边缘
# 从边缘到中心 d(text="Settings").pinch_in(percent=100, steps=10) # 从中心到边缘 d(text="Settings").pinch_out()
等待特定的界面出现或者消失
# 等待UI出现 d(text="Settings").wait(timeout=3.0) # return bool # 等待UI消失 d(text="Settings").wait_gone(timeout=1.0)
对特定的UI对象执行Fling(可滚动)默认时间20s
# 向前(默认) d(scrollable=True).fling() # 掷球前水平 d(scrollable=True).fling.horiz.forward() # 垂直向后投掷 d(scrollable=True).fling.vert.backward() # 水平开始 d(scrollable=True).fling.horiz.toBeginning(max_swipes=1000) # 垂直抛向末端 d(scrollable=True).fling.toEnd()
对特定的UI对象执行滚动
水平或垂直
向前或向后,或开始或结束,
# 垂直向前滚动(默认) 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() # 垂直向前滚动,直到出现特定的UI对象 d(scrollable=True).scroll.to(text="Security")
当选择器找不到匹配项时,uiautomator2将运行所有注册的观察程序,条件匹配时单击目标
d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait") \ .click(text="Force Close") #使用不带参数的click d.watcher("ALERT").when(text="OK").click() #same as d.watcher("ALERT").when(text="OK").click(text="OK") #当条件为真时按键 d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait") \ .press("back", "home") #检查指定的观察程序是否触发一个观察者被触发,这意味着观察者已经运行并且其所有条件都匹配。 # true in case of the specified watcher triggered, else false d.watcher("watcher_name").triggered #展示所有watch d.watchers #通常可以用来自动点击权限确认框,或者自动安装 d.watcher("OK").when(text="OK").click(text="OK") # enable auto trigger watchers d.watchers.watched = True # disable auto trigger watchers d.watchers.watched = False # get current trigger watchers status assert d.watchers.watched == False #移除watch # remove all registered watchers d.watchers.remove() # remove the named watcher, same as d.watcher("watcher_name").remove() d.watchers.remove("watcher_name")
全局设置
#用户每次点击后延迟1.5s d.click_post_delay = 1.5 #设置默认元素超时时间(s) d.wait_timeout = 30.0
#输入法
d.set_fastinput_ime(True) # 切换成FastInputIME输入法 d.send_keys("你好123abcEFG") # adb广播输入 d.clear_text() # 清除输入框所有内容(Require android-uiautomator.apk version >= 1.0.7) d.set_fastinput_ime(False) # 切换成正常的输入法 d.send_action("search") # 模拟输入法的搜索
来源:https://www.cnblogs.com/zhujunsheng/p/10452073.html

浙公网安备 33010602011771号