博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转] Android自动化测试之MonkeyRunner录制和回放脚本(四)
阅读量:6941 次
发布时间:2019-06-27

本文共 2196 字,大约阅读时间需要 7 分钟。

测试脚本录制:

方案一:

我们先看看以下monkeyrecoder.py脚本:

 
  1. #Usage: monkeyrunner recorder.py  
  2. #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   ; 
  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"

转载地址:http://azinl.baihongyu.com/

你可能感兴趣的文章
Linux守护进程的编程实现
查看>>
ISCSI工作流程target和initiator
查看>>
Oracle密码过期the password has expired
查看>>
linux grep常用参数
查看>>
button 按钮,结合onclick事件,验证和提交表单
查看>>
<转>python字典排序 关于sort()、reversed()、sorted()
查看>>
java中Token验证
查看>>
医保业务的相关概念
查看>>
【Mac使用系列】【转载】十几个Mac实用工具
查看>>
网易七鱼 Android 高性能日志写入方案
查看>>
微软Visual Studio 2010架构设计功能应用(转)
查看>>
干净的代码是改出来的
查看>>
微软面试题附答案(转)
查看>>
你必须要知道的架构知识~第三章 接口用来制定操作的统一性
查看>>
关于下拉菜单和iframe的问题
查看>>
ASP.NET File.Delete只读文件引起的访问被拒绝,设置文件属性为Normal
查看>>
Sharepoint学习笔记—ECMAScript对象模型系列-- 11、 Enable/Disable Ribbon上的Button
查看>>
python类库26[sqlite]
查看>>
苹果与三星的专利纠纷
查看>>
boost库在工作(36)网络服务端之六
查看>>