So far, the monkeyrunner that we describe, such as click-and-drag-and-drop for an app, are implemented directly by specifying a coordinate point, such as a script example that touches a button with a coordinate point (60,90):
1device.touch (60,900,MONKEYDEVICE.DOWN_AND_UP)
code 14-1-1 using coordinate points to operate the application directly
In this case, there are several major drawbacks to the code:
- Lack of ease of use: Before you can manipulate a control, you need to find a way to locate the control's coordinate point as a tool
- Poor scalability: When the screen resolution changes, you need to write a common algorithm to deal with the change of coordinate points.
- Poor readability: The code is everywhere the X, Y value of the coordinate point, making people see headaches. It's like using a Java programmer to see the assembly code.
- Poor reusability: When the screen resolution is changed, if you do not provide a general algorithm to automatically calculate the new coordinate points, you need to provide different x, Y coordinates to test the different resolutions
Since there are so many shortcomings in the direct use of coordinate points, is there a better way to eliminate these problems? The answer is yes, the hierarchyviewer described in this chapter is dedicated to solving these problems. When a user needs to manipulate a control, it is no longer positioned directly through the coordinate point, but instead can be obtained through the control ID and then positioned through the control's coordinate properties. In this process, the user does not need to know the coordinate value of the control, because it is encapsulated in the properties of the control, is from the target Android device to get real-time, so even if the test machine, the screen resolution is not affected, the code will continue to run.
In the above example, if the button's ID is "Id/button", then the script code for clicking on it by getting the control will look something like this:
1viewer = Device.gethierarchyviewer () 2 view = Viewer.findviewbyid ("Id/button") 3p = Viewer.getabsolutecenterofview ( View) 4device.touch (P.X,P.Y,MONKEYDEVICE.DOWN_AND_UP)
code 14-1-2 manipulating applications through controls
You can see that the user does not need to be concerned about changes in control coordinates caused by changes in screen resolution. So it can be said that Hierarchyviewer makes Monkeyrunner look closer to a control-oriented UI Automation testing framework.
Of course, in addition to providing the ability to locate a control based on the ID, Hierarchyviewer provides other features, such as getting the Text property of the control based on the control ID, which we will analyze down.
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.
14th Chapter 1 "Monkeyrunner Source Analysis" Hierarchyviewer implementation principle-oriented control programming vs. coordinate programming