代码改变世界

史上最全的MonkeyRunner自动化测试从入门到精通(1)

2018-04-16 03:14  清风软件测试开发  阅读(688)  评论(0)    收藏  举报

原文地址https://zhuanlan.zhihu.com/p/26043620

MonkeyRunner使用

#-*- coding:utf-8 –*-
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
#连接设备
device=MonkeyRunner.waitForConnection(3,"172.10.10.124:5555")
#启动App
device.startActivity("包名/类名")
#暂停两秒
MonkeyRunner.sleep(2)
#点击搜索框
device.touch(100,100,"DOWN_AND_UP")
#输入查询词
device.type('test')
#点击回车键
device.press('KEYCODE_ENTER','DOWN_AND_UP')
#点击搜索按钮
device.touch(400,100,"DOWN_AND_UP")
#截图
image=device.takeSnapshot()
image.writeToFile('./test.png','png')
device.touch(300,100,"DOWN_AND_UP")
#弹出框
MonkeyRunner.alert('test over','This is title','OK')

备注:
1坐标值可通过 sdk自带的uiautomatorviewer获取
2运行方式
 在tool目录下执行monkeyrunner file.py