老李推荐:第8章2节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-解析处理命令行参数 2
我们这一节会先去分析下monkeyrunner是如何对参数进行处理的,我们跳转到MonkeyRunnerOptions这个类里面的processOptions这个方法:
93 public static MonkeyRunnerOptions processOptions(String[] args)
94 {
95 int index = 0;
96
97 String hostname = DEFAULT_MONKEY_SERVER_ADDRESS;
98 File scriptFile = null;
99 int port = DEFAULT_MONKEY_PORT;
100 String backend = "adb";
101 Level logLevel = Level.SEVERE;
102
103 ImmutableList.Builder<File> pluginListBuilder = ImmutableList.builder();
104 ImmutableList.Builder<String> argumentBuilder = ImmutableList.builder();
105 while (index < args.length) {
106 String argument = args[(index++)];
107
108 if ("-s".equals(argument)) {
109 if (index == args.length) {
110 printUsage("Missing Server after -s");
111 return null;
112 }
113 hostname = args[(index++)];
114 }
115 else if ("-p".equals(argument))
116 {
117 if (index == args.length) {
118 printUsage("Missing Server port after -p");
119 return null;
120 }
121 port = Integer.parseInt(args[(index++)]);
122 }
123 else if ("-v".equals(argument))
124 {
125 if (index == args.length) {
126 printUsage("Missing Log Level after -v");
127 return null;
128 }
129
130 logLevel = Level.parse(args[(index++)]);
131 } else if ("-be".equals(argument))
132 {
133 if (index == args.length) {
134 printUsage("Missing backend name after -be");
135 return null;
136 }
137 backend = args[(index++)];
138 } else if ("-plugin".equals(argument))
139 {
140 if (index == args.length) {
141 printUsage("Missing plugin path after -plugin");
142 return null;
143 }
144 File plugin = new File(args[(index++)]);
145 if (!plugin.exists()) {
146 printUsage("Plugin file doesn't exist");
147 return null;
148 }
149
150 if (!plugin.canRead()) {
151 printUsage("Can't read plugin file");
152 return null;
153 }
154
155 pluginListBuilder.add(plugin);
156 } else if (!"-u".equals(argument))
157 {
158 if ((argument.startsWith("-")) && (scriptFile == null))
159 {
160
161
162 printUsage("Unrecognized argument: " + argument + ".");
163 return null;
164 }
165 if (scriptFile == null)
166 {
167
168 scriptFile = new File(argument);
169 if (!scriptFile.exists()) {
170 printUsage("Can't open specified script file");
171 return null;
172 }
173 if (!scriptFile.canRead()) {
174 printUsage("Can't open specified script file");
175 return null;
176 }
177 } else {
178 argumentBuilder.add(argument);
179 }
180 }
181 }
182
183 return new MonkeyRunnerOptions(hostname,
port,
scriptFile,
backend,
logLevel,
pluginListBuilder.build(),
argumentBuilder.build());
184 }
185 }
代码8-2-2 MonkeyRunnerOptions - processOptions
这里首先请看99-101行的几个变量初始化,如果用户在命令行中没有指定对应的参数,那么这些默认参数就会被使用,我们且看下这些默认值分别是什么:
- hostname:对应‘-s'参数,默认值是'127.0.0.1',也就是本机,将会forward给目标设备运行的monkey,所以加上下面的转发port等同于目标机器在listen的monkey服务
- port :对应‘-p'参数,默认值是'12345',也就是monkey默认监听端口
- backend :对应'-be'参数,默认值是‘adb‘,其实往后看代码我们会发现它也只是支持’adb‘而已。这里需要注意的是这是一个隐藏参数,命令行的help没有显示该参数
- logLevel :对应‘-v'参数,默认值'SEVERE',也就是说只打印严重的log
代码往下就是对用户输入的参数的解析并保存了,这里要注意几个隐藏的参数:
- -u :乍一看以为这是一个什么特别的参数,从156-178行可以看到这个参数处理的意义是:当用户输入'-u'的时候不会作任何处理,但当用户输入的是由‘-’开始的但又不是monkeyrunner声称支持的那几个参数的时候,就会根据不同的情况给用户报错。所以这段代码的意思其实就是在用户输入了不支持的参数的时候根据不同的情况给用户提示而已
- -be :backend,如前所述,只支持‘adb'
- -plugin :这里需要一个背景知识,在google官网有说明,用户可以通过遵循一定的规范去编写插件来扩展monkeyrunner的功能,比如在monkeydevice里面按下这个动作是需要通过MonkeyDevice.DOWN这个参数来传给press这个方法的,如果你觉得这样子不好,你希望增加个pressDown这样的方法,里面默认就是用MonkeyDevice.DOWN来驱动MonkeyDevice的press方法,而用户只需要给出坐标点就可以了,那么你就可以遵循google描述的规范去编写一个这方面的插件,到时使用的时候就可以通过python方式直接import进来使用了。本书并不会把MonkeyRunner插件进行重点介绍。
在解析出所有的参数之后,processOptions方法最后根据这些参数来初始化MonkeyRunnerOptions类。我们进入到该构造函数看下它究竟做了什么事情:
38 private MonkeyRunnerOptions(String hostname, int port, File scriptFile, String backend, Level logLevel, Collection<File> plugins, Collection<String> arguments)
39 {
40 this.hostname = hostname;
41 this.port = port;
42 this.scriptFile = scriptFile;
43 this.backend = backend;
44 this.logLevel = logLevel;
45 this.plugins = plugins;
46 this.arguments = arguments;
47 }
代码8-2-3 MonkeyRunnerOptions - 构造函数
所做的事情非常简单,就是把解析出来的所有参数保存到MonkeyRunnerOptions类的实例里面,今后需要的时候就进去拿就好了。
老李推荐:第8章2节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-解析处理命令行参数 2的更多相关文章
- 第8章2节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-解析处理命令行參数
MonkeyRunnerStarter是MonkeyRunner启动时的入口类,由于它里面包括了main方法.它的整个启动过程主要做了以下几件事情: 解析用户启动MonkeyRunner时从命令行传输 ...
- 老李推荐:第14章9节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-遍历控件树查找控件
老李推荐:第14章9节<MonkeyRunner源码剖析> HierarchyViewer实现原理-遍历控件树查找控件 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员 ...
- 老李推荐:第14章5节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态
老李推荐:第14章5节<MonkeyRunner源码剖析> HierarchyViewer实现原理-装备ViewServer-查询ViewServer运行状态 poptest是国内唯一 ...
- 老李推荐:第14章6节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-启动ViewServer
老李推荐:第14章6节<MonkeyRunner源码剖析> HierarchyViewer实现原理-装备ViewServer-启动ViewServer poptest是国内唯一一家培养 ...
- 老李推荐:第14章3节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-HierarchyViewer实例化
老李推荐:第14章3节<MonkeyRunner源码剖析> HierarchyViewer实现原理-HierarchyViewer实例化 poptest是国内唯一一家培养测试开发工程师的培 ...
- 老李推荐: 第14章2节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-HierarchyViewer架构概述
老李推荐: 第14章2节<MonkeyRunner源码剖析> HierarchyViewer实现原理-HierarchyViewer架构概述 HierarchyViewer库的引入让M ...
- 老李推荐:第14章1节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-面向控件编程VS面向坐标编程
老李推荐:第14章1节<MonkeyRunner源码剖析> HierarchyViewer实现原理-面向控件编程VS面向坐标编程 poptest是国内唯一一家培养测试开发工程师的培训机 ...
- 老李推荐:第8章7节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-小结
老李推荐:第8章7节<MonkeyRunner源码剖析>MonkeyRunner启动运行过程-小结 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性 ...
- 老李推荐:第8章5节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-运行测试脚本
老李推荐:第8章5节<MonkeyRunner源码剖析>MonkeyRunner启动运行过程-运行测试脚本 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化 ...
随机推荐
- C++待解
//[要求]按以下描述和要求建立一个含有对象成员的类TeleBook,用类Record定义的数组是TeleBook的数据成员. // 写出所有定义成员函数的代码.执行主函数对其测试. Record私有 ...
- 【UWP】FFmpeg库的编译
本文是关于windows8.1/windows10通用应用下编译ffmpeg的一些需要注意的地方,针对最新的msys2而写,都是我在实际操作中遇到的,但是网上没有提到的.如果大家遇到什么问题或是在之前 ...
- java中final小结
fanal 修饰类,该变量一经赋值,就不能够再修改 修饰类,该类不能让子类继承. 修饰方法,该方法不能被子类重写(隐藏). fanal修饰类与方法的意义 1 某个类或方法实现上已经非常完善,不需要子 ...
- Gulp实现css、js、图片的压缩以及css、js文件的MD5命名
目前做代码压缩合并的工具有很多,诸如gulp,webpack,grunt等等,可以说这些项目构建工具的功能非常之强大:图片压缩.图片转base64.css和js的压缩以及合并,文件的md5重命名 -- ...
- DDD中的分层架构
DDD中的分层架构很好的应用了关注点分离原则Separation of Concerns(SOC),每一层做好自己的事情,减少交叉 表现层 表现层提供用来完成任务的用户界面,如webform wpf ...
- 每天一个Linux命令(14)--head命令
head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然的就是查看档案的结尾啦. 1.命令格式: h ...
- APIJSON,让接口和文档见鬼去吧!
我: APIJSON,让接口和文档见鬼去吧! https://github.com/TommyLemon/APIJSON 服务端: 什么鬼? 客户端: APIJSON是啥? 我: APIJSON是一种 ...
- 《shell脚本学习指南》学习笔记之入门
为什么要使用shell脚本? shell脚本能够轻易处理文件与目录之类的对象,而且是各UNIX系统之间经过POSIX标准化的通用的功能,因此Shell脚本只要“用心写”一次,即可应用到很多系统上,因此 ...
- Dev使用技巧
1. dev调试 I.建工程,选择console application II.设置断点(F4),并按F5,(完成输入后,)其它功能如Next line可运作 2. 修改字体大小:Tools-&g ...
- APICloud使用
APICloud-APP开发平台 [网址:]http://www.apicloud.com/ APICloud studio 下载 打开网址,找到开发者社区->文档->下载->开发工 ...