android 测试 Monkey 和 MonkeyRunner 的使用
一、Monkey的使用
Monkey使用起来比较简单,简而言之就是模拟手机点击效果,随机发送N个点击动作给手机,主要对于程序的稳定和承受压力的测试。
1.首先连接上你的手机或者启动模拟器;
2.运行CMD,进入命令输入框;
3.输入 adb shell monkey -p your.package -vvv 500 > e:\exception.txt (可能有朋友会问为什么不进入adb shell 之后才操作呢? 因为进入adb shell 中他没有可操作的权限了,也就不能创建exception.txt 文件,会报错。)
4.最后运行结束后可以在exception.txt 文档中查看你的运行结果。
解释:
-p 后面也就是app的包名,如果你是在测试你自己的app,尽量在manifest.xml中去复制,也可以adb shell 中 进入 data/data 用命令 ls 列出来,找出你需要的包。
-vvv 这个是输出三种运行的三种状态,就是详细输出事件等级,这个3个v就是输出等级1至3的所有事件。
至于500 则是随机发送500个点击事件给app点击使用。
存储到exception.txt是为了更好的查看测试结果。
其中也可以加上 --throttle 3000 每执行一次有效的事件后休眠3秒,如果又需要也可以添加上去。
二、MonkeyRunner的使用
这个是通用运行脚本文件来测试的,目的性强,更有针对性,更容易控制。
1.首先连接上你的手机或者启动一个模拟器;
2.运行CMD,进入你的sdk的tools文件夹下,在下面有一个monkeyrunner.bat;
3.然后就是创建你的脚本文件,里面有你写好的一系列的操作。(注意:这里你的脚本文件一定要是UTF-8文件格式,不然会出错。)
新建一个testmonkeyrunner.py
#导入我们需要用到的包和类并且起别名
import sys
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi #connect device 连接设备
#第一个参数为等待连接设备时间
#第二个参数为具体连接的设备
device = mr.waitForConnection(1.0,'ca985d92')
if not device:
print >> sys.stderr,"fail"
sys.exit(1)
#定义要启动的Activity
componentName='com.org.hl.john.monkeytest/.MainActivity'
#启动特定的Activity
device.startActivity(component=componentName)
mr.sleep(3.0)
#do someting 进行我们的操作
#输入 helloworld
device.type('helloworld')
#输入回车
device.press('KEYCODE_ENTER')
#return keyboard
#device.press('KEYCODE_BACK')
#------
#takeSnapshot截图
mr.sleep(3.0)
result = device.takeSnapshot() #save to file 保存到文件
result.writeToFile('./shot.png','png');
这里借用了一下其他朋友的代码,表示非常感谢!
在命令行输入
monkeyrunner testmonkeyrunner.py
就会根据你脚本里设置的包名和activity启动应用,然后根据脚本里的一系列的操作,跟着操作。
记录和回放
使用脚本,启用一个可视化操作的界面
新建一个(monkey_recorder.py)
#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder device = mr.waitForConnection()
recorder.start(device)
然后在命令行运行:
monkeyrunner monkey_recorder.py
就会弹出一个可视化的操作界面

然后可以在手机上进入你的app,如图:

跟着就可以在app里面做你想要测试的操作,必须是在电脑上这个界面上操作。
| Wait | 等待时间 |
| Press a Button | 发送,MENU,HOME,or SEARCH 按钮.Press,Down,or Up事件 |
| Type Something | 发送一些字符串 |
| Fling | 用来操作虚拟键盘![]() |
| Export Action | 将我们的脚本导出来 |
| Refresh Display | 刷新当前界面 |
当你操作完成后可以选择export action 导出我们的脚本,我保存的名称是action.mr,保存到tools目录下
导出后打开可以看到里面一系列的操作,但是这样是用不了的,还必须写到可运行的脚本里。
新建脚本:monkey_playback.py (也保存到tools目录下)
#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. import sys
from com.android.monkeyrunner import MonkeyRunner # The format of the file we are parsing is very carfeully constructed.
# Each line corresponds to a single command. The line is split into 2
# parts with a | character. Text to the left of the pipe denotes
# which command to run. The text to the right of the pipe is a python
# dictionary (it can be evaled into existence) that specifies the
# arguments for the command. In most cases, this directly maps to the
# keyword argument dictionary that could be passed to the underlying
# command. # Lookup table to map command strings to functions that implement that
# command.
CMD_MAP = {
'TOUCH': lambda dev, arg: dev.touch(**arg),
'DRAG': lambda dev, arg: dev.drag(**arg),
'PRESS': lambda dev, arg: dev.press(**arg),
'TYPE': lambda dev, arg: dev.type(**arg),
'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
} # Process a single file for the specified device.
def process_file(fp, device):
for line in fp:
(cmd, rest) = line.split('|')
try:
# Parse the pydict
rest = eval(rest)
except:
print 'unable to parse options'
continue if cmd not in CMD_MAP:
print 'unknown command: ' + cmd
continue CMD_MAP[cmd](device, rest) def main():
file = sys.argv[1]
fp = open(file, 'r') device = MonkeyRunner.waitForConnection() process_file(fp, device)
fp.close(); if __name__ == '__main__':
main()
写这个脚本还是必须先研究一下Phthon这个语言,楼主暂时还没有研究,所以暂时还不能解释上面的代码。。。。。
输入:
monkeyrunner play_black.py action.mr
运行即可,之前你操作的动作就重复实现了。
以上仅供参考!
--throttle 3000
android 测试 Monkey 和 MonkeyRunner 的使用的更多相关文章
- Android的Monkey和MonkeyRunner
本文部分解释性语段摘自网络百科或其它BLOG,语句内容网络随处可见,也不知道谁是初始原创,便不再署名出处,如有雷同,还请见谅. Monkey 什么是Monkey Monkey是Android中的一个命 ...
- Android测试-monkey
好久以前搞过monkey,最近看了一个monkey+日志录制的一个分享,准备自己也搞一下. monkey的doc文档: https://developer.android.google.cn/stud ...
- Android软件测试Monkey测试工具
前言: 最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.其 ...
- 【Android测试】【第九节】MonkeyRunner—— 初识
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4836815.html 不得不说两句,过了这么久才再次更新博 ...
- 【Android测试】【第七节】Monkey——源码浅谈
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4713466.html 前言 根据上一篇我们学会了Monke ...
- Android初体验之Monkey和MonkeyRunner
原文地址https://blog.csdn.net/mad1989/article/details/38087737 Monkey 什么是Monkey Monkey是Android中的一个命令行工具, ...
- monkey测试===Android测试工具Monkey用法简介(转载)
Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试.Monkey ...
- Monkey (压力测试)-移动端手机压力测试工具 monkey以及monkeyrunner
4. Monkey (压力测试) 这个是Android提供的系统工具.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试.Monkey测试是一种 ...
- Android APP压力测试-Monkey
压力测试-Monkey学习 Monkey测试特点 什么是Monkey test? 如其名,像猴子一样,虽然什么都不懂,但是可以乱点一通,可以理解为压力测试.在规定的时间或次数范围内做任何随机的操作,随 ...
随机推荐
- Ember.js 的视图层
本指导会详尽阐述 Ember.js 视图层的细节.为想成为熟练 Ember 开发者准备,且包 含了对于入门 Ember 不必要的细节. Ember.js 有一套复杂的用于创建.管理并渲染连接到浏览器 ...
- Tutorial - Deferred Rendering Shadow Mapping 转
http://www.codinglabs.net/tutorial_opengl_deferred_rendering_shadow_mapping.aspx Tutorial - Deferred ...
- [bzoj3192][JLOI2013]删除物品(树状数组)
3192: [JLOI2013]删除物品 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 872 Solved: 508[Submit][Status ...
- PDF二次开发_iStylePDF表单域的填充
wo讲到PDF表单,我们首先需要认识Adobe定义的PDF表单有哪些.以下是我从网上搜索到的简单介绍: PDF 表单简介 PDF 是可移植文档格式(Portable Document Format)的 ...
- OSChina码云试用
首先在码云申请账户. 从 http://git-scm.com/download 下载window版的客户端.下载好,一步一步安装即可. $git config --global user.name ...
- SSH邮箱验证与激活
下面是我写的email验证和激活: 自己瞎写的,能用,不喜欢勿喷 action中regist方法中代码 /** * * 发送邮件的方法 */ StringBuffer sb=new StringBuf ...
- caroufredsel 参数
caroufredsel 参数 参数列表:参数名 默认值 说明circular true 循环模式,true为无限循环,false为单轮循环.infinite ...
- Hibernate连数据库
1.建数据库,建表(一定要设主码) create database Hibernate create table Students( sno char(10) primary key, sna cha ...
- secureCRT远程登录工具的颜色配置(转载)
另外,字体和编码设置(如果需要显示中文):Options->Session Options->Appearance->font(字体:幼圆,字形:常规,大小:小三号,字符集:中文GB ...
- 如何解决子元素设了margin-top之后父元素所受的影响
解决方法: 1.在父元素上加:overflow:hidden. 2.给父元素加border; 3.外容器上加上padding.
