Ten minutes to understand the fastest APP automation tool uiautomator

I believe that many people who have done APP automation with appium are deeply touched:

  • Appium runs slowly and takes a long time
  • When uiautomatorviewer locates elements, you have to turn off appium server
  • Getting toast on a lower version of appium requires switching automationName

Now there is an automated 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 quickly master it within ten minutes

1. Introduction

uiautomator2 is an upgraded version of uiautomator, which is a Java library developed by Google for Android automation testing. uiautomator2 encapsulates the interface in uiautomator into a Python library. So the language it supports is Python

The main advantages are summarized as follows: 

  • Environment setup is simple and convenient
  • Execution speed is fast
  • UI element positioning is convenient, easy to use, and visualized well
  • Getting toast is very simple

According to the official documentation, it is required to:

  • Android version 4.4+
  • Python version 3.6+

2. Environment deployment

1. Install adb and configure environment variables

I won't go into details here, there are many tutorials on the Internet. After the installation, check the version information. If the specific version number appears, the installation is successful.

2. Install python's uiautomator2 library 

Order:

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 a server. Therefore, you must first ensure that adb devices can see the connection on the emulator. Open a night god simulator here as a demonstration

Start installing atx-agent, command:

python-m uiautomator2 init

After the installation is successful, you can see a car icon on the emulator desktop, this is atx.apk

4. Install weditor

Order:

pip install -U weditor

Weditor is a browser-based UI element viewer. After the installation is successful, we will make a demonstration

Entering editor on the command line will automatically open the browser. Enter the device serial number of the simulator on the page, click Connect, and then click Dump Hierarchy to refresh. You can see the simulator interface.

Open Baidu Tieba, a privacy policy pop-up window will pop up, click the refresh button on the UI viewer (of course you can also turn on real-time, which will automatically refresh), select 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 weditor UI viewer at the same time when it is running

3. Write scripts for the homepage of Baidu Tieba

Here we go step by step to illustrate some of the methods that the uiautomator2 tool provides us by writing use cases for Baidu Tieba automated testing. In order to make the code more intuitive, the PO mode is not used here, and each step is directly written in a module

1. First create a project u2-autotest, and then create a module test_tieba.py under it

2. Import the uiautomator2 library, give it the alias u2, and then use u2's connect_usb() method to connect. The parameter of this method is the device serial number (can be obtained through adb devices). The device object returned after the connection is successful, we named it 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 then print d.info, the currentPackageName in the result is the package name

4. Then click Baidu Tieba . The first page that appears is the privacy policy page. Open the editor UI viewer. After refreshing, 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 The type is xpath, here select resourceId

The positioning method of resourceId in uiautomator2 is as follows:

d(resourceId="element positioning expression")

But let's think about it, can we write directly like this

The answer is no, because the element control will not appear immediately after starting the app, so waiting time is required. In uiautomator2, you can use time.sleep() to force the wait, or you can use implicitly_wait(), which is the same as appium and selenium. are consistent.

Syntax for implicit wait: 

d.implicitly_wait (waiting time) #unit is s

Organize the code, add the implicit wait time of 10s, the implicit wait only needs to be added once, and it is universal.

Regardless of which element you are waiting for, the waiting time is always 10s. However, if the first element appears in 2s and the second element appears in 5s, then the actual waiting time for the first element is only 2s and the second element is only 5s. When it appears, the next step will be performed, instead of waiting for the set time like forced waiting. Therefore, implicit waiting is relatively time-saving
 

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

Swipe screen operation is the swipe() method in uiautomator2, similar to appium, sx, ex and sy, ey represent the coordinates of the starting point and end point respectively

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

So how to get 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 to get the screen size is:

d.window_size()

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

  • In order to improve the stability of screen sliding, you need to add mandatory waiting, wait 1s
  • The sliding distance should be as large as possible. For example, the starting and ending points of the x-axis can be set to 0.9 and 0.1. If you set them to 0.9 and 0.5, the sliding distance is likely to be too small, resulting in no sliding.
  • uiautomator2 also provides an extended sliding function, swipe_ext(), which does not require coordinates, just provide directions such as "left", "right", "up", "down". After experimenting, I feel that it is not stable, so I still use the conventional method

6. Swipe to the last page , click Experience Now, you can enter the home page

Here you can use resourceId or description or xpath, here is a demonstration of the use of description.

The syntax for description positioning in uiautomator2 is:

d(description="element positioning expression")

description is also called "content-desc", which is generally unique

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

Seeing that the recommendation has a text value, does uiautomator2 support text as an element positioning method?

OK

d(text="element positioning expression")

Then you can use the get_text() method to get the text value, and use assert to assert

8. After a use case runs, it is necessary to stop the app and clean up the environment. Here, use the app_stop() and app_clear() methods

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 omitted a lot, such as PO mode, pytest, log, report, etc.

Four, uiautomator2 and appium running speed comparison

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

Facts have proved that under the same circumstances, uiautomator2 takes about 1 minute, while appium takes 1 minute and 16s, which is about 16s slower than uiautomator2. Of course, the more use cases and the longer the process, the more obvious the effect 

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

Guess you like

Origin blog.csdn.net/okcross0/article/details/125451648#comments_28543680