android基础知识12:android自动化测试05—monkeyRunner

来源:互联网 发布:凯瑞欧文数据 编辑:程序博客网 时间:2024/06/06 01:45

之前的几篇文章,我们分别介绍了monkey,junit, Robotium,以及基于junit的android测试框架进行介绍,本篇文章我们将对monkeyRunner 进行介绍。

1、什么是monkeyrunner
        monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。
2、monkeyrunner工具同Monkey工具的差别
Monkey:
Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流。
monkeyrunner:
monkeyrunner工具则是在工作站上通过API定义的特定命令和事件控制设备或模拟器。
3、monkeyrunner的测试类型

  • 1、多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。
  • 功能测试: monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。
  • 回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。
  • 可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner API之外,您还可以使用标准的Python os和subprocess模块来调用Android Debug Bridge这样的Android工具
4、运行monkeyrunner
         您可以直接使用一个代码文件运行monkeyrunner,抑或在交互式对话中输入monkeyrunner语句。不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。
monkeyrunner的命令语法为:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
5、实例1
5.1 简单实例
以sample中的ApiDemos为例,先将其生成ApiDemos.apk。
前提:已有device连接
1)、 将ApiDemos.apk放在$Android_Root\tools下。
2)、 在$Android_Root\tools下新建一个monkeyrunnerprogram.py文件,里面内容为:
[java] view plaincopy
  1. # Imports the monkeyrunner modules used by this program  
  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage  
  3. # Connects to the current device, returning a MonkeyDevice object  
  4. device = MonkeyRunner.waitForConnection()  
  5. # Installs the Android package. Notice that this method returns a boolean, so you can test  
  6. # to see if the installation worked.  
  7. device.installPackage('./ApiDemos.apk')  
  8. # Runs the component  
  9. device.startActivity(component='com.example.android.apis/.ApiDemos')  
  10. # Presses the Menu button  
  11. device.press('KEYCODE_MENU','DOWN_AND_UP')  
  12. # Takes a screenshot  
  13. result = device.takeSnapshot()  
  14. # Writes the screenshot to a file  
  15. result.writeToFile('./shot1.png','png')  
注意:SDK上的例子有些错误,不可直接复制,否则执行命令时会发生错误。具体可与我的上面这段代码对照。
3)、 打开命令行转到Android_Root\tools目录下运行一下命令:
monkeyrunner monkeyrunnerprogram.py
若无错误,则运行完成以后,$Android_Root\tools目录下会生成shot1.png文件。注意,在运行过程中,若没有错误,命令行没有任何输出。
5.2 扩展实例
因为ApiDemos首页上按下MENU键没有菜单出现,为了更加形象化,在实例五的基础上继续试验:
1)、 在$Android_Root\tools下新建一个monkeyrunnerprogram1.py文件,里面内容为:
[java] view plaincopy
  1. # Imports the monkeyrunner modules used by this program    
  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage  
  3. # Connects to the current device, returning a MonkeyDevice object  
  4. device = MonkeyRunner.waitForConnection()  
  5. # Takes a screenshot  
  6. result = device.takeSnapshot()  
  7. # Writes the screenshot to a file   
  8. result.writeToFile('./shotbegin.png','png')  
  9. # Presses the Down button  
  10. device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')  
  11. device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')  
  12. device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')  
  13. device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')  
  14. device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')  
  15. # Takes a screenshot  
  16. result = device.takeSnapshot()  
  17. # Writes the screenshot to a file  
  18. result.writeToFile('./shotend.png','png')  
2)、将画面定位在Apidemos的首页,并将光标定位在第一项上。
3)、$Android_Root\tools目录下运行一下命令:
monkeyrunner monkeyrunnerprogram1.py
4)、在运行过程中我们可以看见光标不断向下移动,并且可以在当前目录下我们自定义的截图:
运行前:shotbegin.png


6、扩展应用实例
下面提供一些常用的脚本,自己看着来改吧..
monkey_recorder.py
monkey_placback.py
help.py
具体下载地址为:monkeyrunner_py脚本.rar
虽然,少了些东西,但是,并不影响我们大部分的需要.接下来用一段典型的monkeyRunner代码讲解!
注意!如果monkeyrunner脚本文件要使用中文,记得格式保存为utf8,不然会ASCNII(忘了怎么拼写了..)无法支持错误
6.1 takescreen.py
文件takescreen.py
[java] view plaincopy
  1. #导入我们需要用到的包和类并且起别名  
  2. import sys  
  3. from com.android.monkeyrunner import MonkeyRunner as mr  
  4. from com.android.monkeyrunner import MonkeyDevice as md  
  5. from com.android.monkeyrunner import MonkeyImage as mi  
  6.   
  7. #connect device 连接设备  
  8. #第一个参数为等待连接设备时间  
  9. #第二个参数为具体连接的设备  
  10. device = mr.waitForConnection()  
  11. if not device:  
  12.     print >> sys.stderr,"fail"  
  13.     sys.exit(1)  
  14. #定义要启动的Activity  
  15. componentName='com.example.android.notepad/.NotesList'  
  16. #启动特定的Activity  
  17. device.startActivity(component=componentName)  
  18. mr.sleep(3.0)  
  19. #do someting 进行我们的操作  
  20. #输入 a s d  
  21. #device.press('KEYCODE_HOME')  
  22. #device.touch(418736,'DOWN_AND_UP')  
  23. #mr.sleep(3.0)  
  24. #device.touch(180520,'DOWN_AND_UP')  
  25. #mr.sleep(3.0)  
  26. device.press('KEYCODE_MENU')  
  27. mr.sleep(3.0)  
  28. device.touch(243745,'DOWN_AND_UP')  
  29. mr.sleep(3.0)  
  30. device.type('woaini')  
  31. device.press('KEYCODE_ENTER')  
  32. mr.sleep(3.0)  
  33. device.type(',')  
  34. device.press('KEYCODE_ENTER')  
  35. mr.sleep(3.0)  
  36. device.type('yiwen')  
  37. device.press('KEYCODE_ENTER')  
  38. mr.sleep(3.0)  
  39. device.press('KEYCODE_BACK')  
  40. #device.press('KEYCODE_HOME')  
  41. #device.type('asd')  
  42. #输入回车  
  43. #device.press('KEYCODE_ENTER')  
  44. #return keyboard 点击返回用于取消等下看到截图的下方的白条  
  45. #device.press('KEYCODE_BACK')  
  46. #------  
  47. #takeSnapshot截图  
  48. mr.sleep(3.0)  
  49. result = device.takeSnapshot()  
  50.    
  51. #save to file 保存到文件  
  52. result.writeToFile('result1.png','png');  
这里用到的notelist实例类似于android提供的notepad实例。
其运行结果为(图result1.png):


6.2 monkeyRunner 的记录和回放
        前面讲的都是一些在命令行上的操作,我可记不住那么多的指令操作,我可不知道,我点击的这个点的坐标是多少,我多么希望,我能够在可视化界面里面讲我的操作记录下来,然后,直接重新播放,就像宏一样,我可以很高兴的告诉你,MonkeyRunner有这个功能实现起来也非常简单,我提供的打包文件中有一个,monkey_recorder.py,直接在命令行中打上:
monkeyrunner monkey_recorder.py

其中手机屏幕部分,与当前连接的手机设备的屏幕显示是一致的。
对其中脚本显示的一些说明:

接下来运行我们的保存的脚本,然后,你就看到模拟器,进行你刚才一样的操作
monkeyrunner monkey_playback.py monkey_test.mr
打开我们的文件可以看到其实就是一些monkeyrunner的一些脚本
[python] view plaincopy
  1. TOUCH|{'x':329,'y':132,'type':'downAndUp',}   
  2. TOUCH|{'x':100,'y':100,'type':'downAndUp',}   
  3. TOUCH|{'x':296,'y':407,'type':'downAndUp',}   
  4. TOUCH|{'x':296,'y':407,'type':'downAndUp',}   
  5. TOUCH|{'x':296,'y':407,'type':'downAndUp',}   
  6. TOUCH|{'x':296,'y':407,'type':'downAndUp',}   
  7. TOUCH|{'x':351,'y':227,'type':'downAndUp',}  
当然,有界面为什么不用呢~~~呵呵~
补充一点:如果我们要进行多设备测试怎么办呢?
我们可以打开monkey_playback.py文件
[java] view plaincopy
  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()  

至此,我们已经简单介绍完monkeyRunner ,相信大家已经对其它有一个大致的了解,并且可以简单应用了。
如果大家还不够了解,可以看看“monkeyrunner_py脚本.rar”中的所有源码,以及其中monkeyRunner各个类的说明文件。 
另外谷歌原始说明文件为:http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html,有兴趣可以看看。

源码下载:http://download.csdn.net/detail/xianming01/4253617

参考文献:
Android自动测试之monkeyrunner工具
android实用测试方法之Monkey与MonkeyRunner
转载地址:http://blog.csdn.net/xianming01/article/details/7495868
作者:xianming01
原创粉丝点击