點擊控件的具體位置
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") # 模擬輸入法的搜索
uiautomation2哈哈哈哈
