Ten minutes to understand the fastest app automation tool uiautomator2


I believe many useappiumPeople who have done app automation have deep feelings:

  • Appium runs slowly and takes a long time
  • Uiautomatorviewer has to turn off appium server when locating elements
  • To get toast on a lower version of appium, you need to switch the automationname

Now there is oneautomated testing Tool uiautomator2, which almost perfectly avoids the above problems. Easy to learn and friendly enough for novices. This time we introduce this tool, so that you can master it quickly in ten minutes

1、 Introduction

Uiautomator2 is an upgraded version of uiautomator, a Java library developed by Google for Android automation testing, and uiautomator2 encapsulates the interface in uiautomator into a python library. Therefore, the language it supports is python

The main advantages are summarized as follows: 

  • The environment is simple and convenient
  • Fast execution
  • UI elements are easy to locate, easy to use and visualized
  • Getting toast is very simple

According to the official documents, it is required that:

  • Andorid version 4.4+
  • Python version 3.6+

2、 Environment deployment

1. Install ADB and configure environment variables

Not detailed here, there are many online tutorials. After installation, check the version information, and the specific version number will appear, indicating that the installation is successful


2. Install Python’s uiautomator2 Library


pip install -U uiautomator2

3. Install ATX agent

The purpose of this step is to download ATX agent from GitHub and push it to the mobile phone as the server. Therefore, it is necessary to ensure that ADB devices can see the connection on the simulator. Here is a night God simulator for demonstration

Start installing ATX agent, command:

python-m uiautomator2 init

After successful installation, you can see a car icon on the simulator desktop, which is ATX apk

4. Install weditor


pip install -U weditor

Webitor is a browser based UI element viewer. We will make a demonstration after successful installation


Enter webitor on the command line to automatically open the browser, enter the device serial number of the simulator on the page, click Connect, and then click dump hierarchy refresh to see the interface of the simulator

Open Baidu Post Bar, pop up the privacy policy pop-up window, click the refresh button on the UI viewer (of course, you can also open real-time, which will refresh automatically), select and agree with the mouse, and you can see some element positioning methods on the right, such as resourceid, XPath, etc. Is it very convenient? The most convenient thing is that it can open the webitor UI viewer at the same time when it is running

3、 Write Baidu Post Bar Home page script

Here, step by step, we write the test cases of Baidu Post Bar automation test to illustrate some methods provided by uiautomator2 tool. In order to make the code more intuitive, Po mode is not used here. Each step is directly written in a module

1. First create a project U2 autotest, and then create a module test under it_ tieba. py

2. Import the uiautomator2 library, take an alias U2, and then use U2’s connect_ USB () method connection. The parameter of this method is the device serial number (which can be obtained through ADB devices). The device object returned after successful connection is named D. how to check whether it is connected? Use d.info to get the basic information of the device, indicating that the connection has been successful

3. Start the specified app. The syntax is:

d.app_ Start (“specified package name”)

How to get the package name? You can open the app and print d.info. The currentpackagename in the result is the package name

4. Then click Baidu Post Bar, the first page that appears is the privacy policy page. Open the editor UI viewer, refresh and move the mouse to the Agree button. You can see that there are two positioning methods to choose from, one is resourceid and the other is XPath. Select resourceid here

In uiautomator2, the location of resourceid is as follows:

D (resourceid = "element positioning expression")

But let’s see if we can write it like this

The answer is no, because the element control will not appear immediately after starting the app, so you need to wait. In uiautomator2, you can use forced wait time Sleep() can also use implicit wait_ Wait(), which is consistent with appium and selenium.

Syntax of implicit waiting:

d.implicitly_ Wait (waiting time) # unit: S

Sort out the code and add the hidden waiting time of 10s. The hidden waiting only needs to be added once, which is universal in the whole world.

No matter how many elements are waiting, the waiting time is 10s. However, if the first element appears in 2S and the second element appears in 5S, the actual waiting time of the first element is only 2S and the second element is only 5S. When the element appears, the next operation will be carried out, instead of waiting for the set time. Therefore, hidden waiting is relatively time-saving

5. After clicking agree, we enter the sliding screen page

Sliding screen operation is the swipe () method in uiautomator2. Similar to appium, SX, ex and sy, ey represent the coordinates of the starting point and the ending point respectively

d.swipe(sx, sy, ex, ey, 0.5)

How to obtain the coordinates of the starting point and the ending point?

In order to adapt to different resolutions, the size of the screen can be obtained, and then multiplied by coefficients respectively.

The syntax for obtaining the screen size is:


We see that the slide screen of the home page needs to slide to the left twice, so we can write a for loop. Here are some points to note:

  • In order to improve the stability of screen sliding, it is necessary to add forced waiting for 1s
  • The sliding distance should be as large as possible. For example, the coefficients of the starting and ending points of the x-axis can be set to 0.9 and 0.1 respectively. If you set it to 0.9,0.5, it is likely that the sliding distance is too small, resulting in no sliding
  • Uiautomator2 also provides an extended sliding function, swipe_ Ext() does not need coordinates, but only provides directions, such as “left”, “right”, “up”, “down”. After the test, I feel unstable, so I still use the conventional method

6. Slide to last page, click experience now to enter the home page


Here, you can use resourceid or description or XPath. Here is a demonstration of the use of description.

In uiautomator2, the syntax of description location is:

D (description = "element positioning expression")

Description, also known as “content description”, is generally unique

7. On the home page, you can see that there are four columns in the upper left corner: attention, recommendation, topic and live broadcast. We choose recommendation as the element of assertion

See the recommended text value. Does uiautomator2 support text as an element positioning method?


D (text = "element positioning expression")

You can then use get_ The text () method gets the text value and asserts it with assert

8. After a use case runs, you need to stop the app and clean up the environment. Here, use app_ Stop () and app_ Clear() method

d.app_ Stop ('specified package name ')

d.app_ Clear ('specified package name ')

At this time, we have finished writing a complete use case. Of course, for the sake of demonstration, I have omitted a lot, such as po mode, pytest, log, report and so on

4、 Comparison of running speed between uiautomator2 and appium

Here, I changed the code and used uiautomator2 and appium to execute the use case of Baidu Post Bar search at the same time. The steps are the same and the waiting time is the same. Let’s see which automation tool is faster under the same circumstances

It has been proved that in the same case, uiautomator2 takes about one minute, while appium takes one minute and 16S, which is about 16S slower than uiautomator2. Of course, the more use cases, the longer the process, the more obvious the effect

Finally, the main syntax of uiautomator2 is summarized. For more functions, please read the official documents: http://8rr.co/Ngbk

Now there is such a chance. I invite you to click to enter our websiteSoftware testing learning fish schoolCheck the bulletin board on the right. You can discuss and exchange software testing together, learn software testing technology, interview and other aspects of software testing together, learn document resources and gain more testing skills. Let’s advance Python automated testing / test development and go to the road of high salary.

Let’s give you a word of encouragement: when our ability is insufficient, the first thing to do is internal practice! When our ability is strong enough, we can look outside! 

Recommended Today

Use kubeeye to escort your k8s cluster

Author: kaliarch (Xue Lei), product leader of a cloud MSP service provider, familiar with enterprise level high availability / high concurrency architecture, including hybrid Cloud Architecture and remote disaster, proficient in enterprise Devops transformation and optimization, familiar with shell / Python / go and other development languages, familiar with kubernetes, docker, cloud native, micro service […]