4

1. Background

Since many companies adopt the agile development model, testing must be followed by agile testing. The cycle of each iteration is very short, and regression testing of the original functions is often performed, which increases a lot of repetitive labor costs. The introduction of UI automation testing can be used to quickly regression test the original functions of the app, and testers only need to pay attention to the testing of new functions. Secondly, most of the test cases for mobile apps are related to functional verification, which can be verified through UI operations, which provides convenient conditions for UI automation. Therefore, use cases with high test repetition and low execution efficiency can be quickly and repeatedly executed through UI automation to achieve the goal of improving test efficiency.

2. Introduction to Airtest

The current mainstream UI automatic test tools mainly include: Selenium, Appium and Airtest. Among them, Selenium is an open source web application automated testing tool, which can run directly on a variety of browser platforms. Its supported browsers cover almost all mainstream browsers, but because Dewu is an APP, there is no need for web-side testing. , So this tool was cruelly abandoned. Both Appium and AirTest are automated test tools for APP, both of which can record and replay automated test scripts. But the main reason for choosing AirTest is that it is easy to generate test scripts. Even if testers do not know programming and do not understand scripts, they can automatically complete the recording of scripts through normal users' click and drag operations, which greatly greatly Reduce automation maintenance costs. Through my own practice, it took half a day from UI Automation Xiaobai to the successful operation of the first automated test script. Interested students can try it.

AirtestIDE is a cross-platform UI automation test editor, suitable for games and apps. Its characteristics are as follows:

  • Automated script recording, one-click playback, report viewing, and easy implementation of automated test process support.
  • The Airtest framework based on image recognition is suitable for all Android and Windows game support.
  • Poco framework based on UI control search, suitable for Unity3d, Cocos2d and Android App.
  • Can run on Windows and MacOS.

architecture diagram
image (9).jpeg

As you can see, the main test frameworks at the bottom are AirTest and Poco. The difference between the two is:

  • AirTest: A Python-based, cross-platform UI automation testing framework, based on the principle of image recognition, suitable for games and apps.
  • Poco: An automated testing framework based on UI control search. Its core advantage is that in addition to Android and IOS, it also supports games, as well as WeChat mini programs, WeChat mini games and H5 applications.

Three, Airtset installation and connection equipment

AirTestIDE currently provides two versions of the client for Windows and Mac, please official website , unzip it and use it.

Whether it is an Android/IOS phone or a Windows window, it is regarded as a device in AirTest. The following focuses on the connection of Android devices.

Connect Android phone

Connect computers and Android phones through ADB. ADB is an Android debugging tool officially provided by Google. AirTestIDE relies on ADB to communicate with Android devices.

Open AirTestIDE and follow the steps below to connect:

1. Open the phone settings-developer options-USB debugging switch, refer to Android official document ;

2. Click the refresh ADB button in the AirTestIDE device panel to view the connected device;

3. If the device is not displayed, try restart ADB. If it still does not work, refer to FAQ document for troubleshooting.

4. After you can see the device successfully, click the connect button of the corresponding device to initialize it.

After the connection is successful, you can see the mirror display of the mobile phone screen in AirTestIDE and perform real-time operations, as shown in the following figure:
image - 2021-06-11T181846.170.png

Connect IOS phone

To connect to an IOS phone, you need to prepare a MAC computer with Xcode installed. For the connection method, refer to 160c3494c7b501 document .

Fourth, record automated scripts

After connecting the device, you can start recording the automated test script. In the following content, I will show you how to record the script on an Android phone.

Analog input

AirTest supports finding the location you want and operating it through image recognition, which is based on the AirTest framework.

We can first take a look at how to automatically record scripts: Click the "Record" button on the AirTest auxiliary window on the left side of AirTestIDE, and then as you operate the phone on the device window, the code will be automatically generated in the code window.

After recording, click "Run" to run your first automation script.
截屏2021-06-11 下午6.19.36.png

If you feel that the icons generated by the automatic recording are not accurate enough, you can also click the touch button on the AirTest auxiliary window, and then frame the precise icon on the device window to generate a touch statement. as follows:
https://www.qq.com/video/e3252k79dir

Similar analog input operations include sliding: click the swipe button, select the precise icon on the device window as the starting point of the sliding, and then click the sliding end position, and a swipe statement will be automatically generated.

Other analog input APIs include:

  • Text: text input
  • KeyEvent: key input, including (HOME/BACK/MENU, etc.)
  • Sleep: waiting
  • Snapshot: Screenshot

assertion

Up to this point, we already have various analog input methods that cooperate with logic control statements to make the phone move. There is another very important step in automated testing: result verification. We can take a look at how to declare an assertion.

(1) Verify the UI interface

The recording method is similar to analog input.

  • assert_exists: assert that the picture exists
  • assert_not_exists: Assert that the picture does not exist

(2) Verification value

Obtain the attribute value through Poco, and write the code to make an assertion.

  • assert_equal: assertion box list
  • assert_not_equal: Asserts are not equal

E.g
image (10).jpeg

View test report

After the script is run, click the "View Report" button to open the result report page with the default browser. The report will display the content of each step, the screenshots of the actual execution process, and the results of the operation, so that you can check whether the steps are executed successfully.
image - 2021-06-11T183346.186.png

image (12).jpeg

Five, AirTest script introduction

AirTest is a Python-based, cross-platform UI automation testing framework, based on the principle of image recognition, suitable for games and apps. Although you can quickly generate scripts with the help of the recording function provided by the IDE, generally speaking, proficiency in Python syntax can help us write scripts that are more widely used and less error-prone.

A simple .air script parsing

After downloading and decompressing the Airtest script's exclusive IDE-AirtestIDE, click the "New Script" button to create a script file with the suffix .air by default, which is the exclusive suffix of Airtest script.

Let us open the folder of the newly created script, we can see that in fact the .air script file is an ordinary folder with a .py file with the same name attached. When AirtestIDE executes the script, it actually executes the inside. py file. In other words, the Airtest script is still a Python script in essence, following the Python syntax, and we can freely import other third-party Python libraries according to actual needs.

It is worth noting that there must be a .py file with the same name in the .air folder, otherwise it will fail when running commands such as airtest run test.air on the command line.

AirTest script example

image (13).jpeg

Initialize the environment

First of all, just like a normal Python script, we need to write from airtest.core.api import * at the very beginning of the code file to import all the main APIs of AirTest in order to use these APIs in subsequent scripts.

auto_setup is an interface used to initialize the environment. The interface document is here. It accepts 4 parameters. We can set the path where the current script is located, specify the device to run the script, set the default log path, and set the script parent path.

  • If no parameters are passed to auto_setup, AirTest will read the parameters passed in the runtime command line to initialize the environment.
  • When creating a script in AirTestIDE, the default generated code is the simplest initialization code auto_setup(__file__), which means that the script file is passed in as the script path, and other parameters will be read by default to the parameters passed in the running command line.

There are two forms of script running command line. The parameters in the command line include device, log, etc.:

  • Example of running AirTest script from the command line: >airtest run untitled.air --device Android:///mobile device number --log log.
  • When using AirTestIDE to run the script, a usable command line will be automatically generated in the "Log View Window" for your reference.

Simulate click

As an automated testing framework, Airtest simulates human operations. Common interfaces mainly include:

  • Touch to click on a certain position, you can set the position, times, and hold time parameters of the click
  • Swipe from one location in East China to another location
  • Text calls the input method to enter the specified content
  • KeyEvent input a key response, gift box enter key, delete key
  • Wait wait for a specified picture element to appear
  • Snapshot takes a picture of the current screen
  • other

For the core API, please refer to document . The APIs appearing on this document page are all cross-platform APIs. Since we imported all the interfaces in airtest.core.api in the first line of the code, these APIs can be used in Call directly in the code, like this:
image (14).jpeg
In many interfaces, it is supported to pass in the Template image object as a parameter, and it will click on the position of the image in the screen at runtime, similar to this:
image (15).jpeg
Among them, the Template object is a picture class. AirTest will first try to find a position that matches this picture in the current screen. If it finds it, it will click on this coordinate. If it can't find it, it will throw a recognition exception.

Assert statement

Assertions are very important in unit test code, so it is recommended to use assert statements in our scripts to determine whether the current daily limit of the application under test is the state we expected. Airtest provides two interfaces, assert_exists and assert_not_exists, to assert that a picture exists or does not exist in the current screen. At the same time, two statements, assert_equal and assert_not_equal, are provided to assert that the two values passed in are equal or not equal.

How to use AirTest in a Python script

AirTestIDE can also directly create a .py script file when creating a new script, but before creating it, a setting window will pop up, requiring you to fill in some specified parameters.

After we understand the auto_setup interface, we will know that these parameters are used to pass to it and then initialize the AirTest operating environment. Therefore, the initialization code of a pure .py script can look like this:
image (16).jpeg
The above code means that when using python xxx.py to run this file without any command line parameters, it will automatically use the auto_setup interface to initialize AirTest related parameters. In this way, you only need to fill in the specified parameters when writing the py script, and you can directly use the python xxx.py command to run the script.

At the same time, the original traditional airtest run xxx.air --devices Android: /// command line method is not affected, as long as the script detects that the command line parameters are passed in (that is, if not cli_setup() judgment in the code), then It is still preferred to use command line parameters to initialize the AirTest environment.

Of course, everyone who is proficient in the API can also call the AirTest API in their own Python scripts according to actual needs, which is the same as using the normal pyhton third-party library.

Six, summary

This article simply explains how to use AirTest to record and replay UI automation scripts, as well as an introduction to the automation scripts. If you want to conduct continuous integration of UI automation, you need to further understand and learn.

I hope that students who have time can do some practice and learn about the knowledge related to UI automatic calls. In the future work, we can quickly repeat the use cases with high test repetition and low execution efficiency through UI automation to achieve the purpose of improving test efficiency.

In addition, this article only introduces image recognition through the framework of AirTest and writing test scripts. However, in some special cases, such as dynamic elements in games or APPs, it is difficult to recognize images through images. AirTest also provides another automated framework, Poco, based on UI control search. Students can learn and practice in the following article. For details, please check: https://edu.uwa4d.com/lesson-detail/124/470/0?isPreview=0

Text|Lynne

Pay attention to Dewu Technology and join hands to move towards the cloud of technology


得物技术
854 声望1.5k 粉丝