Skip to content

4.2 Run the script using the command line

After our script is written, it can be run with scripts in addition to AirtestIDE.

1. Run the script using the AirtestIDE command line

Even if the python environment is not installed locally, or if airtest and poco are not installed, we can use the command line to run the script.The trick is to copy the command line that appears in the log window when you run the script in AirtestIDE:

image

Copy the command line code generated by AirtestIDE when running the script to your command line terminal, and press Enter to run it.

Please Note that the way using the AirtestIDE command line to run scripts is a simple and fast way. But if you want to execute in batches and execute them at the same time, you may encounter a problem with pool efficiency.If you need to execute scripts for a long time or in batches, try to install airtest and poco in the local Python environment to execute the script. See the next section for details.

2. Run the script using the local python environment

As described in Method 1 ,using the AirtestIDE command line to run scripts is convenient, but not suitable for more complex operations (for example, if you want to run multiple phones, multiple scripts with multiple command lines).And for some Python developers, you might need to use other powerful Python third-party libraries in your scripts. Therefore, we recommend installing airtest and pocoui in a local python environment, and then running the script from the command line.

Deploy the Python environment

To run the script using the local python environment, you need to configure the local runtime environment:

  • The Python version is available between 2.7 and 3.6, but we recommend Python3. If you prefer, we also recommend creating a clean python environment using virtual environments such as virtualenv.

  • Airtest installation:

    • Install the Airtest framework using the pip command pip install airtest

    • Note: Under Mac/Linux system, you need to manually give adb executable permission, otherwise you may encounter an error of Permission denied when executing the script:
      # mac system 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 the cv2 module reports ImportError: DLL load failed: Cannot find the specified module when running the code, there are several solutions:
      • The root cause of this problem should be the lack of DLL files. We put them in the IDE directory, you can directly download a new version of AirtestIDE.Then find the two DLL files api-ms-win-downlevel-shlwapi-l1-1-0.dll and IEShims.dll in the extracted directory.Finally copy them to the C:\Windows\System32 directory and re-run the code to solve it:
      • If you are using a python version lower than 3.7, please run it directly: pip uninstall opencv-contrib-python # If the following installation statement fails, you can try to update to the latest version and try again pip install opencv-contrib-python==3.2.0.7
      • If you are using python3.7, please install Visual C++ redistributable 2015 and refer to documentation .
  • Poco installation

    • Use the pip instruction pip install pocoui to install the poco framework , please note that the library name is pocoui, don't make it wrong.
  • Pip instruction failed to run

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

After the environment is deployed, we are able to leave the Airtest IDE and run scripts on different host machines and platforms under test.

Run the script using the python command line

The following is an example of running a script using the command line:

>airtest run untitled.air --device Android:///mobile phone number --log log/
>python -m airtest run untitled.air --device Android:///mobile phone number --log log/

The effect of these two command lines is the same. We used airtest to run a script called untitled.air and passed in the two parameters --device and --log.They are our mobile device and log output directory.

The parameter that must be passed in the airtest run command is the path to the specified script. Other optional parameters are --device which specifies the device string, --log which specifies the log output directory. For more parameters please refer to the Airtest-running-air-from-cli documentation.

About device strings

In the --device parameter used in the previous command line, the device string is passed in. Take the Android device as an example. The string is completely defined as follows:

Android://<adbhost[localhost]>:<adbport[5037]>/<serialno>

Among them, adbhost is the ip of the adb server host, the default is the native 127.0.0.1. The default adb port is 5037 and serialno is the serial number of the android phone. For more information on adb, please refer to the document ADB.

When running a script, we can generally write like this:

# Nothing is filled in, the first phone in the current connection will be selected by default  
Android:///
# Connect a mobile phone with the device number 79d03fa connected to the default port of the machine   
Android://127.0.0.1:5037/79d03fa
# Use the adb of this machine to connect to a remote device that has been connected with the adb connect command. Note that 10.254.60.1:5555 is actually the serialno.
Android://127.0.0.1:5037/10.254.60.1:5555
# Connect a Windows window, the window handle is 123456
Windows:///123456
# Connect to a Windows window, the window name matches a regular expression
Windows:///?title_re=Unity.*
# Connect windows desktop without specifying any window
Windows:///
# Connect to the iOS phone
iOS:///127.0.0.1:8100

It is worth mentioning that the connection of windows window uses the pywinauto library. And the connection in AirtestIDE defaults to using the window handle that is currently connected to the IDE. It is envisioned that if the window is closed, the handle may change when the window is opened again.Therefore, our connection string supports the connected interface of pywinauto to connect to the window. See the parameter section in Reference Document, which has several parameter filling methods for writing the connection string: Windows:// /?name=value .

Note: If you can't connect the device correctly using the device string you spelled, but you can connect it properly in AirtestIDE, you can consider running the script once in AirtestIDE, then copying the parameter in --device Android:/// from the command line automatically generated at runtime,and you can use it in the code. This can be greatly avoided device string writing errors.

Special parameters for some Android devices

In the 2.2 Android Connection FAQ section, we mentioned that some devices are more special, and you need to check the special connection options such as use ADB orientation or use javacap when connecting. So when you run the script from the command line, you also need to append these parameters to the device string:

# Connected to the emulator and checked the `Use javacap` mode
Android://127.0.0.1:5037/127.0.0.1:7555?cap_method=JAVACAP
# Devices connected after all options are checked will use && to connect multiple parameter strings.
Android://127.0.0.1:5037/79d03fa?cap_method=JAVACAP&&ori_method=ADBORI&&touch_method=ADBTOUCH

Note: If there are any characters such as ^ < > | & on the command line, they may need to be escaped to take effect. Therefore, if you need to write && in the connection string, you need to rewrite it as ^&^& under windows which means add a ^ symbol to escape, as follows:

# --device Android://127.0.0.1:5037/79d03fa?cap_method=JAVACAP&&ori_method=ADBORI Not available under windows
--device Android://127.0.0.1:5037/79d03fa?cap_method=JAVACAP^&^&ori_method=ADBORI  # the effect of adding ^ to escap in the windows command line
--device Android://127.0.0.1:5037/79d03fa?cap_method=JAVACAP\&\&ori_method=ADBORI  # add \ to escap on the mac command line

Screening the running process of the script

When running a script on an Android phone, we can let airtest automatically record the phone screen during script execution by adding a --recording parameter to the command line that runs the script.After the recording is completed, a command format file will be automatically generated to the log directory, similar to recording_0.mp4. This mp4 file will be displayed by default on the HTML report page when the report is finally generated.

Set local Python.exe to AirtestIDE default environment

Python used in AirtestIDE is a default Python environment with Airtest and Poco built in. Although it is very convenient to use, it can't be supported if you need to use some other third-party libraries.At this point, we can set the local path of Python.exe to the AirtestIDE settings, so that AirtestIDE can easily use the native Python environment to execute the script,and even Python2 is also supported. For the detailed setting methods, please refer to Option Configuration - Run the script using the native Python environment.

3. Generate a run result report

For the relevant content of the report, you can view the Generate Report section.