monkeyrunner脚本的编写、运行和常用api简介
2016-12-17 18:43
411 查看
一、准备工作:
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')
下面部分键值名称,可以自己进行测试验证
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')
下面部分键值名称,可以自己进行测试验证
相关文章推荐
- Android Manifest 用法
- 什么是 GraphQL?
- Spark RDD API详解(一) Map和Reduce
- Spring Boot 开发微服务
- lwn拾遗:[sn3218 led drivers]-api解释-1
- 页面元素查找之Selectors API
- 一个小型js框架myJSFrame附API使用帮助
- 详细分析交换机、路由器、集线器的区别和联系
- PowerShell打开或关闭光驱
- 批处理的api WMIC学习体会有感第1/2页
- 批处理 API实现文件下载的代码第1/2页
- Lua教程(十七):C API简介
- 强制删除工具 xdelbox xdelbox1.5正式版下载
- 揪出交换机端口背后“凶手”导致网速太慢
- 电脑重启后突然检测不到硬盘的原因分析与解决办法
- C#中设计、使用Fluent API
- Google官方支持的NodeJS访问API,提供后台登录授权
- sea.js常用的api简易文档
- PQ分区出错! 巧用Ghost急速补救的绝妙办法
- monkeyrunner之安卓开发环境搭建教程(1)