前言:
最近开始研究Android自动化测试方法,对其中的一些工具、方法和框架做了一些简单的整理,其中包括android测试框架、CTS、Monkey、Monkeyrunner、benchmark、其它test tool等等。因接触时间很短,很多地方有不足之处,希望能和大家多多交流。
一、 什么是Monkey
Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流(如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法。
二、 Monkey的特征
1、测试的对象仅为应用程序包,有一定的局限性。
2、Monky测试使用的事件流数据流是随机的,不能进行自定义。
3、可对MonkeyTest的对象,事件数量,类型,频率等进行设置。
三、Monkey的基本用法
基本语法如下:
$ adb shell monkey [options]
如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:
$ adb shell monkey -p your.package.name -v 500
四、Monkey测试的一个实例
通过这个实例,我们能理解Monkey测试的步骤以及如何知道哪些应用程序能够用Monkey进行测试。
Windows下(注:2—4步是为了查看我们可以测试哪些应用程序包,可省略):
1、 通过eclipse启动一个Android的emulator
2、 在命令行中输入:adb devices查看设备连接情况
C:\Documents and Settings\Administrator>adbdevices
List of devices attached
emulator-5554 device
3、 在有设备连接的前提下,在命令行中输入:adb shell 进入shell界面
C:\Documents and Settings\Administrator>adb shell
#
4、 查看data/data文件夹下的应用程序包。注:我们能测试的应用程序包都在这个目录下面
C:\Documents and Settings\Administrator>adb shell
# ls data/data
ls data/data
com.google.android.btrouter
com.android.providers.telephony
com.android.mms
com.android.providers.downloads
com.android.deskclock
com.android.email
com.android.providers.media
5、 以com.android.calculator2作为对象进行MonkeyTest
#monkey-p com.android.calculator2 -v 500
其中-p表示对象包 –v 表示反馈信息级别
monkey 的使用
Monkey的智力就是一个三岁小孩的水平,所以,使用起来也是非常简单,当然,也做不了什么复杂的东西
1
|
adb shell monkey [options] <event-count>
|
options | event-count |
这个是配置monkey的设置,例如,指定启动那个包,不指定将会随机启动所有程序 | 这个是让monkey发送多少次事件 |
------------------------------以下为折腾星人研究参考资料------------------------------
然后,补充一下官方文档并没有更新的参数…不信你自己对照着来看,如果,你看到时候,官方更新了我说的这个,希望能回复我一下,让我更新一下…
主要多了两个参数:
[--port port] | 指定monkey的端口,实现使用Client-server Monkey,这个参数的作用就是让你可以通过telnet来手动设置monkey的参数,就是让monkey的智力进化到能听懂能的话…可惜的是我一直都没搞成功. |
[--setup scriptfile] [-f scriptfile ….] | 这个是让monkey 指定手机中的脚本运行. |
这里我贴一个,国外有人用本地指定scriptfile成功的使用
I am trying to do 2 things with monkey
1. Execute a script with a command like
adb shell monkey -p MY_PACKAGE --setup scriptfile -f /sdcard/ mon_script1.txt 1
where mon_script.txt contains a few touch commands. After I execute this, I see nothing happening on the screen. It even does not give me the "Number of events injected message". I have verified that my touch co-ordinates fall over actual UI elements.
This is the script file I am using
tap 79 29 tap 100 100 tap 200 200 tap 300 300 quit
2. Execute Monkey Network control to type commands individually. I start up monkey to listen to a port and use PuTTY to send commands. I get "OK" return messages, but nothing happens on the screen.
Whenever I use monkey in the random mode, I see interaction on the screen. But I need to get one of the above 2 methods to work. I have seen the sources of monkey and nothing seems to be wrong. Has anyone used monkey in the above described way? If so, please tell me what I am doing wrong.
使用远程monkey 的代码模板
1
|
adb –e shell monkey -p your.pakagename --port 1080 &
|
然后重定向我们的模拟器端口
1
|
adb -e forward tcp 1080 tcp 1080
|
然后telnet 的我们的模拟器
1
|
telnet localhost 1080
|
接着telnet成功以后据说可以这样
tap 150 200
----以上代码来源于Android application Test Guide 书中
运行过程中,Emulator中的应用程序在不断地切换画面。
按照选定的不同级别的反馈信息,在Monkey中还可以看到其执行过程报告和生成的事件。
注:具体参数的设定可参考:
http://developer.android.com/guide/developing/tools/monkey.html
五、关于Monkey测试的停止条件
Monkey Test执行过程中在下列三种情况下会自动停止:
1、如果限定了Monkey运行在一个或几个特定的包上,那么它会监测试图转到其它包的操作,并对其进行阻止。
2、如果应用程序崩溃或接收到任何失控异常,Monkey将停止并报错。
3、如果应用程序产生了应用程序不响应(application not responding)的错误,Monkey将会停止并报错。
通过多次并且不同设定下的Monkey测试才算它是一个稳定性足够的程序。