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

Source: Internet
Author: User
Tags touch command

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 first "add Diary" example in the previous section, we did not see whether the diary actually increased successfully, that is to say, there was no result comparison. In fact, in the Monkeyrunner framework, the comparison of test results is usually done by the screen-cutting ratio. The method of screen-cutting is mainly implemented by Monkeyimage, but this action is monkeydevice provided by the Takesnapshot method to achieve. So in this section we are primarily familiar with the basics of screenshots compared to the relevant test code.

Before we start, let's take a look at the section we'll use to compare the screenshots that are already in their class.

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

Let's look at a sample code to see how these methods can be used to compare test results. The example continues with the 7th step of the previous section, "New Diary", to add a journal to start, and then to modify the contents of the journal, and then compare the changes before and after the two of the diary content of the screenshot to see whether it is consistent. Because the expectation is inconsistent, so if the same will mean that the error, then will be modified before and after two pictures saved to the desktop for viewing.

Let's take a look at the Notepad after the seventh step of "new Diary" in the previous section:?

Figure 3-3-1 The current status of the journal

Eighth step: Click the journal title to enter the journal modification page Noteeditor

Code 3-3-1 opening the current journal

# Step 8:open The note monkeyrunner.sleep (3device.touch (240,120  46 Monkeyrunner.sleep (3)  #Wait a bit for the new page to get ready

Through the lower right corner, we can know where the coordinates of the current diary are, so that we can get the effective click coordinates within that boundary, for example, here we use (240,120) This coordinate point. Once you have the effective click coordinates for the journal, you can send a touch command touch on line 44 to simulate the click of the journal. Once clicked, Notepad will open the journal and jump to the Noteeditor page to allow the user to edit the contents of the journal.

Nineth step: Get the journal original content control sub-diagram

After jumping to the journal editing page, we need to get the screenshot of the control that loaded the journal content, and the screenshot needs to be taken in two steps: The first step is to screen the entire display first, and the second step is to determine the bounds of the content control on the screen. It then obtains the sub-graph within the bounds range. Gives the bounds of the journaling content control obtained through the Uiautoatorviewer tool:

Figure 3-3-2 Original diary content and control bounds

Code 3-3-2 get Journal content sub-diagram

# Step 9:take A snapshot for the current note content Imgorigin =subimgorigin = Imgorigin.getsubima GE ((0,76,480,391))

First, the whole screen is obtained through the Takesnapshot method of the Monkeydevice of line 49th, and then on the basis of that, line 50th can get to the diary content sub-graph according to the range of the diary content that we determined by the Uiautomatorviewer tool above. With this sub-chart, we can compare the content with the modified diary, and see if the changes are successful.

Tenth step: Modify the journal contents and save

Code 3-3-3 Modify the contents of the journal and Save

# Step 10:add Something to the content of the note and save it Device.type ("yourfirstnote  "  device.press ('keycode_menu'  57 Monkeyrunner.sleep (3)  #Wait a bit for the new page to get ready  Device.touch (150,65 0,MONKEYDEVICE.DOWN_AND_UP)

Several key methods of code we have already learned in the previous section, and we do not make a statement here. One thing to mention is that when you press the System menu key after modifying the content, the effective click coordinates of the Save menu item can also be obtained by Uiautomatorviewer, just by obtaining the bounds of the control, and then identifying a boundary that falls on it, as shown in the boundary:

Figure 3-3-3 Save menu item coordinates

11th Step: Reopen the journal content and get the sub-image

Code 3-3-4 reopen the journal content and get the child map

# Step 11:open The note again and take another snapshot monkeyrunner.sleep (3Device.touch (240,120
   
      Monkeyrunner.sleep (3)  
    #
    Wait a bit for the new page to get the ready  imgnew =
    subimgnew = Imgnew.getsubimage ((0,78,480,391))
   

After saving the newly added journal content in the previous step, the Notepad app will automatically return to the activity interface of noteslist to list all the diaries, with only one diary in our example. The above code 63 line is directly click on the journal, and then go to the Noteedtor page to display the diary content, and finally in 69-68 row to get to the diary content of the sub-diagram, the entire process and the eighth step above the Nineth step is the same, but here does not open the diary and get the sub-map into two steps to complete it. Displays the contents of the Open modified journal and the bounds of the content sub-graph.

Figure 3-3-4 Journal content and control boundaries after modification

12th Step, compare the contents of the diary before and after the modification

Code 3-3-5 Compare journal changes before and after content and save

70#Step 12:compare The imagesSubimgnew.sameas res = (subimgorigin,1) 72 73#assert res = = True74 75if  notres = =True:Subimgnew.writetofile ("/users/apple/desktop/subimgnew.png",                        "PNG") Subimgorigin.writetofile ("/users/apple/desktop/subimgorg.png",                        "PNG")

After the previous steps to get the diary content modification before and after, you can use 71 lines of Monkeyimage Sameas method to compare the content of two. The second parameter is set to 1, representing two kinds of graphs that would be considered consistent if they were identical. What we expect here is that the content of the two graphs is inconsistent because one content has not been modified and one has been modified. So in the 75 Guild judge if the return results that the two graphs are consistent, you need to save the two diagram, I am here to save the desktop, so that we can manually check whether the two diagram is really the same, or our code in addition to the problem.

——— to Be Continued ———

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

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