3.2 Create a new script¶
Create a new .air script¶
Open your IDE and click on the file in the upper left corner --New Script --.air Airtest project,then you can create a new
.air script is successfully created, you will see the default initialization code:
The initialization code helps you to import the various interfaces of airtest from the api and automatically initialize the device.In fact, the
.air script is a folder that holds the
.py files with the same name as the
.air script and the associated image files.When you run the script, you actually use python to call the
.py file inside, because Airtest is essentially a Python third-party library.(For how to run the script, please click here.)
Create a new .py script [new]¶
According to the above, we can know that in the ordinary
.py file, the interface of Airtest can be called.As this demand is increasing day by day, we have added a convenient function to create
.py scripts in AirtestIDE and Airtest.The sample code for the reference can be viewed at here.The way to create it in the IDE is as follows:
Click on the file in the upper left corner -- New Script --.py Pure Python (Advanced Users):
The settings window for the python file will pop up:
The configuration items are as follows:
- Save Path： File save path, required, select the path before you can continue to configure other options
- Log Directory： The path to save the log when the script is run. The default is the
log/directory in the directory where the .py file is located.
- Devices： You can choose to automatically initialize the parameters of the devices using the currently connected mobile device.Or you can fill it in later
- Project Root： The parent directory of the project, for the future use of the
usingstatement to introduce other subfolders of the same parent directory
After configuring the required options and clicking the ok button, it will automatically create a template python file with the configuration content, then just write a script like a normal
.py file.When your
.py script is successfully created, you can also see some initialization code for the settings:
The initialization code in the above figure means that when using
python xxx.py to run this file without any command line arguments, it will automatically use the auto_setup interface to initialize the airtest related parameters.So just fill in the desired parameters when writing a python script, and you can run the script directly with the
python xx.py directive.The original command
airtest run xx.air --devices xx is also unaffected. As long as the script detects the incoming command line arguments, it still uses the command line arguments to initialize airtest.
Report generation for pure .py scripts¶
A detailed description of how to generate a report for
.air scripts is provided in the document 4.2.3 Generating Report for the Running Results.If you need to generate a report in a pure .py script by using code, you can also refer to what is described in the documentation.
In AirtestIDE, if you open the .py file and want to be able to directly use the IDE's one-click report generation function after running the script, you need to manually specify the path of the log directory when you click the Generating Report button:
How to debug scripts¶
At present, AirtestIDE does not support breakpoint debugging, so you can only debug scripts in a simpler way such as print log.
However, since Airtest's screenshot statement involves the accuracy problem of image recognition, it is necessary to run and debug repeatedly to determine the appropriate image and recognition threshold. Therefore, we provide a debugging function, that is,
select part of the code to run separately:
You can select the part of the code and click the right mouse button, then select
only run the selected code, finally you can run the selected code paragraph separately.It should be noted that the function of running the code alone will not execute the contents of other code in the script, and the variables elsewhere may not be initialized. Therefore, it can only be used as an auxiliary tool. It cannot be completely relied on to debug the whole. script.
Automatic saving of scripts and retrieval of the cache files¶
In the scripting work, we highly recommend that you use some version management software to manage the scripts you write, so as to avoid the loss of many scripts and the problem of multi-person collaboration.
When writing scripts using AirtestIDE, we will automatically save the scripts of the current editing window every 30 seconds to avoid missing scripts caused by forgetting to save.
However, in some extreme cases (computer blue screen, crash, etc.), there may be showing confusion problems with the script edit box, resulting in the loss of the original script.So we have added a script cache retrieving function that considers the current file to be a complete script file whenever Manually select to Save or Run Script. Therefore, the system will automatically save the plain text content of the script in the cache directory.If you encounter an exception and need to retrieve a previously saved script, you can right click on the script title:
In the subsequent pop-up window, there are three options:
The leftmost button is a one-click auto-recovery button that restores the contents of the most recent cache file to the current edit window. The middle button is to open the cache folder. When you open it, you will see some plain text files. The naming rule is to replace the symbols such as
:\/ in the path of the script file with
%, and finally end with a timestamp. You can find the cache file corresponding to the file name, and open it with a text editor to see the automatically saved content.