Monkey实例测试(MarshalChen)

Monkey实例测试

Windows下(注:2—4步是为了查看我们可以测试哪些应用程序包,可省略):

1、 通过eclipse启动一个Android的emulator

2、 在命令行中输入:adb devices查看设备连接情况

C:\Documents and Settings\Administrator>adb devices

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

如:

com.android.htmlviewer
com.android.settings
com.android.netspeed
com.android.providers.userdictionary
com.android.browser
com.android.contacts
com.android.alarmclock


5.在其中找到我们需要测试的包名com.marshalchen.MonkeyTest1


6、 以com.marshalchen.MonkeyTest1 作为对象进行MonkeyTest

#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,抑或在交互式对话中输入monkeyrunner语句。不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。

monkeyrunner的命令语法为:

monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>


monkeyrunnerbasic.py:

[python]  view plain copy
  1. # 导入此程序所需的monkeyrunner模块   
  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice   
  3. # 连接当前设备,返回一个MonkeyDevice对象   
  4. device = MonkeyRunner.waitForConnection()   
  5. # 安装Android包,注意,此方法返回的返回值为boolean,由此您可以判断安装过程是否正常   
  6. device.installPackage('myproject/bin/MyApplication.apk')   
  7. # 运行此应用中的一个活动device.startActivity(component='com.marshalchen.Monkeyrunner.MainActivity')   
  8. # 按下菜单按键   
  9. device.press('KEYCODE_MENU','DOWN_AND_UP')   
  10. # 截取屏幕截图   
  11. result = device.takeSnapShot  
  12. # 将截图保存至文件   
  13. result.writeToFile('myproject/shot1.png','png')  

然后转到$SDK\TOOLS下执行 monkeyrunner monkeyrunnerbasic.py 就可执行测试了。

PS:上述内容,部分参考了 子杨 的博客,在此表示感谢。

三、常用功能的Monkeyrunner python脚本介绍:


1.输入等基本操作测试


[python]  view plain copy
  1. import sys  
  2. from com.android.monkeyrunner import MonkeyRunner as mr  
  3. from com.android.monkeyrunner import MonkeyDevice as md  
  4. from com.android.monkeyrunner import MonkeyImage as mi  
  5.    
  6. #connect device 连接设备  
  7. #第一个参数为等待连接设备时间  
  8. #第二个参数为具体连接的设备  
  9. device = mr.waitForConnection(1.0,'emulator-5554')  
  10. if not device:  
  11.     print >> sys.stderr,"fail"  
  12.     sys.exit(1)  
  13. #定义要启动的Activity  
  14. componentName='kg.monkey/.MonkeyActivity'  
  15. #启动特定的Activity  
  16. device.startActivity(component=componentName)  
  17. mr.sleep(3.0)  
  18. #do someting 进行我们的操作  
  19. #输入 a s d  
  20. device.type('asd')  
  21. #输入回车  
  22. device.press('KEYCODE_ENTER')  
  23. #return keyboard 点击返回用于取消等下看到截图的下方的白条  
  24. #device.press('KEYCODE_BACK')  
  25. #------  
  26. #takeSnapshot截图  
  27. mr.sleep(3.0)  
  28. result = device.takeSnapshot()  
  29.    
  30. #save to file 保存到文件  
  31. result.writeToFile('takeSnapshot\\result1.png','png');  


2.图形化记录与回放

利用
[plain]  view plain copy
  1. <span style="font-family:SimSun;">monkeyrunner monkey_recorder.py</span>  

脚本,可以打开如图所示可视化模拟器,各种操作可以显示在屏幕右侧,并且通过“Export Actions”导出。




导出之后可以运行monkeyrunner monkey_playback.py monkey_test1.mr,(monkey_test1.mr为刚才保存的脚本),这时可以看到模拟器,进行刚才一样的操作。

补充多设备操作:可以在monkey_playback.py中的main()方法的device设备获取时指定多设备。

下面是这两个有用的脚本:
monkey_recorder.py:

[python]  view plain copy
  1. #!/usr/bin/env monkeyrunner  
  2. # Copyright 2010, The Android Open Source Project  
  3. #  
  4. # Licensed under the Apache License, Version 2.0 (the "License");  
  5. # you may not use this file except in compliance with the License.  
  6. # You may obtain a copy of the License at  
  7. #  
  8. #     http://www.apache.org/licenses/LICENSE-2.0  
  9. #  
  10. # Unless required by applicable law or agreed to in writing, software  
  11. # distributed under the License is distributed on an "AS IS" BASIS,  
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13. # See the License for the specific language governing permissions and  
  14. # limitations under the License.  
  15.   
  16. from com.android.monkeyrunner import MonkeyRunner as mr  
  17. from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder  
  18.   
  19. device = mr.waitForConnection()  
  20. recorder.start(device)  


monkey_playback.py:

[python]  view plain copy
  1. #!/usr/bin/env monkeyrunner  
  2. # Copyright 2010, The Android Open Source Project  
  3. #  
  4. # Licensed under the Apache License, Version 2.0 (the "License");  
  5. # you may not use this file except in compliance with the License.  
  6. # You may obtain a copy of the License at  
  7. #  
  8. #     http://www.apache.org/licenses/LICENSE-2.0  
  9. #  
  10. # Unless required by applicable law or agreed to in writing, software  
  11. # distributed under the License is distributed on an "AS IS" BASIS,  
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13. # See the License for the specific language governing permissions and  
  14. # limitations under the License.  
  15.   
  16. import sys  
  17. from com.android.monkeyrunner import MonkeyRunner  
  18.   
  19. # The format of the file we are parsing is very carfeully constructed.  
  20. # Each line corresponds to a single command.  The line is split into 2  
  21. # parts with a | character.  Text to the left of the pipe denotes  
  22. # which command to run.  The text to the right of the pipe is a python  
  23. # dictionary (it can be evaled into existence) that specifies the  
  24. # arguments for the command.  In most cases, this directly maps to the  
  25. # keyword argument dictionary that could be passed to the underlying  
  26. # command.   
  27.   
  28. # Lookup table to map command strings to functions that implement that  
  29. # command.  
  30. CMD_MAP = {  
  31.     'TOUCH'lambda dev, arg: dev.touch(**arg),  
  32.     'DRAG'lambda dev, arg: dev.drag(**arg),  
  33.     'PRESS'lambda dev, arg: dev.press(**arg),  
  34.     'TYPE'lambda dev, arg: dev.type(**arg),  
  35.     'WAIT'lambda dev, arg: MonkeyRunner.sleep(**arg)  
  36.     }  
  37.   
  38. # Process a single file for the specified device.  
  39. def process_file(fp, device):  
  40.     for line in fp:  
  41.         (cmd, rest) = line.split('|')  
  42.         try:  
  43.             # Parse the pydict  
  44.             rest = eval(rest)  
  45.         except:  
  46.             print 'unable to parse options'  
  47.             continue  
  48.   
  49.         if cmd not in CMD_MAP:  
  50.             print 'unknown command: ' + cmd  
  51.             continue  
  52.   
  53.         CMD_MAP[cmd](device, rest)  
  54.   
  55.   
  56. def main():  
  57.     file = sys.argv[1]  
  58.     fp = open(file, 'r')  
  59.   
  60.     device = MonkeyRunner.waitForConnection()  
  61.       
  62.     process_file(fp, device)  
  63.     fp.close();  
  64.       
  65.   
  66. if __name__ == '__main__':  
  67.     main()  







PS:脚本内容 部分参考了 youxilua ,非常感谢。



四、使用插件扩展Monkeyrunner

您可以用Java语言创建新的类,并打包成一个或多个.jar文件,以此来扩展monkeyrunnerAPI。您可以使用您自己写的类或者继承现有的类来扩展monkeyrunnerAPI。您还可以使用此功能来初始化monkeyrunner环境。
为了使monkeyrunner加载一个插件,您应当如使用如表1中所述的-plugin参数来调用monkeyrunner命令。
在您编写的插件中,您可以导入或继承位于com.android.monkeyrunner包中的几个主要的monkeyrunner类:MonkeyDevice, MonkeyImageMonkeyRunner.
请注意,插件无法让你访问Android的SDK。您不能导入com.android.app等包。这是因为monkeyrunner是在框架API层次之下与设备或模拟器进行交互的。

插件启动类

用于插件的.jar文件可以指定一个类,使其在脚本执行之前就实例化。如欲指定这个类,您需要在.jar文件的manifest中添加键MonkeyRunnerStartupRunner。其值为启动时运行的类的名称。以下代码段显示了如何在一个ant构建脚本达到这样的目的:
如欲访问monkeyrunner的运行时环境,启动类可以实现com.google.common.base.Predicate。例如,用这个类在默认的命名空间中设置一些变量:

[python]  view plain copy
  1. package com.android.example;  
  2.    
  3.  import com.google.common.base.Predicate;  
  4.  import org.python.util.PythonInterpreter;  
  5.    
  6.  public class Main implements Predicate {  
  7.      @Override  
  8.      public boolean apply(PythonInterpreter anInterpreter) {  
  9.    
  10.          /*  
  11.          * Examples of creating and initializing variables in the monkeyrunner environment's  
  12.          * namespace. During execution, the monkeyrunner program can refer to the variables "newtest"  
  13.          * and "use_emulator"  
  14.          *  
  15.          */  
  16.          anInterpreter.set("newtest""enabled");  
  17.          anInterpreter.set("use_emulator"1);  
  18.    
  19.          return true;  
  20.      }  
  21.  }  


五、常见问题(评论中的问题会不断补充)


1.“device.touch(10,100, 'DOWN_AND_UP')” 为什么报错?
ANS:可以把 'DOWN_AND_UP' 替换为 'MonkeyDevice.DOWN_AND_UP' ,这个问题的主要原因跟Monkeyrunner有关。

2.我输入了很多指令,但是很多没有执行
Ans:可以尝试  MonkeyRunner.sleep(time_in_seconds) 方法,让不同的动作之间有间隔。
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值