python自动化测试(一)--uiautomator总结

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()
# 等待直到窗口发生刷新事件
d.wait.update()

7.监视器

#当一个选择器找不到匹配时,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)

你可能感兴趣的:(python自动化测试(一)--uiautomator总结)