Global variable of Airtest

Last review: Airtest API text()

Based on

Just as all test frameworks have a place to store global variables, Airtest also has a place to store global variables:

# File location: your_python_path/site-packages/airtest/core/
class Settings(object):
    DEBUG = False
    LOG_DIR = None
    LOG_FILE = "log.txt"
    RESIZE_METHOD = staticmethod(cocos_min_strategy)
    # keypoint matching: kaze/brisk/akaze/orb, contrib: sift/surf/brief
    CVSTRATEGY = ["mstpl", "tpl", "surf", "brisk"]
    if LooseVersion(cv2.__version__) > LooseVersion('3.4.2'):
        CVSTRATEGY = ["mstpl", "tpl", "sift", "brisk"]
    THRESHOLD = 0.7  # [0, 1]
    THRESHOLD_STRICT = None  # dedicated parameter for assert_exists
    OPDELAY = 0.1
    PROJECT_ROOT = os.environ.get("PROJECT_ROOT", "")  # for ``using`` other script
    SNAPSHOT_QUALITY = 10  # 1-100
    # Image compression size, e.g. 1200, means that the size of the screenshot does not exceed 1200*1200
    IMAGE_MAXSIZE = os.environ.get("IMAGE_MAXSIZE", None)
    SAVE_IMAGE = True

A few global variables have been used or talked about in previous articles, and they will be stated again in future articles. Here, let's briefly talk about the more typical:

  • LOG_DIR and LOG_FILE: set the directory and file name of the generated log. In general, we should use the default. If you are not familiar with Python, you may not be able to generate logs and reports after customizing the settings

  • RESIZE_METHOD: resolution adaptation rule. Use the default. The specific principle is slightly complex, and the original text on the official website is quoted:

When using devices with different resolutions for image recognition, the recognition success rate may be poor. Therefore, Airtest provides the default resolution adaptation rule (using the default scaling rule of Cocos engine)

The best way to improve the recognition accuracy of 2d games is to specify the resolution adaptation rules of your game, for example, directly in The beginning of the air script file reads:

from airtest.core.api import *

def custom_resize_method(w, h, sch_resolution, src_resolution):
    return int(w), int(h)

#Replace default RESIZE_METHOD
ST.RESIZE_METHOD = custom_resize_method

The above code specifies a custom scaling rule: directly return the original value. No matter the screen resolution, all UI s will not be scaled (this is the strategy in some games).

Resize here_ Method is the custom defined by us_ resize_ The input parameters used by method are:
w. H # the width and height of the recorded UI picture
sch_resolution # recording screen resolution
src_resolution # playback screen resolution

Output is:
UI picture width and height during playback

To customize your RESIZE_METHOD, just know the scaling rules of the game you are testing, and then click custom_resize_method can be implemented in code. In this way, the success rate of image recognition under different resolution devices can be greatly improved.

from airtest.core.settings import Settings as ST
ST.CVSTRATEGY = ["mstpl", "tpl"]
  • THRESHOLD: the THRESHOLD of image recognition. See the previous article for details Airtest API Template

  • OPDELAY: the interval between operations. Just keep the default. But if you try to be stable and want to stop for a while before each operation, you can turn it up.

  • FIND_TIMEOUT and FIND_TIMEOUT_TMP: in Airtest source code analysis -- the overall process of image recognition As we said in, image recognition is carried out within a specified time, and timeout is considered as failure. FIND_TIMEOUT and FIND_TIMEOUT_TMP is long and short timeout respectively.
    Use FIND_TIMEOUT interfaces include: assert_exists(), touch(), wait(), swipe(), etc;
    Use FIND_TIMEOUT_TMP interfaces include: assert_not_exists(), exists(), etc.

    The global timeout can also be defined in some global timeout variables, such as:

from airtest.core.settings import Settings as ST

# Set the global timeout to 60 s

#Set the timeout length of a single wait statement
wait(Template(r"tpl.png", record_pos=(-0.044, -0.177), resolution=(1080, 1920)),timeout=120)


from airtest.core.settings import Settings as ST

# PROJECT_ROOT Absolute path is required
ST.PROJECT_ROOT = "D:/test/user/project"

#If we do not set the project root directory, we may call test1 air,test2.air


  • SNAPSHOT_QUALITY and IMAGE_MAXSIZE: indicates the screenshot accuracy and screenshot size respectively for use in the report. Airtest core API summary A detailed example is provided in the point 12 snapshot

  • SAVE_IMAGE: whether to save the screenshot during script running. The saved screenshots will be reflected in the report, and the screenshots at that time and the operation points will be displayed in pictures, such as:

from airtest.core.api import *

ST.SAVE_IMAGE = False  # Close screenshot
touch((100, 100))  # This statement will not save the current picture

ST.SAVE_IMAGE = True  #Save screenshot
touch((100, 100))

After the report is generated, the first touch will not have a screenshot, and the second touch will display a screenshot

First touch

Second touch

Note: if you want to modify a global variable and make it globally valid, remember to put it at the beginning of the script.



Pay attention to WeChat official account mobile phone and check more.

Keywords: Testing

Added by Wayne on Tue, 04 Jan 2022 12:10:22 +0200