MonkeyRunner_采用MonkeyRunner自动化测试

来源:互联网 发布:关于网络诈骗的例子 编辑:程序博客网 时间:2024/06/07 10:08

转载自:http://blog.csdn.net/vrix/article/details/6893944

既然是自动化测试,而且又需要写脚本,那是不是可以自动生成测试脚本呢?带着这个问题,我找到了下面的代码 monkeyrecoder.py

[html] view plaincopy
  1. #!/usr/bin/envmonkeyrunner  
  2. # Copyright 2010, TheAndroid Open Source Project  
  3. #  
  4. # Licensed under theApache License, Version 2.0 (the "License");  
  5. # you may not usethis file except in compliance with the License.  
  6. # You may obtain acopy of the License at  
  7. #  
  8. #     http://www.apache.org/licenses/LICENSE-2.0  
  9. #  
  10. # Unless required byapplicable law or agreed to in writing, software  
  11. # distributed underthe License is distributed on an "AS IS" BASIS,  
  12. # WITHOUT WARRANTIESOR CONDITIONS OF ANY KIND, either express or implied.  
  13. # See the License forthe specific language governing permissions and  
  14. # limitations underthe License.  
  15.    
  16. fromcom.android.monkeyrunner import MonkeyRunner as mr  
  17. fromcom.android.monkeyrunner.recorder import MonkeyRecorder as recorder  
  18.    
  19. device =mr.waitForConnection()  
  20. recorder.start(device)  

首先,连接你已经打开调试模式的ANDROID设备,然后运行上面的脚本 monkeyrunner.bat monkeyrecoder.py

执行下面的代码后,将运行录制脚本的程序,截图如下:

 

这个软件就可以生成脚本。但是这个软件生成的脚本不是monkeyrunner可以直接运行的

TOUCH|{'x':243,'y':746,'type':'downAndUp',}
TOUCH|{'x':244,'y':763,'type':'downAndUp',}
DRAG|{'start':(384,320),'end':(76,320),'duration':1.0,'steps':10,}

这种脚本需要另外一个monkeyrunner的脚本来解释执行。monkeyplayback.py

[html] view plaincopy
  1. #!/usr/bin/envmonkeyrunner  
  2. # Copyright 2010, TheAndroid Open Source Project  
  3. #  
  4. # Licensed under theApache License, Version 2.0 (the "License");  
  5. # you may not usethis file except in compliance with the License.  
  6. # You may obtain acopy of the License at  
  7. #  
  8. #     http://www.apache.org/licenses/LICENSE-2.0  
  9. #  
  10. # Unless required byapplicable law or agreed to in writing, software  
  11. # distributed underthe License is distributed on an "AS IS" BASIS,  
  12. # WITHOUT WARRANTIESOR CONDITIONS OF ANY KIND, either express or implied.  
  13. # See the License forthe specific language governing permissions and  
  14. # limitations underthe License.  
  15.    
  16. import sys  
  17. fromcom.android.monkeyrunner import MonkeyRunner  
  18.    
  19. # The format of thefile we are parsing is very carfeully constructed.  
  20. # Each linecorresponds to a single command.  Theline is split into 2  
  21. # parts with a |character.  Text to the left of the pipedenotes  
  22. # which command torun.  The text to the right of the pipeis a python  
  23. # dictionary (it canbe evaled into existence) that specifies the  
  24. # arguments for thecommand.  In most cases, this directlymaps to the  
  25. # keyword argumentdictionary that could be passed to the underlying  
  26. # command.  
  27.    
  28. # Lookup table to mapcommand 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 singlefile 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() 

原创粉丝点击