Skip to content

3.1. Scripting

How do I start writing scripts?

We provide two libraries Airtest and Poco libraries for scripting and running. They are Python third-party libraries, so scripting needs to have a certain Python foundation.If you are not familiar with the Python syntax, there are quite a few very good Python tutorials on the web to learn, such as Liao Xuefeng's Python Newbie Tutorial.

In the scripting process, you can use Airtest and Poco at the same time according to your needs, or you can freely add other python third-party libraries you want to use to complete more powerful functions.

However, please note that you can mix Airtest and Poco code. It does not mean that you can use Airtest's picture and Poco's statement in the same statement. Please pay attention to each library's own API and grammar when you write your script.

Before you start your scripting work, you can read the Tutorial provided by the official website to help you get started faster.

Preparation for the development environment

If you use AirtestIDE to write scripts, you only need to download the latest version of AirtestIDE on Official Website and unzip it to use it directly.AirtestIDE has built-in Python 3.6.5, airtest and poco environments, and you can use directly without installing python environment.

If you need to run the script using the python command line, please refer to the Environment Deployment chapter in Run Script.

If you want to use other Python third-party libraries that need to be installed, or a Python2 environment, please do local python environment deployment first.Then add the local path of Python.exe to the AirtestIDE settings. See IDE settings for details.

The difference between Airtest and Poco

The Airtest framework is based on image recognition, and when writing a script, something like this:

image

The Poco framework is based on a UI control search, and when writing a script, something like this:

image

How to initialize Airtest and Poco

The scripts created in AirtestIDE have the suffix .air, but when we are running, we actually run the .py file(with the same name as the .air file) in the .air directory.So, like a normal python script, when we need to use the airtest interface (such as the touch command), we need to import the airtest api at the top of the .py script.We can create a new script in AirtestIDE and you will see the default initialization code:

# -*- encoding=utf8 -*-
        __author__ = "user"

        from airtest.core.api import *
        # After importing the interface from the api, you can directly use the various interfaces of the airtest.
        auto_setup(__file__)  # Automatically initialize the device

For more API information provided by airtest, please refer to Airtest Documentation.

For how to connect to the corresponding device I want, please refer to How to connect to the phone/windows window.

Unlike Airtest, Poco needs to insert a piece of initialization code before use, as shown in the figure:

image

For the initialization of Poco, please refer to How to use Poco.

How to run a script from the command line

For using command line to run the script, we have a very detailed introduction in the Run Script section, please check it.

We do not provide the way to run the scripts in batches. If you need them, you can write the scheduling script to run them.

The script written in AirtestIDE is missing

The first time you open AirtestIDE, it will help you to create a new empty script in the system temp directory by default, so that you can familiarize yourself with the software functions and get started writing scripts.In the scripting work,you should try to avoid saving the script in the system directory or in the directory without permission to read or write.If it's MacOS, please don't save the script in the AirtestIDE.app directory (for example: /Applications/AirtestIDE.app/Contents/MacOS), because once you reload the app, it will be overwritten causing the script to be lost.

Be sure to choose a suitable, accessible directory to store your scripts.

Record Airtest statement in AirtestIDE

AirtestIDE provides a lot of convenience for recording Airtest statements with screenshots. For the recording method, please refer to 2. Airtest Script Assist Recording.

Airtest API documentation

Most of Airtest's common API are listed in the Airtest aux window in AirtestIDE.When using various common screenshot statements, it is very convenient to move the mouse over the button to see the common parameters and return value information of each interface.

image

For more detailed API information, please refer to the airtest.core.api page.

The API information in airtest.core.api is a common API and common parameters for each platform.The same interface may have different parameters and even some extra unique interfaces under different platforms.For example, on the Android device, the touch interface is to click on a certain location, but in Windows, we may have double-click and right-click operations. At this time, we need to check the corresponding interface documentation of the corresponding platform. As shown in the figure:

image

In the advanced reading chapter of our quick start tutorial How to write a test script for your game, we take a detailed introduction to it by using touch as an example. And you can use as a reference to see how to use and how to access the documents.

If your script involves an Android device and you want to use a unique interface on the Android device (for example, to easily call the adb shell ls command), you can refer to the tutorial - Test On Android phones, which introduces several Android device-specific interfaces and calling methods in detail. Other platforms are similar, please refer to the related tutorial for more information.

For more details, please refer to Chapter 5 Airtest.

Use Poco in AirtestIDE

Similarly, AirtestIDE provides a number of convenient features for Poco recording and use. For details, please refer to 3. Poco auxiliary function.

Poco API documentation

In our quick tutorial How to write a test script for your game, we use a game as an example to elaborate on how to get started writing airtest and poco scripts. It is highly recommended that novices read in advance.

For more details, please refer to Chapter 6 Poco.