This article from the official public project number: AirtestProject the original address: airtest.doc.io.netease.com/tutorial/1_… The project source addresses of Airtest and Poco are available in the public account menu bar

preface

This document will demonstrate the process of writing an Airtest+Poco automation script using AirtestIDE, an editor dedicated to AirtestProject. It is highly recommended that you start reading this document and scripting it using the AirtestIDE.

Introduction to the

AirtestIDE is a cross-platform UI automation test editor for games and apps.

  • Automated script recording, one-click playback, report view, easy to automate the test process
  • Support for image recognition based Airtest framework for all Android/iOS/Windows applications
  • Support Poco framework based on UI control search, suitable for Unity3d, Cocos2d, Android/iOS App and other platforms
  • It runs on Windows and MacOS

Visit the official website to see more features in the video.

Through this tutorial, you will learn how to get your hands dirty with automated testing (or writing scripts to play the game automatically). Trust me, the process will be very enjoyable

The installation

The AirtestIDE provides a Windows and Mac client. Download it from the official website.

Connected devices

The AirtestIDE currently supports testing apps on Android/Windows/iOS, and more platforms are in development.

Whether it’s an Android/iOS phone or a Windows window, Airtest treats it as a device, and we’ll show you how to connect a device.

Connecting to An Android Phone

Use ADB to connect your computer to your Android phone and start debugging Android apps. ADB is Google’s official Android debugging tool. The AirtestIDE relies on ADB to communicate with Android devices.

Open the AirtestIDE and connect by following these steps:

  1. Open the phoneSettings - Developer options -USB debuggingSwitch, refer toAndroid Official Documentation
  2. Click in the AirtestIDE device panelrefresh ADBButton to view connected devices
  3. If no device is displayed, tryrestart ADBIf not, please refer toFAQDocument troubleshooting
  4. After the device is displayed, click of the corresponding deviceConnectButton to initialize

After connecting to your phone successfully, you can see a mirror display of your phone’s screen on the AirtestIDE to perform real-time operations.

If the mobile phone fails to be connected, troubleshoot the problem by referring to the FAQ. If you still fail, please submit your phone model and AirtestIDE to Github Issue, and the developer will fix it as soon as possible. Due to the serious fragmentation problem of Android phones, we really appreciate your feedback to help make this project better.

Connecting a Windows Window

For testing a Windows desktop program, the AirtestIDE can embed the window under test, facilitating script recording and debugging.

  • Click in the AirtestIDE device panelWindows- Box selected game windowbutton
  • Move the mouse pointer to the window of the program under test, and a green border will be displayed to frame the corresponding window
  • Click the left button to embed the corresponding window into the AirtestIDE

If the above method does not find the window of the program under test correctly, you can use an alternate embedding method.

Connecting to aN iOS phone

To connect to an iOS phone, you’ll need to have a Mac with Xcode installed.

Recording automation script

Once the device is connected, we are ready to record the automated test script. In the next section, we will use a Unity game application on an Android device to show you how to record the script.

The analog input

Let’s start with the most common type of mock click, which means to mimic what you’re doing by clicking on a specific location on a device.

Image recognition based

At present, we support the way of image recognition to find the position you want to click and operate, which is based on the framework of Airtest.

Click the record button on the Airtest auxiliary window of the left side of the AirtestIDE, and as you move your phone in the device window, the code will be automatically generated in the code window.

Check it out now and hit the Run button to run your first automation script!

If you feel that the icon generated by automatic recording is not accurate enough, you can also click the Touch button in the Airtest auxiliary window and select the exact icon in the device window. You can also automatically generate a Touch statement.

Swipe is a similar analog input operation: Click the Swipe button, select the exact icon in the box on the device window as the start of the swipe, and then click the end of the swipe to automatically generate a SWIPE statement.

Other apis for simulating input include:

  • Text: Indicates the text input
  • Keyevent: Key input, including (HOME/BACK/MENU, etc.)
  • Sleep: wait
  • The snapshot: screenshots
Ui-based controls

If you find that the image recognition is not accurate enough, you can also use a UI control-based search to automate the test. Unlike Airtest, this is what Poco does.

At present, Poco directly supports a variety of game engines such as Unity3d, Cocos2d and Egret Engine, as well as Android/iOS native apps.

If it is an Android/iOS native app, it is plug and play and does not need to access the SDK. But since the game engine renders directly using a graphical interface like OpenGL instead of Android’s native UI system, we need to communicate with the game Runtime to capture the entire UI structure.

We provide a very convenient SDK access method, see here for a list of currently supported platforms and how to access Poco for your project.

If your project uses an engine or platform that is not documented, we also support self-extending the SDK.

In fact, within netease games, we supported Messiah/NeoX/ Dream and other homegrown engines in this way.

We can start as soon as it’s plugged in. Start the game on your phone and switch mode to the corresponding engine type in the Poco auxiliary window of the AirtestIDE to see the entire UI structure.

Click the Record button, and with your mouse action, the Poco statement will be automatically generated in the script edit box.

Similarly, you can view UI controls more accurately through the UI tree structure, double click the node to automatically generate Poco statements, or choose a better way to write them.

Automatically recorded statements are not necessarily suitable for all scenarios, and writing code with more reasonable selectors generally increases the robustness and readability of the entire automated script.

After recording the script, remember to run it to see how it looks.

For Android/iOS native apps, you do not need to access the SDK to use. For example, after connecting to an Android phone, you can switch the MODE of THE Poco auxiliary window to Android, and you can see the entire UI tree structure.

The framework of information

The above two UI identification methods are based on two frameworks respectively:

  • Airtest framework based on image recognition
  • Poco framework based on UI control search

Both frameworks are Python third-party libraries developed by our team, and in our experience on projects we have found that they work best together. During scripting, we often need to consult their project API documentation as well.

Using Python syntax

The code recorded and run in the AirtestIDE is based on Python. Python syntax is compact and powerful, and third libraries and tools abound.

For beginners, Python is easy to get started with. You can write the logical statements needed in automated scripts by learning the basic syntax.

touch(PNG "Open card package.png")
if exists("Bonus panel.png") :for i in range(5):
		Poco("Reward - % s" % i).click()

Copy the code

For the veteran, you can use a variety of third-party libraries in AirtestIDE to make your automated scripts even more powerful. By adding the PYTHONPATH setting, you can run your scripts using the native python.exe.

In addition to the statements provided in the helper window, for more API documentation, check out the Airtest and Poco repositories.

assertions

So far, we’ve had a variety of analog input methods, combined with logical control statements to get the hand moving. There is another important step in automated testing: validation of results, so let’s look at how to declare assertions.

Validate the UI

The recording method is similar to analog input

  • assert_exists: Asserts that the picture exists
  • assert_not_exists: Asserts that the picture does not exist

Validation of numerical

Attribute values are obtained through Poco, and assertions are made in handwritten code

  • assert_equal: assert equality
  • assert_not_equal: Assertions are unequal

For example,

#... After simulating input and scoring 20 points

value = Poco("Score button").attr("num")
assert_equal(value, 20, "Score 20 points.")
Copy the code

Viewing test Report

After the script is run, click the View report button (shortcut key Ctrl+L) to open the result report page using the default browser. The report displays the contents of each step, screenshots of the actual execution process, and running results to check whether the steps are successfully executed.

Command line interface

Now you’ve learned to automate tests. Next, you can use the command line interface to combine automated testing with continuous integration. What is continuous integration?

When you run the script in the AirtestIDE, a command will be printed in the LOG window.

You can use that command from the command line to start the test script without starting the IDE, for example:

"D:\ Xunlei download \AirtestIDE\AirtestIDE" runner "D:\ airtestide_2018-01-24_83 \untitled.air" --device Android: / / 127.0.0.1:5037 / F8UDU16409004135 - log "C:\Users\gzliuxin\AppData\Local\Temp\AirtestIDE\scripts\cdfc40e8c297b6ad88e09de64d8bafa3"Copy the code

Using the AirtestIDE you can record your test script and save it as an. Air script. Note that a.air script does not contain too much content, organize your script with a good script name and directory structure, and cover all test points.

You can also run tests on different machines for different devices by running the.air script from the command line. For products released on multiple platforms, cross-platform APIS and command lines can be flexibly used, and the same set of test scripts can be run on Android and Windows for testing.

Within netease Games, our large games usually have hundreds of test scripts covering common playtests. Each week, these hundreds of scripts are run on 200 phones for compatibility testing.

For more information about the tutorials and the project, please visit our official account AirtestProject to check out the previous tutorials: