Monkeyrunnerstarter is the entry class at Monkeyrunner startup, because it contains the main method. Its entire startup process mainly does the following several things:
- resolves the parameters that were passed in from the command line when the user started Monkeyrunner : Because Monkeyrunner needs to do things according to the specified parameters, such as a script that needs to be executed. If you do not know any parameters, it will enter Monkeyrunner interactive mode, in fact, is the Jythong interactive mode, so that users can write code side to execute
- start Androiddebugbridge : It is actually starting the ADB server, because one of the most important ways Monkeyrunner communicates with the device is to request the service of the target device by sending a command to the ADB server
- start Device monitoring thread : This is actually started when the Androiddebugbridge is started. Device monitoring thread The main thing to do is to check whether the device has access or removal, if a new device is connected, or the device becomes online status (a device has multiple states: online| offline| recovery| Unauthorized), then you need to take the monitoring device inside each of the debugging process, which is mainly used for debugging tools such as DDMS. It maintains an up-to-date list of devices
- start Androiddebugbridge :
Li class= "li1" >
start Monkey :
- Run the test script :
In this section we will first look at how Monkeyrunner gets the command line arguments and parses them when it is started. The whole process is similar to the command-line parameter analysis of monkey at boot time. Let's look at the relationship between the key classes involved:
Figure 8-2-1 Monkeyrunnerstarter class diagram
From the class diagram we see that Monkeyrunnerstarter holds a member variant of the Monkeyrunneroptions type options, which is the parsed command-line argument that the instance holds. The class also provides a Processoptions method to specifically parse the command-line arguments.
Let's go first to the main method of the Monkeyrunnerstart class:
178 public static void Main (string[] args) {179 monkeyrunneroptions options = Monkeyrunneroptions.processoptions (args); 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 (); 191 192 system.exit (error); 193 }194}
Code 3-2-1 monkeyrunnerstart-main
Here are three main things to do:
- 179 line to handle the command line arguments entered when the user starts Monkeyrunner
- 188 lines to initialize the Monkeyrunnerstarter, the main is the initialization of the chimpchat,chimpchat and to open the Androiddebugbridge process and to open the Devicemonitor device monitoring thread, We'll do a detailed analysis in the next section.
- 189 lines to run the Monkeyrunner, including the case with script parameters and not to provide the Jython command line directly without scripting parameters
In this section we will first analyze how the Monkeyrunner parameter is processed, and we jump to the Processoptions method in the Monkeyrunneroptions class:
Monkeyrunneroptions public static processoptions (string[] args) 94 {hostn int index = 0; ame = default_monkey_server_address; 98 File scriptfile = NULL; backend int port = default_monkey_port;100 String = "adb"; 101 level LogLevel = level.severe;102 103 Imm utablelist.builder<file> Pluginlistbuilder = Immutablelist.builder (); 104 immutablelist.builder<string> Argumentbuilder = Immutablelist.builder (); while (Index < args.length) {106 String argument = args[(index+ +)];107 108 if ("-S". Equals (argument)) {109 if (index = = args.length) {+ printusage ("Missing Ser Ver after-s "); 111 return null;112}113 hostname = args[(index++)];114}115 else if ("-P". Equals (argument)) 117 if (index = = args.length) {118 printusage ("Missing Server port aft Er-p "); 119 return null;120}121 port = Integer.parseinT (args[(index++)]); 122}123 Else if ("-V". Equals (argument)) 124 {(index = = args.length) {1 Printusage ("Missing Log level After-v"); 127 return null;128}129, logLevel = Le Vel.parse (args[(index++))); 131} else if ("-be". Equals (argument)). {133 if (index = = args.length) {1 Printusage ("Missing backend name after-be"); 135 return null;136}137 backend = arg s[(index++)];138} else if ("-plugin". Equals (argument)) 139 {if (index = = args.length) {141 Printusage ("Missing plugin path After-plugin"); 142 return null;143}144 File plugin = new Fi Le (args[(index++)]); 145 if (!plugin.exists ()) {146 Printusage ("plugin file doesn ' t exist"); 147 return null;148}149 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 {16 0 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 ()) {printusage ("Can ' t open specified script file"); 171 return null;172}173 if (!scriptfile.canread ()) {174 printusage ("Can ' t open s pecified script File "), 175 return null;176}177} else {178 argumentbuilder.add (AR gument); 179}180}181}182 183 return new Monkeyrunneroptions (hostname, port, ScriptFile, backend, l Oglevel, Pluginlistbuilder.build (), Argumentbuilder.build ()); 184}185}
Code 8-2-2 monkeyrunneroptions-processoptions
Here we first look at a few variables of 99-101 rows initialized, if the user does not specify the corresponding parameters in the command line, then these default parameters will be used, and we look at these default values are what:
- Hostname: Corresponding to the '-s ' parameter, the default value is ' 127.0.0.1 ', that is, the machine will be forward to the target device to run monkey, so add the following forwarding port equivalent to the target machine listen the monkey Service
- Port: Corresponding to the '-P ' parameter, the default value is ' 12345 ', which is monkey default listening port
- Backend: corresponding to the '-be ' parameter, the default value is ' adb ', actually looking back at the code we will find that it is only supporting ' adb '. It is important to note that this is a hidden parameter, and the command line help does not show this parameter
- LogLevel: Corresponds to '-V ' parameter, default value ' SEVERE ', which means only serious log is printed
The code down is the user input parameters of the parse and save, here to pay attention to a few hidden parameters:
- -U: At first glance think this is a special parameter, from 156-178 rows can see the meaning of this parameter processing is: When the user input '-u ' is not any processing, but when the user entered by the ' -' started but not Monkeyrunner claimed to support the parameters of the time, will be based on different circumstances to the user error. So the meaning of this code is that when the user enters an unsupported parameter, the user is prompted according to different circumstances.
- -be:backend, as mentioned earlier, only supports ' adb '
- -plugin: Here needs a background knowledge, on the Google official website has the description, the user may follow certain specification to write the plug-in to expand the Monkeyrunner function, For example, in the Monkeydevice to press this action is required by monkeydevice.down this parameter to pass to the press this method, if you think this is not good, you want to add a pressdown such method, the default is to use Monkeydevice.down to drive Dynamic Monkeydevice of the press method, and the user only need to give the coordinates point can be, then you can follow the Google description of the specification to write a plug-in this aspect, when the use of the time can be used in Python directly import into use. This book does not highlight the Monkeyrunner plugin.
After parsing all the parameters, the Processoptions method initializes the Monkeyrunneroptions class based on these parameters. Let's go into the constructor and see what it does:
monkeyrunneroptions Private (string hostname, int port, File scriptfile, String backend, level logLevel, Collection <File> plugins, collection<string> arguments) (+ this.hostname = hostname; This.port = Port this.scriptfile = scriptfile; this.backend = backend; this.loglevel = LogLevel; This.plugins = plugins; this.arguments = arguments;
Code 8-2-3 monkeyrunneroptions-Constructors
The thing to do is simply to save all the parsed arguments to an instance of the Monkeyrunneroptions class, and then go in and take it in the future.
Note: More articles please pay attention to the public number: Techgogogo or personal blog http://techgogogo.com. Of course, you are also very welcome to pick up directly (ZHUBAITIAN1). This article by Heaven Zhuhai Branch Rudder original. Reproduced please consciously, whether the complaint rights to look at the mood.
8th Chapter 2 "Monkeyrunner Source Analysis" Monkeyrunner start run process-Parse processing command line arguments