Airtest scripts settings¶
Airtest provides some additional settings. By modifying them, we can achieve a better result of running scripts.
2. Global settings for scripts¶
Some default global properties are provided in airtest.core.settings. Here we list some common attributes and their default value:
- RESIZE_METHOD = staticmethod(cocos_min_strategy)
- THRESHOLD = 0.7 # [0, 1]
- THRESHOLD_STRICT = 0.7 # [0, 1]
- OPDELAY = 0.1
- FIND_TIMEOUT = 20
- FIND_TIMEOUT_TMP = 3
- PROJECT_ROOT = os.environ.get(“PROJECT_ROOT”, “”) # for using other script
How to set global attributes in the scripts¶
If you want to modify the settings of above global values, see below:
from airtest.core.api import * # airtest.core.api contains a variable named ST, which is global settings ST.THRESHOLD = 0.8 # Image threshold unspecified, uses 0.8 in ST.THRESHOLD by default touch(Template(r"tpl1532588127987.png", record_pos=(0.779, 0.382), resolution=(407, 264))) # Image threshold manually specified, 0.6 as shown in the image touch(Template(r"tpl1532588127987.png", record_pos=(0.779, 0.382), resolution=(407, 264), threshold=0.6))
Description of global setting attributes¶
When you use devices with different resolution to perform image recognition, the success rate may be poor. Therefore, Airtest provides default resolution adaptation rules (using Default scaling rules of Cocos engine ). See airtest source code here .
The best way to improve recognition accuracy of 2d games is to clearly specify the resolution adaptation rules of your game. For example, you can write this at the beginning of .air script files:
from airtest.core.api import * def custom_resize_method(w, h, sch_resolution, src_resolution): return int(w), int(h) # Replace the default RESIZE_METHOD ST.RESIZE_METHOD = custom_resize_method
The code above specifies a custom scaling rule: return is the original value regardless of the screen resolution. All UIs do not scale (this is an strategy for some games).
RESIZE_METHOD, which is the
custom_resize_method we defined, uses the arguments below:
- w, h # width and height of UI images during recording
- sch_resolution # screen resolution during recording
- src_resolution # screen resolution during playback
- Width and height of UI images during playback
If you want to customize your
RESIZE_METHOD, all you need to know is the scaling rule of the game tested, then implement the
custom_resize_method in the code. This can greatly improve the success rate of image recognition under different resolution devices.
Besides setting the
THRESHOLD value for an individual image, we can also set global
THRESHOLD so it affects every image.
There is also
THRESHOLD_STRICT value, which is a more strict option to set the threshold, but it applies only to
In the script, there is a short interval time between each operation. You can use
OPDELAY to set this value.
The value of
OPDELAY is 0.1 by default, which means it waits for 0.1 second after each operation. Set a larger value in order to pause longer after each operation and avoid failures caused by operations starting too quickly.
Every time an image is searched, if it is not found, the loop will start the screenshot-search process again and again until it times out. This timeout value is called
FIND_TIMEOUT and it is 20 seconds by default.
At the same time, we also provide
FIND_TIMEOUT_TMP, which is only 3 seconds by default. These two values are respectively used in different screenshot interfaces:
Use FIND_TIMEOUT as the interface for the timeout of image lookup:
- swipe (supports swiping from the first image to the second, here only the first image uses
- wait (it supports passing a timeout argument directly. If timeout is not specified, then it uses
For other interfaces that have lower requirements for image search,
FIND_TIMEOUT_TMP is used, which has a shorter length of timeout:
- swipe (If an image is passed to the second argument, it applies the shorter search time)
If you want to modify the global timeout for image search, make sure that the values are modifed appropriately.
You can set a default project root directory via PROJECT_ROOT. The using interface can search for other subscripts under the current root directory without providing a complete path. This is more convenient for scripts to call each other:
from airtest.core.api import * ST.PROJECT_ROOT = "/User/test/project" # actual path of test1.air is /User/test/project/test1.air using("test1.air") from test1 import test
Image recognition algorithm selects CVSTRATEGY¶
In Airtest, a variety of different image recognition algorithms are provided. The list of method names is: [“tpl”, “kaze”, “brisk”, “akaze”, “orb”, “sift”, “surf”, “brief “].
For specific evaluation results of each algorithm, such as memory recognition and memory usage, refer to the Airtest/benchmark document, in actual use. Due to the use of scenes, image resolution and the type of mobile phone, the performance of each algorithm is different. Therefore, you can use your own use case to test the algorithm and choose a more suitable algorithm.
In the Airtest script, use the following code to set the image matching algorithm in the script:
from airtest.core.settings import Settings as ST # image matching will follow the method list, until the result is found or timeout: ST.CVSTRATEGY = ["surf", "tpl"]