1.初始化
from uiautomator import device as d
from uiautomator import Device
d=Device('014E05DE0F02000E')
2.按鍵操作
KEYS = ["home","back","left",'right',"up","down","center","menu","search","enter","delete","recent","volume_up", "volume_down",
"volume_mute","camera","power"]
for key in KEYS:
print key
d.press(key)
time.sleep(1)
3.坐標操作
#點屏
d.click(2023, 1256)
# #長按屏幕
# long click (x, y) on screen
d.long_click(201, 0235)
4.滑動
# swipe from (sx, sy) to (ex, ey)
d.swipe(sx, sy, ex, ey)
# # swipe from (sx, sy) to (ex, ey) with 10 steps
d.swipe(sx, sy, ex, ey, steps=10)
5.拖動
# drag from (sx, sy) to (ex, ey)
d.drag(sx, sy, ex, ey)
# drag from (sx, sy) to (ex, ey) with 10 steps
d.drag(sx, sy, ex, ey, steps=10)
6.屏幕
print d.info
# 打開屏幕
d.screen.on()
# 關閉屏幕
d.screen.off()
# # natural 或者 n 代替
# # left 或者 l 代替
# # right 或者 r 代替
# # upsidedown或 u(不能設定)
# #獲取orientation(方向),可能是上述中的任意一種
orientation = d.orientation
print orientation
# #設置定向和凍結旋轉井厌。
# #說明:"upsidedown"不能用于Android 4.3 以前的版本
d.orientation="l"
d.orientation="r"
d.orientation="n"
# 打開通知消息欄稻扬,不能用于Android 4.3以前的版本
d.notification()
# 打開快速設置欄踪旷,不能用于Android 4.3以前的版本
d.open.quick_settings()
# 等待當前窗口空閑
d.wait.idle()
# 等待直到窗口發(fā)生刷新事件
d.wait.update()
7.監(jiān)視器
#當一個選擇器找不到匹配時,uiautomator 會運行全部已經注冊的觀察者
#條件匹配時點擊目標
d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait").click(text="Force Close")
#條件匹配時按下按鍵
d.watcher("AUTO_FC_WHEN_ANR").when(text="ANR").when(text="Wait").press("back", "home")
8.選擇器
#選擇器支持以下參數
# text,textContains吨枉,textMatches矮锈,textStartsWith
# className, classNameMatches
# description咖为,descriptionContains秕狰,descriptionMatches,descriptionStartsWith
# checkable躁染,checked鸣哀,clickable,longClickable
# scrollable吞彤,enabled我衬,focusable,focused饰恕,selected
# packageName挠羔, packageNameMatches
# resourceId, resourceIdMatches
# index埋嵌, instance
d(text="Settings").clear_text() # clear the text(清除文本信息)
d(text="Settings").set_text("My text...") # set the text(設置文本信息)
d(text="Settings").click()
d(text="Settings").long_click()
#d(text="Settings").drag.to(x, y, steps=100)
d(text="Settings").drag.to(text="Clock", steps=50)
d(text="Settings").swipe.right()
d(text="Settings").swipe.left(steps=10)
d(text="Settings").swipe.up(steps=10)
d(text="Settings").swipe.down()
# d(text="Settings").gesture((sx1, sy1), (sx2, sy2)).to((ex1, ey1), (ex2, ey2))
# d().gestureM((sx1, sy1), (sx2, sy2),(sx3, sy3)).to((ex1, ey1), (ex2, ey2),(ex3,ey3))
d(text="Settings").wait.exists(timeout=3000)
d(text="Settings").exists(timeout=3000)
d(text="Settings").wait.gone(timeout=1000)