1. What is UiAutomator2
A lot of people probably don’t know the difference between UiAutomator2 and UiAutomator
UiAutomator is a UI automated test tool developed by Google that runs on Android devices. It is based on JAVA language. There is a limitation in using UiAutomator, that is, it must be packaged into APK or JAR and uploaded to the device before it can run
In fact, UiAutomator2 is also available in JAVA and Python, and today we’re talking about the Python version of UiAutomator2
For the JAVA version, please refer to the previous article: click me to view
UiAutomator2 project address:
Github.com/openatx/uia…
2. Appium and UiAutomator2
As the granddaddy of mobile automation, earlier versions of Appium were based on UiAutomator and bootstrap.jar
During the initialization of Appium, Bootstrap is pushed to the Android device to listen to the request sent by Appium and send it to UiAutomator for processing after conversion to complete automatic operation
The latest version of Appium has added support for UiAutomator2, with updated principles and improved functionality and stability
Schematic diagram can refer to:
3. Prepare
Before using UiAutomator2, you need to do the following
1. Configure the Android development environment on the PC
Install uiAutomator2 dependencies using PIP
If you need to take screenshots, you need to install pillow pip3 Install PillowCopy the code
3. Install atX-Agent on your phone
Ps: AtX-Agent runs in the background as a server
-m uiautomator2 initCopy the code
4. Install the Weditor
The WEditor connects to your phone via IP, allowing you to view interface elements of your App in real time
And Appium DeskTop similar, can simulate click, slide operation, generate operation source code and other functions
First, install the Weditor dependencies through PIP
Pip3 Install -u weditor pip3 Install -u weditorCopy the code
Then, enter weditor on the command line, and it will be automatically opened in the browser. Then, connect to the corresponding device through IP, that is, you can obtain the control information of the current interface on the device
Information content includes: control hierarchy, control ID, text content, coordinate value and other content
4. Get real
Let’s talk about the use of UiAutomator2
1. Connect the device
There are three ways to connect to a device using UiAutomator2:
-
IP address of a LAN device
-
USB connection + device serial number
-
ADB + IP + port number
Import UIautomator2 as U2 device = U2.connect (mobile IP address) First, connect the device to the PC using a USB cable, and enter the command: Adb tcpIP port mapping # Unplug USB cable, connect via IP address + port number device = U2.connect_ADb_wifi (mobile IP address: port number)Copy the code
2. Open xianyu APP
Call the app_start() method on the device object above, passing in the package name of the application as an argument to open the application
Note that the second argument in the method, if passed True, cold launches the App, which defaults to False
# Start device.app_start(PACKAGE_NAME, stop=True)Copy the code
3. Click the search bar to enter the search interface
First, set an implicit wait globally to ensure that elements are found without exceptions due to lag and network
# ImplICITly_wait (20)Copy the code
Then, the WEditor locates basic information about the search entry control
There are six commonly used UiAutomator2 positioning modes, which are as follows:
-
ID positioning
-
Text Text location
-
The Description positioning
-
The ClassName positioning
-
Xpath positioning
-
positioning
Such as:
ID ID (resourceId= element ID).click() # ID ID (resourceId= element ID).click() # ID ID (resourceId= element ID).click() D (Description ="AirPython").click() D (ClassName =" android.widget.textView ").click() D. Xpath ("//*[@content-desc='AirPython']") {className=" android.widget.listview ", resourceId= element ID)Copy the code
It should be noted that combined positioning is useful when the interface attribute values are not unique
This example uses the ID directly to find the element, and then performs the click operation to jump to the search screen
# click to search page device (resourceId = "com. Taobao. Idlefish: id/search_bg_img_front",). Click ()Copy the code
4, search,
The send_keys() method is provided in UiAutomator2 to set text to the input box
Note: The clear argument, if set to True, clears the input field before entering anything. The default value is False
Send_keys ("Python", clear=True) # Click search button device(text=" search ").click()Copy the code
5, sliding
UiAutomator2 provides two methods for sliding the interface:
-
Swipe_ext (slide direction)
-
Swipe (Start X axis, start Y axis, end X axis, end Y value, sliding time)
Swipe () is recommended because swipe_ext() is unstable
For I in range(5): print(' swipe_custom ') swipe_custom(Device, 0.5, 0.8, 0.5, 0.2, 1.2)Copy the code
In addition, to ensure compatibility with devices of different resolutions, you are advised to customize the sliding method by screen percentage
Def swipe_custom(device, start_x_percent, start_y_percent, end_x_percent, end_y_percent, during=1.0, interval=1): "" Custom sliding for higher adaptability :param device: :param start_x_percent: :param start_y_percent: :param end_X_percent: :param end_y_percent: :param during: :return: Width, height = device.window_size() device.swipe(start_x_percent * width, start_y_percent * height, end_x_percent * width, end_y_percent * height, during) if interval > 0: sleep(interval)Copy the code
6. Close the application
After the automation is complete, the app_stop() method can be called to force the application to be shut down
# Stop App device.app_stop(PACKAGE_NAME)Copy the code
Of course, you can use the method app_clear() provided by UiAutomator2 to clear App data after each operation
# device.app_clear(PACKAGE_NAME)Copy the code
5. The last
Through the above example, we find that UiAutomator2 has more concise syntax and less code than Appium
However, since Uiautomator2 is only applicable to Android, Appium has multi-language, cross-platform features, and enterprise automation generally chooses the latter
I have uploaded all the source code to the background, reply “UIauto2” can get all the source code
If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!
Recommended reading
Automatic Teamwork control with Python
Talk about Python to do wechat applet automation, those who step on the pit?
Why should Helium replace Selenium in Python Automation?