3rd Chapter 1 "Monkeyrunner Source Analysis" Scripting Example: Monkeyrunner API use Example (original)

Source: Internet
Author: User

Heaven Zhuhai Branch Rudder note : Originally this series is to prepare a book, details please see the earlier blog "to seek partners to write the deep understanding of Monkeyrunner" books. But for many reasons, there is no wish. So the draft is shared here, so mistakes are unavoidable. If necessary, please refer to the following, the forwarding of the word also keep the end of each article source and other information.

Monkeyrunner This class can be said to be the entry to write Monkeyrunner script, because this class has a very important method watforconnection, its function is to try to put Monkeyrunner background and the specified device to establish a good connection. Only when the device is connected, Monkeyrunner can drive various services on the device side to operate the application under test. So the first step in writing the Monkeyrunner test script is to call the method to make the connection first.

The method accepts two parameters, but the user can fill it out, or it can not fill it out. The first parameter is the number of seconds in the connection waiting time, the default is not filled out is infinite wait; the second parameter is the device ID, which can be represented by a regular expression, which specifies the first device to be found if it is not filled in by default. We can obtain the target device serial number by ordering the "adb devices-l" command:

Figure 3-1-1 List all device information

This command lists the basic information about all the Android devices that are connected to the host, the leftmost of which is the ID of the device we are looking for. It should be noted that there are two devices listed, and that there is a big difference in their form. This is because there are two ways to connect devices through the ADB, one of which is directly connected via the USB protocol, once the device is connected to the host can be listed by the command device, and the other is to specify the IP address of the target Android machine via TCP to connect, the user needs to execute "adb Connect IP "To connect the IP-address device to the host, and then the device can be listed by the previous command." If the USB protocol is connected directly, the listed device ID is equivalent to the serial number of the device, if it is connected through the TCP protocol, the format of the listed device ID is "Ip:port", where IP is the target device IP address, port is the target device ADBD listening port.

Of course, the Monkeyrunner class has many other methods in addition to the most critical waitforconnection, and Waitforconnection is the same static method as these methods, So users do not need to instantiate monkeyrunner to be able to use these methods directly.

In this section we will look at a code example of how the Waitforconnection method should be called in the Monkeyrunner script to connect to the device. The goal of this code is simply to print out the serial number of the target Android device after connecting the device.

In addition to using Monkeyrunner's Waitforconnection method, the sample code uses an alert method to pop up a prompt to the user when the connection fails, and to determine if the device on the connection is really the target device we want, Also call the next section describes the Monkeydevice Getsystemproperty method to obtain the device serial number and print out, the following use of these methods are listed below for your reference:

Table 3-1-1 the list of key methods used in the sample code

The sample code is as follows:

Code 3-1-1 monkeyrunner-waitforconnection Example

1  fromCom.android.monkeyrunnerImportMonkeyrunner,monkeydevice2   3DEVICEID ="192.168.1.102:5555"  4TIMEOUT = -  5   6 Try:7     #Connect to specific device  8device = Monkeyrunner.waitforconnection (Timeout,deviceid)9  Ten     #Sleep 3 seconds  OneMonkeyrunner.sleep (3) A   -     PrintDevice.getsystemproperty ("Ro.serialno") - except: theMonkeyrunner.alert ("Faield to connect to device","Exception occurs","OK")

Since this is our first example of scripting, here we will parse the code one line at a point:

    • Line 1th: From the Com.android.monkeyrunner of the Monkeyrunner.jar library, this package introduces the two classes of Monkeyrunner and Monkeydevice, because the test script uses the Jython language to write, so you can call the Java-written library. With these two classes, mainly with the Monkeyrunner class, we can connect the device.
    • 3–4: Two parameters are required to define the Waitforconnection method for calling Monkeyrunner, the ID of the target Android machine and the connection wait timeout
    • Line 8th: Call the Waitforconnection method of Monkeyrunner with timeout wait time and target device ID as parameters, note that the method returns an instance of Monkeydevice when it succeeds
    • Line 11th: Call Monkeyrunner's Sleep method to allow the test case to wait 3 seconds before the device is not fully ready.
    • Line 13th: Call the Getsystemproperty method of the Monkeydevice instance to get the serial number of the target Android device and print it out
    • 第14-15: If the connection waitforconnection connection target Android device fails, it throws an exception, the purpose of these two lines is to catch the exception and call the Monkeyrunner alert method to pop up a popup box on the host side to remind the user

After writing the test case, you can call up the Monkeyrunner command to run the Jython script. The following is the result of the successful serial number output:

Figure 3-1-3 Output-successfully obtained serial number

If the device fails to connect, such as if the device ID is not found, the script pops up a prompt to tell the user about the failure to connect to the target device:

Figure 3-1-4 Connecting the device failure pop-up box

——— to Be Continued ———

Heaven Zhuhai Branch Rudder
Public Number: Techgogogo
Weibo: Http://weibo.com/techgogogo
Csdn:http://blog.csdn.net/zhubaitian

3rd Chapter 1 "Monkeyrunner Source Analysis" Scripting Example: Monkeyrunner API use Example (original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.