关于Android自动化测试,研究了Monkey,Robotium 这次来看下 Monkeyrunner.

具体实践最靠谱的当然还是官网资料了。

http://developer.android.com/tools/help/monkeyrunner_concepts.html

 

这里简单记录下实践过程,Monkeyrunner 需要用Python来编写,对于曾未学过Python的童鞋来说也没关系,因为Python属于比较好学的一门脚本语言.笔者也未曾学过Python,但有其他编程基础如:PHP,Java,Peal,还是能够很好理解Python的。

 

一、monkeyrunner 介绍

monkeyrunner 提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.

 

二、monkeyrunner 测试类型

多设备控制、功能测试、回归测试

 

三、实例(测试MyAndroid.apk)

1. 新建一个 monkeyrunnerTest.py

            
  1. # Import the monkey runner modules used by this program 
  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 
  3.  
  4. # Connects to current device, returning a MonkeyDevice object 
  5. device = MonkeyRunner.waitForConnection() 
  6.  
  7. # Installs the Android package 
  8. device.installPackage("./MyAndroid.apk"
  9.  
  10. # Runs the component 
  11. device.startActivity(component = 'com.luwenjie.android/.MyAndroidActivity'
  12.  
  13. #Presses the Menu button 
  14. device.press('KEYCODE_MENU','DOWN_AND_UP'
  15.  
  16. #Takes a screenshot 
  17. result = device.takeSnapshot() 
  18.  
  19. # Writes the screenshot to a file 
  20. result.writeToFile('./shot1.png','png')

2. 运行在 %Android_HOME%\tools 目录下运行一下命令

monkeyrunner  monkeyrunnerTest.py

 

四、API参考

MonkeyRunner:http://developer.android.com/tools/help/MonkeyRunner.html

MonkeyDevice:http://developer.android.com/tools/help/MonkeyDevice.html

MonkeyImage:http://developer.android.com/tools/help/MonkeyImage.html

 

五、录制模式

            
  1. #Usage: monkeyrunner recorder.py 
  2.  
  3. #recorder.py  http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_recorder.py 
  4.  
  5.  
  6. from com.android.monkeyrunner import MonkeyRunner as mr 
  7. from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder 
  8.  
  9. device = mr.waitForConnection() 
  10. recorder.start(device) 
  11. #END recorder.py 
  12. #Press ExportAction to save recorded scrip to a file 
  13. #Example of result: 
  14. #PRESS|{'name':'MENU','type':'downAndUp',} 
  15. #TOUCH|{'x':190,'y':195,'type':'downAndUp',} 
  16. #TYPE|{'message':'',} 
  17.  
  18.  
  19. #Usage: monkeyrunner playback.py "myscript" 
  20. #playback.py   http://mirror.yongbok.net/linux/android/repository/platform/sdk/monkeyrunner/scripts/monkey_playback.py 
  21. import sys 
  22. from com.android.monkeyrunner import MonkeyRunner 
  23.  
  24. # The format of the file we are parsing is very carfeully constructed. 
  25. # Each line corresponds to a single command.  The line is split into 2 
  26. # parts with a | character.  Text to the left of the pipe denotes 
  27. # which command to run.  The text to the right of the pipe is a python 
  28. # dictionary (it can be evaled into existence) that specifies the 
  29. # arguments for the command.  In most cases, this directly maps to the 
  30. # keyword argument dictionary that could be passed to the underlying 
  31. # command.  
  32.  
  33. # Lookup table to map command strings to functions that implement that 
  34. # command. 
  35. CMD_MAP = { 
  36.     'TOUCH'lambda dev, arg: dev.touch(**arg), 
  37.     'DRAG'lambda dev, arg: dev.drag(**arg), 
  38.     'PRESS'lambda dev, arg: dev.press(**arg), 
  39.     'TYPE'lambda dev, arg: dev.type(**arg), 
  40.     'WAIT'lambda dev, arg: MonkeyRunner.sleep(**arg) 
  41.     } 
  42.  
  43. # Process a single file for the specified device. 
  44. def process_file(fp, device): 
  45.     for line in fp: 
  46.         (cmd, rest) = line.split('|'
  47.         try
  48.             # Parse the pydict 
  49.             rest = eval(rest) 
  50.         except
  51.             print 'unable to parse options' 
  52.             continue 
  53.  
  54.         if cmd not in CMD_MAP: 
  55.             print 'unknown command: ' + cmd 
  56.             continue 
  57.  
  58.         CMD_MAP[cmd](device, rest) 
  59.  
  60.  
  61. def main(): 
  62.     file = sys.argv[1
  63.     fp = open(file, 'r'
  64.  
  65.     device = MonkeyRunner.waitForConnection() 
  66.      
  67.     process_file(fp, device) 
  68.     fp.close(); 
  69.      
  70.  
  71. if __name__ == '__main__'
  72.     main() 

 

更多相关文章

  1. Kali Linux将Android设备变成黑客瑞士军刀
  2. Android官方开发文档Training系列课程中文版:连接无线设备之通过W
  3. Android模拟器虚拟串口实现串口通信
  4. Android SDK中的自动化测试
  5. Android 关于极光推送时测试和正式的问题
  6. Android开发-搭建Junit测试环境

随机推荐

  1. linux常用的有关网络操作的命令:
  2. linux的0号进程和1号进程
  3. Linux 驱动面试题总结
  4. Linux命令之find(一)
  5. 从Android应用访问Chromebook的localhost
  6. MeeGo定位跨4大平台OS Novell主攻平板电
  7. linux下安装nginx,支持rewrite、ssl
  8. 在linux 列出 超级用户 普通用户和 系统
  9. Ubuntu 14.04 LTS 使用sudo免输密码
  10. 为SWIG指定Python头和库。