App automation, why does appium use uiautomator2?

Time:2021-8-10

App automation, why does appium use uiautomator2?

1. What is uiautomator2

Maybe a lot of people are right   UiAutomator2   and   UiAutomator   Silly can’t tell

UiAutomator   It is a UI automation test tool developed by Google and running on Android devices. It is based on Java language. There is a limitation to using it, that is, it must be packaged into APK or jar and then uploaded to the device before it can run

In fact, uiautomator2   Also have   Java and python versions. Today we are talking about the python version of uiautomator2

For the Java version, please refer to the previous article:Click me to check

Uiautomator2 project address for Python version:

https://github.com/openatx/ui…

2. Appium and   UiAutomator2

As the ancestor of mobile terminal automation, the early version of appium is based on uiautomator   And bootstrap.jar

During appium initialization, the bootstrap is pushed to the Android device, which is responsible for listening to the requests sent by appium and sending them to uiautomator for processing after conversion to complete the automation operation

The latest version of appium adds support for uiautomator2, updates the principle, and improves the function and stability

Schematic diagram can refer to:

App automation, why does appium use uiautomator2?

3. Preparation

in use   Before uiautomator2, you need to make the following preparations

1. Configure Android development environment on PC

2. Installing using pip   uiautomator2   rely on

#   Installation dependency
pip3 install -U uiautomator2

#   If you need a screenshot, you need to install pilot
pip3 install pillow

3. Install on phone   atx-agent   application

PS: as a server, ATX agent always runs in the background

#   Install APK service on mobile phone
python -m uiautomator2 init

4. Install WebEditor

WEditor   Connect the mobile phone through IP, that is, 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 webiter dependency package through pip

#   Browser based viewing   App   Interface elements
pip3 install -U weditor

Then, enter webitor on the command line and it will automatically open in the browser. Then connect the corresponding device through IP, that is, you can obtain the control information of the current interface on the device side

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

App automation, why does appium use uiautomator2?

4. Practice

Or take the free fish search product as an example to talk about the use of uiautomator2

1. Connecting equipment

Connecting devices using uiautomator2   three   Two methods are:

  • LAN device IP address
  • USB connection+   Equipment serial number
  • ADB  + IP + port number
import uiautomator2 as u2

#Method 1: LAN device IP address
Device = u2.connect (mobile IP address)

#Mode 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 and input the command: ADB TCPIP port number for mapping
#Unplug the USB cable and connect through IP address + port number
device = u2.connect_ adb_ WiFi (mobile IP address: port number)

2. Open the free fish app

Call above   Device object   app_ start()   Method, the package name passed in to the application can be used 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 app
device.app_start(PACKAGE_NAME, stop=True)

3. Click the search bar to enter the search interface

App automation, why does appium use uiautomator2?

First, an implicit wait is set globally to ensure that exceptions caused by jamming and network are avoided when looking for elements

#   Implicitly wait for 20s to ensure that the control loading is completed
device.implicitly_wait(20)

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

App automation, why does appium use uiautomator2?

frequently-used   Uiautomator2 positioning modes are   six   Species are:

  • ID location
  • Text positioning
  • Description location
  • Classname positioning
  • XPath positioning
  • Combined positioning

For example:

#6 common positioning methods
#Method 1: ID positioning
D (resourceid = element ID). Click()

#Mode 2: text positioning
D (text= "official account: AirPython").Click ()

#Method 3: description value positioning
d(description="AirPython").click()

#Method 4: classname positioning
d(className="android.widget.TextView").click()

#Mode 5: XPath positioning
d.xpath("//*[@content-desc='AirPython']")

#Mode 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, combination positioning is very practical

This example directly uses the ID to find the element, and then performs the click operation to jump to the search interface

#Click to the search page
device(resourceId="com.taobao.idlefish:id/search_bg_img_front",).click()

4. Search

Uiautomator2 provides   send_ keys()   Method to set text into an input box

Note: if the parameter clear is set to true, the input box will be cleared before entering content, and the default value is false

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

#Click the search button
Device (text = "search"). Click()

5. Sliding

Uiautomator2 provides two methods for sliding the interface, namely:

  • swipe_ Ext (sliding direction)
  • Swipe (start x-axis, start y-axis, end x-axis, end Y-axis value, sliding time)

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

for i in range(5):
    Print ('slide 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 for better adaptability
    :param device:
    :param start_x_percent:
    :param start_y_percent:
    :param end_x_percent:
    :param end_y_percent:
    :param during:
    :return:
    """
    #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,
                 during)

    if interval > 0:
        sleep(interval)

6. Close app

After the automation operation is completed, you can call   app_ stop()   Method to force the application to close

#Stop app
device.app_stop(PACKAGE_NAME)

Of course, you can use the method provided by uiautomator2 after each operation   app_ clear()   Clear app data

#Clear app data
# device.app_clear(PACKAGE_NAME)

5. Finally

Through the above example, we find that   Compared with appium, uiautomator2 has simpler syntax and less code

However, because uiautomator2 is only applicable to Android, appium has multi language and cross platform features, and the latter is generally selected for enterprise automation

I have uploaded all the source code in this article to the background, and the background replies ”  uiauto2 」 You can get all the source code

If you think the article is good, please  Like, share and leave messagesNext, because this will be the strongest driving force for me to continue to output more high-quality articles!

Recommended Today

A thorough understanding of the hystrix go source code

Series articles: Usage and principle of hystrix go A thorough understanding of the hystrix go source code Opening The last article mainly introducedhystrix-goIn this article, let’s comprehensively analyze the source code. This article is very long. Please read it patiently. In addition, because the direct release of the source code affects the mobile phone reading […]