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? 如其名,像猴子一样,虽然什么都不懂,但是可以乱点一通,可以理解为压力测试.在规定的时间或次数范围内做任何随机的操作,随 ...
随机推荐
- MVC架构 使用FastReport
1.Web.config文件 添加配置 <httpHandlers> <add path="FastReport.Export.axd" verb="* ...
- log4jWARN Please initialize the log4j system properly解决办法
原因是没有对log4j这个jar进行文件配置. 要解决这个问题非常简单,建立LOG4J 的配置文件即可.在src 目录下创建配置文件,选择菜单File > New > File,文件名输入 ...
- 解决hibernate删除时的异常
由于关联关系是一对多和多对一的关系,于是在代码中需要删除多的一方的对象时出现了 deleted object would be re-saved by cascade (remove deleted ...
- File system needs to be upgraded. You have version null and I want version 7
安装hbase时候报错: File system needs to be upgraded. You have version null and I want version 7 注: 我安装的hba ...
- mysql复制一列到另一列
mysql复制一列到另一列 UPDATE 表名 SET B列名=A列名 需求:把一个表某个字段内容复制到另一张表的某个字段. 实现sql语句1: 复制代码代码如下: UPDATE file_man ...
- 搭建ubuntu14.04的hadoop集群【docker容器充当服务器】
首先弄出来装有hadoop.java.ssh.vim的镜像起名badboyf/hadoop.做镜像有两种方法,一种是用Dockerfile来生成一个镜像,一种是基于ubuntu14.04的基础镜像生成 ...
- Sprint1(第二天11.15)
Sprint1(第二天11.15) Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:web版-餐厅到店点餐系统 4.团队博客地址: http:// ...
- eclipse svn提交报错
修改文件格式:右键-properties-text file encoding-other UTF-8
- Unity 3D 正交相机(Orthographic)
1. Camera.aspect 表示摄像机显示区域的纵横比.宽高比,摄像机初始化的时候会默认设置成当前屏幕的宽高比,可以更改,也可以通过 Camera.ResetAspect 来重置. 2. Cam ...
- YY前端课程3
1. 常用的字符实体(html实体):空格= <=< >=> 版权符号=© 2. ID就像身份证号一样,是唯一的,html页面的ID不能重复: ...
