Android Monkeyrunner Test

    關於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() 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章