码迷,mamicode.com
首页 > 其他好文 > 详细

第8章5节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-运行测试脚本

时间:2015-12-08 10:17:39      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

MonkeyRunner在准备好AndroidDebugBridge和DeviceMonitor等服务之后,就基本上是解决了和目标设备通信的问题了,那往下需要做的就是把测试脚本运行起来了。

178   public static void main(String[] args) {
179     MonkeyRunnerOptions options = MonkeyRunnerOptions.processOptions(args);
180 
181     if (options == null) {
182       return;
183     }
184 
185 
186     replaceAllLogFormatters(MonkeyFormatter.DEFAULT_INSTANCE, options.getLogLevel());
187 
188     MonkeyRunnerStarter runner = new MonkeyRunnerStarter(options);
189     int error = runner.run();
190 
191 
192     System.exit(error);
193   }
194 }
代码8-5-1 MonkeyRunnerStarter - Main

从以上代码和本章上面几节分析可知,MonkeyRunnerStarter在实例化MonkeyRunnerStarter的过程中启动了AndroidDebugBridge和DeviceMonitor,然后就会进入下一行189行去调用MonkeyRunnerStarter的run方法。

66   private int run()
 67   {
 68     String monkeyRunnerPath = 
System.getProperty("com.android.monkeyrunner.bindir") 
+ 
File.separator + "monkeyrunner";
 69 
 70 
 71     Map<String, Predicate<PythonInterpreter>> plugins = handlePlugins();
 72     if (this.options.getScriptFile() == null) {
 73       ScriptRunner.console(monkeyRunnerPath);
 74       this.chimp.shutdown();
 75       return 0;
 76     }
 77     int error = ScriptRunner.run(monkeyRunnerPath, 
this.options.getScriptFile().getAbsolutePath(), this.options.getArguments(), plugins);
 78    
 79     this.chimp.shutdown();
 80     return error;
 81   }
代码8-5-2 MonkeyRunnerStarter - run

  • 68行:取得monkeyrunner脚本的绝对路径。“com.android.monkeyrunner.bindir"我们在前面分析过,它代表的就是你的sdk安装目录下的”/tools”,然后再加上文件分隔符”/”以及”monkeyrunner”这个脚本。所以最终的结果就类似于”/Users/apple/Develop/sdk/tools/monkeyrunner”
  • 72-73行: 如果用户在命令行运行monkeyrunner时没有提供脚本文件路径这个参数,那么就调用ScriptRunner类的console来请求jython解析器打开一个交互窗口来让用户进行交互
  • 74行: 用户停止交互关闭窗口时调用ChimpChat的shutDown方法来通知相应模块测试已经停止,以便它们做相应的处理。比如会给monkey服务发送“quit”命令,通知它测试已经停止
  • 77行: 如果用户在命令行运行monkeyrunner时提供了脚本路径这个参数,那么调用的将会是ScriptRunner的run方法来将该脚本运行起来,其实里面最终调用的就是jython的解析器来运行脚本。

无论是打开交互console还是直接运行脚本,最终用到的都是jython解析器来做事情,比如我们进去ScriptRunner的run方法:

77   public static int run(String executablePath, 
String scriptfilename, 
Collection<String> args, 
Map<String, Predicate<PythonInterpreter>> plugins)
 78   {
...
 94     PythonInterpreter python = new PythonInterpreter();
...
114     try
115     {
116       python.execfile(scriptfilename);
117     }
...
}
代码8-3-3 ScriptRunner - run

做的事情就是去实例化一个jython的解析器,PythonInterpreter所在的包是“org.python.util”。获得jython解析器后就直接调用解析器的execfile方法去执行目标测试脚本了。

注:更多文章请关注公众号:techgogogo或个人博客http://techgogogo.com。当然,也非常欢迎您直接微信(zhubaitian1)勾搭。本文由天地会珠海分舵原创。转载请自觉,是否投诉维权看心情。


第8章5节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-运行测试脚本

标签:

原文地址:http://blog.csdn.net/zhubaitian/article/details/50212811

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!