3rd Chapter 2 "Monkeyrunner Source Analysis" Scripting Example: Monkeydevice 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.

In the previous section, we learned how to establish a connection between the background and the device by using the static method of the Monkeyrunner class Waitforconnection, and saw that the method would return a Monkeydevice instance object after the connection was established successfully. So in this section we'll write some script code examples to learn how to use the Monkeydevice class.

Monkeydevice This class, we can tell from the class name that what it does is basically related to the target Android device, and in fact, For example, in the previous section, the Getsystemproperty method of the device object is used to obtain the serial number of the target device saved in the system environment variable.

Of course, in addition to getsystemproperty this method, Monkeydevice also provides a lot of device-related operational methods, and these methods are a large part of our test scripts are often used when writing. Let's take a look at the use of the key methods that we used in this section, and we'll refer to them for your reference:

Table 3-2-1 List of key methods used by the sample code


From the table above we can see that you need to specify an action type parameter when calling Monkeydevice's press key method and Touch touch method. Depending on the action type, you can control the sending of different types of events to the target device, and these actions have a corresponding definition in Monkeydevice:

    • Down: A press or Touch method is used to send a pressed event to the device, simulating the action of the user pressing a key or touching the finger on the touch screen.
    • Up: A release event is sent to the device via the press or touch method, simulating the action of the user releasing the button after pressing a key or the user presses the finger on the screen and then releases the finger.
    • DOWN_AND_UP: Sends a press or touch method to the device to send a down event, and then immediately send a release up event, the simulation is the user pressed a button immediately after releasing the finger and the user touch screen immediately after releasing the finger action
    • Move: Sends a mobile touch event to the target Android device, which is typically used in combination with down and up to simulate a drag drag gesture action. Note that this action only touches the touch of the Monkeydevice, because the button only simulates touch screen touch action can do the drag action, the key is no way to do the drag action

So let's take a look at two examples of how we should call these methods to do things in the Monkeyrunner script. At the same time, it is important to reiterate that these sample codes are intended for readers who are not exposed to Monkeyrunner scripting, and are not encapsulated in any way for easy description.

Let's first look at the first example, which is to create a journal in the Notepad app and then delete it, and the whole process involves the invocation of most of the methods we mentioned above.
The first step: connect the target Android machine.

Code 3-2-1 increase Journal-Connect devices

  1fromimport MonkeyRunner,MonkeyDevice  2   3#Step 1: Connect to the target device  4 device = MonkeyRunner.waitForConnection(30,"192.168.1.102:5555")

This step we have seen in the previous section, so do not dwell on, if not remember, please return to the previous section.

Step two: Determine the target Android device on the connection is the device we want.

Code 3-2-2 increase diary-Make sure the target device is desired

  6#Step 2: Make sure that the device connected is as what we expected  7 ret = device.getSystemProperty("ro.serialno")  8assert"HT21ATD05099"

Obtain the serial number by using the Getsystemproperty method provided by Monkeydevice, and then use the assertion on line 8th to determine if the sequence number is the expected sequence number.

Step three: Make sure the target Android device is installed Notepad this app, if not, install it.

Code 3-2-3 Add Journal-install app

 Ten #Step 3:check Whether the NotePad application has already been installed  OneRetpackage = Device.shell ("PM List Packages | grep "+"Com.example.android.notepad") A #value return for "PM list packages" are with the format like "Package:com.example.android.notepad"  - ifRetpackage! ="Package:"+"Com.example.android.notepad": -ret = Device.installpackage ("/users/apple/documents/workspace-luna/monkeyrunnerdemo/app/notepad.apk") the     assertRET = =True

This is mainly concerned with the use of the two methods of Monkeydevice. The first one is the shell method. The purpose of this method is to send the "PM list Packages" command to the target Android machine to query whether the package name of the application is already present on the system, and if it already exists, it means that the application is already installed on the system. Otherwise, you need to call Monkeydevice's second method installpackage to push the application package for the host-specified path to the target machine and install it. Here, check if the Notepad app's package name exists full command is "PM list Packages|grep" Com.example.android.notepad ", where PM is the command tool specifically used to manage application packages on Android, The first part of the pipeline means the package name of all installed applications is listed with the PM command, and the post part of the pipeline means to find the package name "Com.example.android.notepad" of the target application Notepad in all package names. In fact, the return of this command is the same as we do in the host command line "adb shell pm List pakcages|grep" Com.example.android.notepad ". Below we compare the output of the function when the other packet is filtered by the back part of the pipeline and the output after the filter with the back part of the pipeline.

Figure 3-2-1 List all installed package names

Figure 3-2-2 lists only the target package names

From the comparison you can see that if the filter function is not added to the list is a lot of installed applications of the package name, coupled with the filtering function behind the pipeline will only list our target application Notepad corresponding package name. These are actually the basic commands of the Linux operating system to use knowledge.

It is important to note that the PM command returns the package name information for the target app, preceded by an identity string "packages:", and then the real package name. Therefore, it is necessary to do some processing on the 13 line to determine whether the package name returned is the same as the expected package name.

If the target app's package name does not exist on the target system, then call Monkeydevice's InstallPackage method on line 14 to install the APK for the host local path specified by the parameter onto the target Android machine.

The last 15 lines use assertions to determine whether the command executes successfully, that is, if the installation was successful, and if it fails, throws an exception.

Fourth step: Launch the app.

Code 3-2-4 Add Journal-launch app

17#Step 4: Start the NotePad apk and direct to activity com.example.android.notepad.NotesList18 device.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList"19  20 MonkeyRunner.sleep(3)

After determining that the target application has been installed on the target device, the next thing to do is to open the target application and navigate to the noteslist of the target application. Just as every code has a portal main function, every app in Android will have an entry activity. As to whether an activity is an entry is specified by the Androidmanifest.xml of the project, see the description of the Androidmanifest.xml from the Notepad project for a specific example:

Figure 3-2-3 Androidmanifest.xml designated entry actvity

Fifth step: Enter Noteeditor Activity

Code 3-2-5 Add Journal-Open Noteeditor Activity

  A #Step 5:direct to the Noteeditor activity to add a note  atDevice.press (' Keycode_menu ', monkeydevice.down_and_up); -   -Monkeyrunner.sleep (3)#Wait a bit for the new page to get ready  -   - #Touch on the "Add Note" menu entry by coordinate  -Device.touch ( -, -, monkeydevice.down_and_up) in   -Monkeyrunner.sleep (3)#Wait a bit for the new page to get ready

After entering the activity noteslist, the next thing to do is to open the Noteeditor activity to add a new diary. The way to enter the activity is to first click on the System menu button to bring up the Options menu, then the Touch menu item "ADD Note" will go to the target activity. Here 23 lines of the key method of the press the first parameter represents the key value, it has a number of options, such as the System menu key can be written as "Keycode_menu", can also be written as "menu", can even be written as the real key value "82". In the future, we will see why this is the case in the analysis of the principle, here first sell a Xiaoguanzi. Then we see that the second parameter of the press is the case action described earlier in this section, which is Down_and_up, which represents a normal push button and then releases the key action. When you open the menu option, you need to simulate a touch event to click on the "ADD Note" menu item, which is called Monkeydevice's touch Touch method, and the first two parameters represent the absolute coordinates of the screen that needs to be touched. We can find this coordinate value through the Uiautomator Viewer tool, which we have already described in the previous chapter, here we are not exhausted, we do not know what to do please go back to the previous chapter to review. When you touch the menu item, the app opens the Noteeditor activity.

Sixth step: Enter new journal content

Code 3-2-6 increase Journal-Enter journal content

#Step 6: Type in the text for the note33 device.type("MyFirstNote")

Through the previous chapter of the study, we know that after entering the activity of Noteeditor, Android's soft keyboard will automatically pop out to facilitate users to enter the content of the diary. So the thing to do in this step is to call the Monkeydevice.type method to specify the contents of the journal, but also the title, written to the Noteeditor journal control, the final effect as shown.

Figure 3-2-4 adding diary content

Seventh Step: Save the new added diary

Code 3-2-7 add Journal-Save Journal

35#Step7: Save the note by touch on the "save" menu entry by coordinate36 device.press(‘KEYCODE_MENU‘37  38 MonkeyRunner.sleep(3)  #Wait a bit for the new page to get ready39  40 device.touch(200,750,MonkeyDevice.DOWN_AND_UP)

The process of saving the diary is the same as the process of opening noteeditor, which is to use the press method to simulate clicking on the System menu button and then using the touch touch method to touch the "save" option of the menu option to save the new journal.

Eighth Step: Delete Diary

Code 3-2-8 Deleting a journal

 42   #Step 8:touch on the" Delete "menu entry of the context menu options to delete the note  43  monkeyrunner.sleep (3 ) 44  Device.drag (240 , 120 ), (240 , 120 ), 3 , 1 ) 45  device.touch (84 , Span class= "Hljs-number" >450 , monkeydevice.down_and_up)  

After the diary is saved, the Notepad app automatically jumps back to the noteslist activity to show all the new additions to the diary. The above code is to delete the journal by long pressing the newly added journal on this page and selecting the "delete" option in the Popup context menu option. In this process, a monkeydevice drag method is used. The former face of drag this method of description we can know that its role is to simulate drag this action, the specified first two parameters are the drag start coordinates and the end of the coordinates, here we set the start and end coordinates are the same, and then the drag is often set to 3, Most of the parameters that represent the drag step are not set and there is no relationship here. Such a combination of settings is actually a long-press action, where the coordinates specified in fact is the new diary in the screen coordinates, so this represents the new diary to perform a long-click action. When you press and hold it, a context menu pops up, one of which is the deletion, and the 45th line touches the option by touching it to delete the journal.

Figure 3-2-5 Deleting a journal

Nineth Step: Uninstall the App

Code 3-2-9 uninstalling the app

47#Uninstall the application48 device.removePackage("com.example.android.notepad")             

The final step of the process is to call Monkeydevice's Removepackage method to remove the application that is represented by the package name specified by the parameter, and to restore the system to its pre-test state.

With the previous example we have basically gone through the usual methods of monkeydevice, and below we will be familiar with the use of the two methods of GetProperty and getpropertylist by another simple example.

The purpose of this example is to list all the supported system environment variable attributes through Getpropertylist and then print out the values of each environment variable by GetProperty. Because the code is not many, the entire code is listed here and parsed accordingly.

Code 3-2-10 getting the list of environment variables and values

  1  fromCom.android.monkeyrunnerImportMonkeyrunner2   3device = Monkeyrunner.waitforconnection ()4   5Varlist = Device.getpropertylist ()6 assertLen (varlist) >0  7 Print "Property and Value List:"  8 Print "--------------------------------------------------"  9i =1;Ten  forVarinchVarlist: One     PrintI"\ T", var +"\t\t\t:"+ Device.getproperty (Var) Ai + =1

The previous section we said Monkeydevice Getsystemproperty and GetProperty is not the same, although the official website said they are the same thing, but Getsystemproperty is for developers to use. But after our practice, as in the previous section, we can get the serial number "ht21atd05099" of the test machine according to the parameter "Ro.serialno" by calling Getsystemproperty. But from the output you can see that getpropertlist lists all of the supported environment variable lists in fact there is no "Ro.serialno" This item, there is no "ht21atd05099" the value of this serial number is listed. As for their respective implementation principles, our Future Analysis chapters will be described in detail. We return the above code, which is called Getpropertylist on line 5th to list all of the system-supported environment variables, and then 10-12 rows loop out each environment variable name and call it as a parameter GetProperty method to get the value of the environment variable. Finally format the output to the screen. Please see the output results.

Figure 3-2-6 Getting the environment variable list and value output results

——— to Be Continued ———

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

3rd Chapter 2 "Monkeyrunner Source Analysis" Scripting Example: Monkeydevice 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.