您的位置:首页 > 其它

monkeyrunner脚本的编写、运行和常用api简介

2016-12-17 18:43 344 查看
一、准备工作:

1、Android SDK ,可以到官网下载,其中我们运行monkeyrunner脚本需要到一个monkeyrunner.bat就包含其中



2、一个python编译环境,并配置好环境变量,可以自行查找相关资料

以上两步成功之后可以运行monkeyrunner脚本的环境已经准备好了

二、我们需要确认我们adb连接上了电脑

adb 使用的方法自行学习

三、脚本得编写

我们需要创建一个后缀为py的文件

1、导入模块

import sys

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage

2、连接当前设备,并返回一个MonkeyDevice的对象

device=MonkeyRunner.waitForConnection(10,'U8YHT859GUZHJBP7')



#其中第一个参数10是指等待连接上设备的时间,单位为S;第二个参数是设备得ID

#获取设备id的方法:在dos窗口输入adb devces





3、实例部分

(1)唤醒设备(即亮屏的意思)

device.wake()

(2)模拟拖拽

用法:MonkeyDevice.drag(start,end,duration,steps)

start:拖曳开始坐标 - The starting point for the drag (a tuple (x,y) in pixels)

end:拖曳结束坐标点- The end point for the drag (a tuple (x,y) in pixels

duration:持续时间 - Duration of the drag in seconds (default is 1.0 seconds)

steps:拖曳步骤- The number of steps to take when interpolating points. (default is 10)


不过我们步骤2已经获得了MonkeyDevice对象可以直接下面这样用

其中里面得参数根据上面得介绍来使用,根据自己手机的分辨率来定

device.drag((1,500),(400,1),3,10)




用法:MonkeyDevice.drag(start,end,duration,steps)

start:拖曳开始坐标 - The starting point for the drag (a tuple (x,y) in pixels)

end:拖曳结束坐标点- The end point for the drag (a tuple (x,y) in pixels

duration:持续时间 - Duration of the drag in seconds (default is 1.0 seconds)

steps:拖曳步骤- The number of steps to take when interpolating points. (default is 10)




(3)点击屏幕

device.touch(122,122,"DOWN_AND_UP") #device为MonkeyDevice的对象

(4)执行adb shell 命令

device.shell("am start am - com.xxx.xxx")

(5)延时(秒)

MonkeyRunner.sleep(xxxs)

(6)按键指令,如返回键



device.press('KEYCODE_BACK','DOWN_AND_UP')


下面部分键值名称,可以自己进行测试验证
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息