继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

慕码人8056858
关注TA
已关注
手记 1293
粉丝 351
获赞 1326

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 stepsd.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 stepsd.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.orientationprint 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, instanced(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)



作者:Godric_wsw
链接:https://www.jianshu.com/p/874e5ceb72bb


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP