App automation, why does Appium use UiAutomator2?


1. What is UiAutomator2

Maybe a lot of people don't know the difference between UiAutomator2 and UiAutomator

UiAutomator is a UI automation testing tool developed by Google that runs on Android devices. It is based on the JAVA language. There is a limitation in using it, that is, it must be packaged into APK or JAR, and then uploaded to the device to run.

In fact, UiAutomator2 also has JAVA and Python versions, today we are talking about the Python version of UiAutomator2

As for the JAVA version, you can refer to the article written before: click me to view

Python version of UiAutomator2 project address:

2.Appium and UiAutomator2

As the originator of mobile automation, earlier versions of Appium were based on UiAutomator and Bootstrap.jar

Among them, Bootstrap is pushed to the Android device when Appium is initialized, and is responsible for monitoring the request sent by Appium, and after conversion, it is sent to UiAutomator for processing to complete the automation operation.

The latest version of Appium has added support for UiAutomator2, the principle has been updated, and the function and stability are more perfect

The schematic diagram can refer to:

3. Prepare

Before using UiAutomator2, you need to do the following preparations

1. Configure the Android development environment on the PC side

2. Use pip to install uiautomator2 dependencies

#Install dependencies
pip3 install -U uiautomator2

#If you need to take screenshots, you need to install pillow
pip3 install pillow

3. Install the atx-agent application on your phone

ps: atx-agent as a server, always running in the background

#Install the apk service on the phone
python -m uiautomator2 init

4. Install the editor

WEditor connects the mobile phone through ip, you can view the interface element information of the App in real time

Similar to Appium DeskTop, it can simulate click, slide operation, generate operation source code and other functions

First, install the editor dependencies via pip

#Browser-based viewing of App interface elements
pip3 install -U weditor

Then, enter weditor on the command line, it will be automatically opened in the browser, and then connect to the corresponding device through ip, you can get the control information of the current interface on the device side

The information content includes: the hierarchical relationship of the control, the control ID, the text content, the coordinate value, etc.

4. Let's fight

Let's take Xianyu search for products as an example, let's talk about the use of UiAutomator2

1. Connect the device

There are 3 ways to use UiAutomator2 to connect devices, they are:

  • LAN device IP address

  • USB connection + device serial number

  • ADB + IP + port number

import uiautomator2 as u2

# Method 1: ip address of the LAN device
device = u2.connect(cell phone ip address)

# Method 2: USB + device serial number
device = u2.connect(Mobile phone serial number)

# Method 3: ADB+
# First, connect the device to the PC with a USB cable, enter the command: adb tcpip port number to map
# Unplug the USB cable and connect by ip address + port number
device = u2.connect_adb_wifi(cell phone ip address:The port number)

2. Open Xianyu APP

Call the app_start() method in the device object above, and pass the package name of the application as a parameter to open the application

It should be noted that if the second parameter in the method is passed in True, the App can be cold started, and the default value is False

# Open the app
device.app_start(PACKAGE_NAME, stop=True)

3. Click the search bar to enter the search interface

First, set an implicit wait globally to avoid exceptions caused by stuck and network when searching for elements

#Implicitly wait for 20s to ensure that the control is loaded

Then, navigate to the basic information of the search entry control through WEditor

There are 6 commonly used UiAutomator2 positioning methods, namely:

  • ID targeting

  • Text text positioning

  • Description Targeting

  • ClassName targeting

  • Xpath positioning

  • Combination targeting


# 6 commonly used positioning methods
# Method 1: ID positioning
d(resourceId=element ID).click()

# Method 2: Text text positioning
d(text="No public: AirPython").click()

# Method 3: Description value positioning

# Method 4: ClassName positioning

# Method 5: Xpath positioning

# Method 6: Combined positioning
d(className="android.widget.ListView", resourceId=element ID)

It should be pointed out that when the interface attribute value is not unique, combined positioning is very practical

In this example, the ID is directly used to find the element, and then the click operation is performed to jump to the search interface

# Click to search page

4. Search

The send_keys() method is provided in UiAutomator2 to set text to the input box

Note: If the parameter clear is set to True, the input box will be cleared before entering the content. The default value is False

# input
device.send_keys("Python", clear=True)

# Click the search button

5. Swipe

UiAutomator2 provides two methods for sliding the interface, namely:

  • swipe_ext (swipe direction)

  • swipe( start x axis, start y axis, end x axis, end y axis value, swipe time )

After testing, it is found that the effect of swipe_ext() is unstable in sliding operation. It is recommended to use the swipe() function

for i in range(5):
    print('swipe once')
    swipe_custom(device, 0.5, 0.8, 0.5, 0.2, 1.2)

In addition, in order to ensure compatibility with devices with different resolutions, it is recommended to customize the sliding method through the 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, higher adaptability
    :param device:
    :param start_x_percent:
    :param start_y_percent:
    :param end_x_percent:
    :param end_y_percent:
    :param during:
    # Get the width and height of the screen
    width, height = device.window_size()
    device.swipe(start_x_percent * width, start_y_percent * height, end_x_percent * width, end_y_percent * height,

    if interval > 0:

6. Close the application

After completing the automated operation, you can call the app_stop() method to force the app to close

# stop App

Of course, you can use the method app_clear() provided by UiAutomator2 to clear App data after each operation

# Clear App Data
# device.app_clear(PACKAGE_NAME)

5. Finally

Through the above examples, we found that compared with Appium, UiAutomator2 has a more concise and easy-to-understand syntax and a lot less code.

However, since Uiautomator2 is only applicable to the Android side, Appium has multi-language and cross-platform features, and enterprise-level automation generally chooses the latter

I have uploaded all the source code in the article to the background, follow the public account "AirPython" and reply "uiauto2" to get all the source code

If you think the article is not bad, please like, share and leave a message, because this will be the strongest motivation for me to continue to output more high-quality articles!

Recommended reading

Take you to realize automatic group control with Python (Introduction)

Let’s talk about the automation of WeChat applet in Python, those pits that have been stepped on?

Python automation, why does Helium replace Selenium?

Posted by roflpwnt on Fri, 20 May 2022 18:34:04 +0300