Skip to content

I. Running Script in AirtestIDE

1.Clicking on the Run Script Button

If we use AirtestIDE to write scripts, then running the scripts is very simple. Just click on the Run Script button on the top or press the shortcut key F5.

image-20211013174053412

After clicking on the Run Script button, the script will start to run. The log window will refresh with real-time logs. If you want to stop it, simply click the Stop button next to the Run Script button or press Shift+F5.

image-20211013174320070

2.Right-Clicking to Debug Separately

In the previous section on script writing, we introduce the option to debug scripts by right-clicking (running only selected code), which is different from running the scripts. Debugging a script by right-clicking means selecting one or more script(s) in the IDE's script editor and executing them separately. This method can help us quickly verify the correctness of the selected scripts and view the debugging logs in the log window. However, this is not part of the actual script running process, and the IDE will not save the running log content for us.

Therefore, even if you select the entire script and choose to only run the selected code, after execution, the IDE will not save the running log or generate a test report based on it.

3.Viewing the Running Log

Let's run a simplest .air script to have a look, which only contains one touch script besides the import and initialization statements automatically added when creating the script:

run_script

Upon touching the Run Script button, we can see that the log viewer will display the command for running the script and real-time updates of the script's log content. During the script running process, the IDE will mark the current line of the script with a cursor, making it very convenient for us to observe the script's running status. (This example uses the built-in IDE environment to run the script).

1) Command for running scripts in IDE

Let's take a closer look at the content printed in the log viewer. First, at the top is the command for running the script:

"D:\demo\AirtestIDE-win-1.2.12\AirtestIDE\AirtestIDE" runner "D:\test\song.air"  --device android://127.0.0.1:5037/127.0.0.1:7555?cap_method=MINICAP&&ori_method=MINICAPORI&&touch_method=MINITOUCH --log "D:/test/test01\0d86098ed0cd4a54c8c611578a3d71b7"

The meaning of this command is to run the script song.air in the IDE's built-in environment, with the device being the mumu emulator on port number 7555, and the log contents saved in the directory D:/test/test01\0d86098ed0cd4a54c8c611578a3d71b7.We can directly copy the command and run it in the terminal.

2) Filter log level in the log viewer

After running the script, the log viewer displays some log information such as [debug], [info], etc. following the command at the top. If we want to focus on the [error] information in the log, we can use the log viewer's shortcut button to filter the log level and quickly find the logs we need:

log

4.Running Scripts in Built-In and Local Environments

AirtestIDE comes with a built-in Python environment that already has Airtest, Pocoui, and other libraries installed. So, after writing your automation script, you can simply click on the Run Script button to run your script in this built-in Python environment in the IDE.

But if you want to add some other python third-party libraries, you need to turn to the local python environment to run the scripts and install the desired python third-party libraries into the local python environment:

image-20211014104459728

Note

If you set to use the local Python environment to run the script, it is independent of the environment that comes with the IDE. In this case, if you want to check whether you are using the latest environment to run the script, you should not check whether the IDE is the latest version, but rather checking whether the airtest and pocoui libraries in the Python environment settings are the latest.

1) How to deploy a local Python environment?

It is very easy to deploy a local Python environment in the IDE. What we need to do is set the path of python.exe in the IDE settings. However, not any Python environment can run the Airtest or Poco scripts you write, so we need to pre-install related Python third-party libraries in this Python environment.

① Python version selection

Python 2.7 or <=3.9 are supported, but we highly recommend Python 3. We also recommend creating a clean python environment using a virtual environment such as virtualenv if you wish.

Note: If after installing python3.9, you still cannot use Airtest and receive the error "ImportError: numpy.core.multiarray failed to import", you can manually downgrade the numpy version to 1.19.3 to fix it.

pip install -U numpy==1.19.3
② Airtest installation
  • Install the Airtest framework with pip: pip install airtest.
  • Note: In Mac/Linux systems, it is necessary to manually grant ADB executable permissions, otherwise you may encounter a "Permission denied" error when running the script.
# Mac OS

> cd {your_python_path}/site-packages/airtest/core/android/static/adb/mac

# Linux System

> cd {your_python_path}/site-packages/airtest/core/android/static/adb/linux
> chmod +x adb
  • If "ImportError: DLL load failed: The specified module could not be found" occurs in the cv2 module when running the code, there are several solutions:
  • The root cause of this issue should be the missing DLL files which are placed in the directory of the IDE. To solve it, you can simply download the latest version of AirtestIDE, find the two DLL files named api-ms-win-downlevel-shlwapi-l1-1-0.dll and IEShims.dll in the extracted directory, copy them to the C:\Windows\System32 directory, and run the code again.
  • If you are using Python 3.7, please install Visual C++ redistributable 2015. Refer to this documentation.
  • If your version of Python used is lower than 3.7, please run it directly:
>>pip uninstall opencv-contrib-python   

# If the following installation command fails to run, you can try updating pip to the latest version and try again.

pip install opencv-contrib-python==3.2.0.7  
  • If an error of "DLL load failed" occurs when importing win32api in win.py, you can try reinstalling the pywin32 module:

pip uninstall pywin32 pip install pywin32==223

③ Poco installation

Use pip instruction pip install Pocoui to install the Poco framework. Please note that the library name is "pocoui" and make sure you fill in the right name. If you have installed both Poco and Pocoui, conflicts may occur when running scripts. Please make sure that you have only installed the correct Pocoui library in your Python environment.

④ Airtest-Selenium installation

Use the pip instruction pip install Airtest-Selenium to install the Airtest-Selenium framework. If you are not doing web testing, you can install this framework in your Python environment later when you need it.

⑤ Pip instruction failed to run

For domestic users, please add -i https://pypi.tuna.tsinghua.edu.cn/simple (Tsinghua source) after the pip install instruction and try again. Refer to this link.

2) When using the local Python environment, you encounter an error of no module named...

When we set the IDE to use the local Python environment to run the script, and encounter errors like "no module named airtest", it is mostly because the relevant library has not been installed in the local Python environment. We can refer to the previous section on How to Deploy the Local Python Environment to install the corresponding third-party library. This kind of error may also occur when we have installed libraries such as Airtest in the local Python environment and have checked that by using the pip list command. In this case, we should check whether there are multiple Python environments in the local system, and in which Python environment we installed the libraries, and which one is set to be used in the IDE.

5.Difference Between Running .air scripts And .py scripts in IDE

1) Differences in the running commands

In the beginning, let's compare the commands to run .air scripts and .py scripts:

# Command for running .air scripts in the IDE

"D:\demo\AirtestIDE-win-1.2.12\AirtestIDE\AirtestIDE" runner "D:\test\song.air"  --device android://127.0.0.1:5037/127.0.0.1:7555?cap_method=MINICAP&&ori_method=MINICAPORI&&touch_method=MINITOUCH --log "D:/test/test01\0d86098ed0cd4a54c8c611578a3d71b7"

# Command for running .py script in IDE
"D:\demo\AirtestIDE-win-1.2.12\AirtestIDE\AirtestIDE" pyrunner "D:\test\song.py" 

You can see that when running an .air script in the IDE, it will automatically pass in the device information connected to the device screen and save the running log content in the default log storage path.

But when running the .py script, it simply runs a single .py file and does not pass any device information or save log information.

This means that when running .py scripts in the IDE, we need to manually connect devices and saving logs.

2) Differences in the initialization scripts

The differences in running commands also lead to differences in the initialization scripts for .air and .py. The initialization statement for .air script only requires the following script:

# -*- encoding=utf8 -*-

__author__ = "AirtestProject"
​
from airtest.core.api import *
​
auto_setup(__file__)

Even if no parameters are passed into the auto_setup API, the IDE will automatically use commands to connect to the device and save log content when running the script. We don't need to handle it separately, which is very simple and convenient. However, the initialization script for .py is different:

# -*- encoding=utf8 -*-

__author__ = "AirtestProject"
​
from airtest.core.api import *
from airtest.cli.parser import cli_setup
​
if not cli_setup():
    auto_setup(__file__, logdir=True, devices=["android://127.0.0.1:5037/127.0.0.1:7555?cap_method=MINICAP&&ori_method=MINICAPORI&&touch_method=MINITOUCH",])

The m eaning of this initialization code is that when running this file using python xxx.py without any command line arguments, it will automatically use the auto_setup interface to initialize the Airtest-related parameters. In the auto_setup interface, logdir=True is set to save log content in the current script directory for the later generation of Airtest reports. The devices parameter is passed a device list that includes one Mumu emulator with a port number of 7555, indicating that the current script will run on this device. After handling the expected parameters in the script, we can directly run the script using the command python xx.py. The original command "airtest run xx.air --devices xx" is not affected. As long as the script detects that command line arguments are passed, it will still prioritize using the command line arguments to initialize Airtest. !!! Warning "Note": Therefore, we highly recommend that beginners use .air scripts instead of .py scripts. Of course, if you are already very familiar with the differences between the two, you can easily add necessary parameters in .py scripts or use .py scripts to write automation scripts.

6.Notes on Running Scripts

1) Please do not delete initialization scripts at will

When creating an .air or .py script in the IDE, it will automatically insert some initialization code. Do not delete these initialization codes to avoid affecting the normal operation of the script.

2) Pay attention to distinguish between running scripts and running only selected code

There is a big difference between clicking on the Run Script button to run the script and only running the selected code. When you click on the Run Script button to execute the script, the IDE will help save the log content for generating Airtest reports later. But when you choose to only run the selected code, it is just a script debugging, and the IDE will not save the log content, nor can we generate an Airtest report on it.

3) No highlight on the running script in local Python environment

In the previous section, we have mentioned that when using the Python environment built into the IDE to run scripts, the script editor in the IDE will highlight the line currently being executed. However, if you set up a local Python environment in the IDE to run the script, the current line being executed will not be highlighted in the script editor.s