[轉] Android自動化測試之MonkeyRunner錄製和回放指令碼(四)

來源:互聯網
上載者:User

標籤:android   http   io   os   ar   for   sp   div   art   

測試指令碼錄製:

方案一:

我們先看看以下monkeyrecoder.py指令碼:

 
  1. #Usage: monkeyrunner recorder.py  
  2. #recorder.py  http://mirror.yongbok.net/linux/ ... ey_recorder.py ; 
  3.  com.android.monkeyrunner import MonkeyRunner as mr  
  4.  com.android.monkeyrunner.recorder import MonkeyRecorder as recorder  
  5.   
  6.  device = mr.waitForConnection()  
  7.  recorder.start(device)  
  8. #END recorder.py  

 

首先,串連你已經開啟偵錯模式的ANDROID裝置或模擬器,然後運行上面的指令碼,例如在cmd視窗中執行命令: monkeyrunner monkeyrecoder.py

執行下面的代碼後,將運行錄製指令碼的程式:

#Press ExportAction to save recorded scrip to a file


#Example of result:
#PRESS|{""name"":""MENU"",""type"":""downAndUp"",}
#TOUCH|{""x"":180,""y"":175,""type"":""downAndUp"",}
#TYPE|{""message"":"""",}

=================================================

這種指令碼需要另外一個monkeyrunner的指令碼來解釋執行。monkeyplayback.py

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

 =================================================

Usage:monkeyrunner playback.py "myscript"

[轉] Android自動化測試之MonkeyRunner錄製和回放指令碼(四)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.