第8章第5节 2

从以上代码和本章上面几节分析可知,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方法去执行目标测试脚本了。


你可能感兴趣的:(软件测试开发)