- Android 测试工具Monkey & Monkeyrunner 使用方法
-
程序编写完成后进行测试一定是必不可少的,Android Sdk给我们提供了Monkey和Monkeyrunner这两个自动化测试工具。时光境迁,随着版本的不断更新迭代,Monkey和Monkeyrunner也在不断的改变,改变的速度甚至已经到了官方文档有时都跟不上了,这无疑给我们的学习带来很多困扰。
· 基本配置选项,如设置尝试的事件数量。
· 运行约束选项,如设置只对单独的一个包进行测试。
· 事件类型和频率。
· 调试选项。
$ adb shell monkey [options] <event-count>
如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:
$ adb shell monkey -p your.package.name -v 500
一些常用的参数信息:
-v
命令行的每一个-v将增加反馈信息的级别。Level 0(缺省值)除启动提示、测试完成和最终结果之外,提供较少信息。Level 1提供较为详细的测试信息,如逐个发送到Activity的事件。Level 2提供更加详细的设置信息,如测试中被选中的或未被选中的Activity。事件
-s <seed>
伪随机数生成器的seed值。如果用相同的seed值再次运行Monkey,它将生成相同的事件序列。约束限制
-p <allowed-package-name>
如果用此参数指定了一个或几个包,Monkey将只允许系统启动这些包里的Activity。如果你的应用程序还需要访问其它包里的Activity(如选择取一个联系人),那些包也需要在此同时指定。如果不指定任何包,Monkey将允许系统启动全部包里的Activity。要指定多个包,需要使用多个 -p选项,每个-p选项只能用于一个包。调试
--dbg-no-events
设置此选项,Monkey将执行初始启动,进入到一个测试Activity,然后不会再进一步生成事件。为了得到**结果,把它与-v、一个或几个包约 束、以及一个保持Monkey运行30秒或更长时间的非零值联合起来,从而提供一个环境,可以监视应用程序所调用的包之间的转换。--wait-dbg
停止执行中的Monkey,直到有调试器和它相连接。-v
命令行的每一个-v将增加反馈信息的级别。Level 0(缺省值)除启动提示、测试完成和最终结果之外,提供较少信息。Level 1提供较为详细的测试信息,如逐个发送到Activity的事件。Level 2提供更加详细的设置信息,如测试中被选中的或未被选中的Activity。事件
-s <seed>
伪随机数生成器的seed值。如果用相同的seed值再次运行Monkey,它将生成相同的事件序列。约束限制
-p <allowed-package-name>
如果用此参数指定了一个或几个包,Monkey将只允许系统启动这些包里的Activity。如果你的应用程序还需要访问其它包里的Activity(如选择取一个联系人),那些包也需要在此同时指定。如果不指定任何包,Monkey将允许系统启动全部包里的Activity。要指定多个包,需要使用多个 -p选项,每个-p选项只能用于一个包。调试
--dbg-no-events
设置此选项,Monkey将执行初始启动,进入到一个测试Activity,然后不会再进一步生成事件。为了得到**结果,把它与-v、一个或几个包约 束、以及一个保持Monkey运行30秒或更长时间的非零值联合起来,从而提供一个环境,可以监视应用程序所调用的包之间的转换。
三、Monkey实例测试
Windows下(注:2—4步是为了查看我们可以测试哪些应用程序包,可省略):1、 通过eclipse启动一个Android的emulator
2、 在命令行中输入:adb devices查看设备连接情况
emulator-5554 device
3、 在有设备连接的前提下,在命令行中输入:adb shell 进入shell界面
#
4、 查看data/data文件夹下的应用程序包。注:我们能测试的应用程序包都在这个目录下面
com.android.htmlviewer
com.android.settings
com.android.netspeed
com.android.providers.userdictionary
com.android.browser
com.android.contacts
com.android.alarmclock#monkey -p com.marshalchen.MonkeyTest1 -v 500
其中-p表示对象包 –v 表示事件数量

运行过程中,Emulator中的应用程序在不断地切换画面。
按照选定的不同级别的反馈信息,在Monkey中还可以看到其执行过程报告和生成的事件。
如:Sending Pointer ACTION_DOWN x=35.0 y=259.0
:Sending Pointer ACTION_UP x=195.0 y=259.0
:Sending Pointer ACTION_DOWN x=295.0 y=223.0
:Dropped: keys=0 pointers=0 trackballs=0 flips=0
等等信息。
四、Monkey停止的条件
Monkey Test执行过程中在下列三种情况下会自动停止:1、如果限定了Monkey运行在一个或几个特定的包上,那么它会监测试图转到其它包的操作,并对其进行阻止。
2、如果应用程序崩溃或接收到任何失控异常,Monkey将停止并报错。
3、如果应用程序产生了应用程序不响应(application not responding)的错误,Monkey将会停止并报错。
通过多次并且不同设定下的Monkey测试才算它是一个稳定性足够的程序。
MonkeyRunner 使用
Monkey的使用简洁有效,但是总是给人感觉功能不够强大。所以,接下来向大家介绍非常给力的工具MonkeyRunner。
一、什么是MonkeyRunner
monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。monkeyrunner工具与monkey工具并无关联。monkey工具直接运行在设备或模拟器的adbshell中,生成用户或系统的伪随机事件流。而monkeyrunner工具则是在工作站上通过API定义的特定命令和事件控制设备或模拟器。
monkeyrunner工具为Android测试提供了以下特性:
1.多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。
2.功能测试: monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。
3.回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。
4.可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner API之外,您还可以使用标准的Python os和subpress模块来调用如adb这样的Android工具。
您还可以向monkeyrunner API中添加您自己的类。
monkeyrunner工具使用Jython(使用Java编程语言的一种Python实现)。Jython允许monkeyrunnerAPI与Android框架轻松的进行交互。使用Jython,您可以使用Python语法来获取API中的常量、类以及方法。monkeyrunner的命令语法为:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
monkeyrunnerbasic.py:
[python]
# 导入此程序所需的monkeyrunner模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# 连接当前设备,返回一个MonkeyDevice对象
device = MonkeyRunner.waitForConnection()
# 安装Android包,注意,此方法返回的返回值为boolean,由此您可以判断安装过程是否正常
device.installPackage('myproject/bin/MyApplication.apk')
# 运行此应用中的一个活动device.startActivity(component='com.marshalchen.Monkeyrunner.MainActivity')
# 按下菜单按键
device.press('KEYCODE_MENU','DOWN_AND_UP')
# 截取屏幕截图
result = device.takeSnapShot
# 将截图保存至文件
result.writeToFile('myproject/shot1.png','png')# 导入此程序所需的monkeyrunner模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# 连接当前设备,返回一个MonkeyDevice对象
device = MonkeyRunner.waitForConnection()
# 安装Android包,注意,此方法返回的返回值为boolean,由此您可以判断安装过程是否正常
device.installPackage('myproject/bin/MyApplication.apk')
# 运行此应用中的一个活动device.startActivity(component='com.marshalchen.Monkeyrunner.MainActivity')
# 按下菜单按键
device.press('KEYCODE_MENU','DOWN_AND_UP')
# 截取屏幕截图
result = device.takeSnapShot
# 将截图保存至文件
result.writeToFile('myproject/shot1.png','png')
然后转到$SDK\TOOLS下执行 monkeyrunner monkeyrunnerbasic.py 就可执行测试了。
[python]
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,'emulator-5554')
if not device:
print >> sys.stderr,"fail"
sys.exit(1)
#定义要启动的Activity
componentName='kg.monkey/.MonkeyActivity'
#启动特定的Activity
device.startActivity(component=componentName)
mr.sleep(3.0)
#do someting 进行我们的操作
#输入 a s d
device.type('asd')
#输入回车
device.press('KEYCODE_ENTER')
#return keyboard 点击返回用于取消等下看到截图的下方的白条
#device.press('KEYCODE_BACK')
#------
#takeSnapshot截图
mr.sleep(3.0)
result = device.takeSnapshot()
#save to file 保存到文件
result.writeToFile('takeSnapshot\\result1.png','png');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,'emulator-5554')
if not device:
print >> sys.stderr,"fail"
sys.exit(1)
#定义要启动的Activity
componentName='kg.monkey/.MonkeyActivity'
#启动特定的Activity
device.startActivity(component=componentName)
mr.sleep(3.0)
#do someting 进行我们的操作
#输入 a s d
device.type('asd')
#输入回车
device.press('KEYCODE_ENTER')
#return keyboard 点击返回用于取消等下看到截图的下方的白条
#device.press('KEYCODE_BACK')
#------
#takeSnapshot截图
mr.sleep(3.0)
result = device.takeSnapshot()
#save to file 保存到文件
result.writeToFile('takeSnapshot\\result1.png','png');
2.图形化记录与回放
利用
[plain]
SPAN style="FONT-FAMILY: SimSun">monkeyrunner monkey_recorder.py</SPAN>导出之后可以运行monkeyrunner monkey_playback.py monkey_test1.mr,(monkey_test1.mr为刚才保存的脚本),这时可以看到模拟器,进行刚才一样的操作。
下面是这两个有用的脚本:
monkey_recorder.py:
[python]
#!/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)#!/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.[python]
#!/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()#!/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.# 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'
continueif cmd not in CMD_MAP:
print 'unknown command: ' + cmd
continueCMD_MAP[cmd](device, rest)
def main():
file = sys.argv[1]
fp = open(file, 'r')device = MonkeyRunner.waitForConnection()
process_file(fp, device)
fp.close();
四、使用插件扩展Monkeyrunner
您可以用Java语言创建新的类,并打包成一个或多个.jar文件,以此来扩展monkeyrunnerAPI。您可以使用您自己写的类或者继承现有的类来扩展monkeyrunnerAPI。您还可以使用此功能来初始化monkeyrunner环境。
为了使monkeyrunner加载一个插件,您应当如使用如表1中所述的-plugin参数来调用monkeyrunner命令。
在您编写的插件中,您可以导入或继承位于com.android.monkeyrunner包中的几个主要的monkeyrunner类:MonkeyDevice, MonkeyImage和MonkeyRunner.请注意,插件无法让你访问Android的SDK。您不能导入com.android.app等包。这是因为monkeyrunner是在框架API层次之下与设备或模拟器进行交互的。
插件启动类
用于插件的.jar文件可以指定一个类,使其在脚本执行之前就实例化。如欲指定这个类,您需要在.jar文件的manifest中添加键MonkeyRunnerStartupRunner。其值为启动时运行的类的名称。以下代码段显示了如何在一个ant构建脚本达到这样的目的:
如欲访问monkeyrunner的运行时环境,启动类可以实现com.google.common.base.Predicate。例如,用这个类在默认的命名空间中设置一些变量:
[python]
package com.android.example;
import com.google.common.base.Predicate;
import org.python.util.PythonInterpreter;
public class Main implements Predicate {
@Override
public boolean apply(PythonInterpreter anInterpreter) {
/*
* Examples of creating and initializing variables in the monkeyrunner environment's
* namespace. During execution, the monkeyrunner program can refer to the variables "newtest"
* and "use_emulator"
*
*/
anInterpreter.set("newtest", "enabled");
anInterpreter.set("use_emulator", 1);
return true;
}
}package com.android.example;
import com.google.common.base.Predicate;
import org.python.util.PythonInterpreter;
public class Main implements Predicate {
@Override
public boolean apply(PythonInterpreter anInterpreter) {
/*
* Examples of creating and initializing variables in the monkeyrunner environment's
* namespace. During execution, the monkeyrunner program can refer to the variables "newtest"
* and "use_emulator"
*
*/
anInterpreter.set("newtest", "enabled");
anInterpreter.set("use_emulator", 1);
return true;
}
}
1.“device.touch(10,100, 'DOWN_AND_UP')” 为什么报错?ANS:可以把 'DOWN_AND_UP' 替换为 'MonkeyDevice.DOWN_AND_UP' ,这个问题的主要原因跟Monkeyrunner有关。
2.我输入了很多指令,但是很多没有执行Ans:可以尝试 MonkeyRunner.sleep(time_in_seconds) 方法,让不同的动作之间有间隔。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/24533.html