Appium+python automation (22) - control coordinate acquisition (super detailed explanation)

Introduction

  Some friends or children’s shoes may be curious and ask how the coordinate points in the monkey script in the previous article were obtained. They were not guessed casually by themselves, or they were measured by their own eyes or a ruler. The answer Of course: NO. The method of obtaining the control coordinate point here is Hongge sharing and explaining three methods to the friends. I personally like the second method, and I also recommend the second method for friends and children’s shoes. Of course, radishes and vegetables have their own preferences. Make mandatory. Didn't Comrade Xiaoping say: It doesn't matter if a cat is a white cat or a black cat that catches mice, it is a good cat. The one that suits you is the best, and friends can choose according to their own preferences.

Get the coordinate point method of the control

The first method (MonkeyRecorder gets the coordinates)

    The following is a demonstration of how MonkeyRecorder obtains coordinates. MonkeyRecorder is a relatively easy-to-use tool for obtaining coordinates. It is a tool used to obtain coordinates on the real machine or simulator. When we click on the space on the real machine or the simulator, it can display the coordinates on the real machine or the simulator. The coordinates of the clicked point.

(1) Startup of MonkeyRecorder

1. First connect the Android phone to the computer, and ensure that the following two conditions are met:

a. The terminal USB is adjusted to developer mode

b. Install the mobile phone driver on the computer

2. After the mobile phone is successfully connected, open the cmd window and enter adb devices to view the name of the connected real device or simulator device. We still use the Yeshen simulator here as the representative. As shown in the picture:

3. In the cmd window, enter monkeyrunner and start Monkeyrunner. Do the following: import the MonkeyRecorder package, connect the simulator device, start the simulator in MonkeyRecorder mode, and enter in sequence

Command as follows:

1 from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
2 
3 from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
4 
5 device=MonkeyRunner.waitForConnection()
6 
7 recorder.start(device)

4. At this point, MonkeyRecorder is officially launched. Screenshot below.

(2) Use of MonkeyRecorder

1. We just use MonkeyRecorder to record the coordinates here, and the way to obtain the coordinates is very simple. For example, on the login interface of qq, if you click the "login" button, the coordinates of the button will be displayed on the right; similarly, if you click on the account input box or password input box, the coordinates will also be displayed on the right. This coordinate is what we need to get.

2. At the same time, the interface in MonkeyRecorder is consistent with that of the simulator page. If any operation is triggered in MonkeyRecorder, there will be a corresponding trigger on the simulator. If the two are not consistent, click Refresh Display in the upper right corner of MonkeyRecorder to refresh the page.

The second method (Uiautomatorviewer)

  Many recommended ways to obtain coordinate points on the Internet are to use the uiautomatorviewer tool provided in the android-sdk/tools directory. But this method is very cumbersome, and in fact, we don't need such precise coordinate points. After all, the control is so large, as long as the clicked coordinates fall on the position of the control, it will be fine.

Uiautomatorviewer is a tool that comes with the Android sdk, and its location is under sdk/tools.

Tool opening method:

1. Under sdk/tools, find uiautomatorviewer, double click to open

2. On the cmd command line, directly enter uiautomatorviewer to open the tool

Once the tool is open, we are ready to go.

Operating environment: Windows

Instructions:

1. Connect the simulator or real machine (we use the simulator here)

2. Open the app specified in the simulator, and take a screenshot of the specified page in uiautomatorviewer

3. After the screenshot is successful, the page is displayed as follows:

It should be noted that the api of your mobile device or emulator must be above 16, that is, the android version must be above 4.1, because this tool was launched by Google after 4.1, and it is only applicable to versions after 4.1

4. Result analysis

From the figure above, you can get the ID of the corresponding control and the coordinates of the upper left corner and lower right corner of the control.

1) For the coordinates of the control, it is very simple here, we generally take the coordinates of the midpoint, namely:

Midpoint coordinates = ((abscissa of upper left corner + abscissa of lower right corner)/2, (ordinate of upper left corner + ordinate of lower right corner)/2)

For example, in the figure above, the midpoint coordinates are ((30+1009)/2,(268+314)/2)=(519.5,291)

Note: Coordinates are generally rounded up.

From this, we can get the coordinates of the desired control. In the script, just touch the coordinates of the point directly.

2) For the control ID, that is, the part in the red box in the above figure, also in the script, just touch the ID directly.

In addition, using uiautomatorviewer can also obtain the relationship between parent and child controls, and the obtained relationship is consistent with HierarchyViewer.

The uiautomatorviewer is introduced here. 

The third method (Pointer location to obtain coordinates)

  Then I recommend another simpler way to obtain the coordinate point positioning method of the control on the device under test that you see currently. That is to take advantage of the developer options for Android devices.

Here is an example of the Night God Simulator:

1. Click Settings

2. Click Developer Options

3. At the pointer position, after enabling this property, when the finger slides on the screen, you can see the X and Y coordinates of the current touch point on the top, which is what we need.

Note: The developer option is not visible by default, you need to enable the developer mode yourself, and then perform this operation. (This is not something you can check on the Internet by yourself. If it is a Yeshen simulator, you can refer to the summary section of Brother Hong to enable the developer options)

summary

(1) There are many ways to enable the developer mode in the Android system. Our simulator is version 6.3.0.0. Here we only talk about the method suitable for the Yeshen simulator.
  Next, enter the graphic tutorial:

  1. Click Settings

1.png

  2. Click About Tablet

2.png

3. Click the version number 7 times (click 20 times if you are handicapped), click the version number pointed by the arrow (the bottom version number), not the Android version number above

3.png

 4. Go back to the settings menu

4.png

5.png

6.png

 One more point here, if you feel that the developer option is intrusive, you can choose to turn it off. How to turn it off:

  Settings--Application--All--Find the setting software, click to enter--Clear data
  so that the developer mode is hidden again~~

7.png

  This is the end of the tutorial O(∩_∩)O~~

2. Well, this is the end of today's acquisition of control coordinate points! Ha ha! Finally, I wish everyone can master it! ! !


              [The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled]


1. From entry to mastery of Python programming

2. Interface automation project actual combat

3. Actual Combat of Web Automation Project


4. Actual Combat of App Automation Project

5. Resume of first-tier manufacturers


6. Test and develop DevOps system

7. Commonly used automated testing tools


Eight, JMeter performance test

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131917571