airtest.core.helper module

class DeviceMetaProperty[source]

Bases: type

property DEVICE
class G[source]

Bases: object

Represent the globals variables

BASEDIR = []
LOGGER = <airtest.utils.logwraper.AirtestLogger object>
LOGGING = <Logger airtest.core.api (DEBUG)>
SCREEN = None
DEVICE_LIST = []
RECENT_CAPTURE = None
RECENT_CAPTURE_PATH = None
CUSTOM_DEVICES = {}
classmethod add_device(dev)[source]

Add device instance in G and set as current device.

Examples

G.add_device(Android())

Parameters:

dev – device to init

Returns:

None

classmethod register_custom_device(device_cls)[source]
set_logdir(dirpath)[source]

set log dir for logfile and screenshots.

Parameters:

dirpath – directory to save logfile and screenshots

Returns:

log(arg, timestamp=None, desc='', snapshot=False)[source]

Insert user log, will be displayed in Html report.

Parameters:
  • arg – log message or Exception object

  • timestamp – the timestamp of the log, default is time.time()

  • desc – description of log, default is arg.class.__name__

  • snapshot – whether to take a screenshot, default is False

Returns:

None

Examples

>>> log("hello world", snapshot=True)
>>> log({"key": "value"}, timestamp=time.time(), desc="log dict")
>>> try:
        1/0
    except Exception as e:
        log(e)
logwrap(f)[source]

A decorator used to add the current function to the airtest log, which can be seen on the report html page

Parameters:

f – The function being decorated

Returns:

The decorated function

Examples

Add foo() to the airtest report html page:

@logwrap
def foo():
    pass
device_platform(device=None)[source]
using(path)[source]

Import a function from another .air script, the using interface will find the image path from the imported function.

Parameters:

path – relative or absolute. This function transforms a given relative path, searching in the project root, current working directory, or the current script’s directory, into an absolute path and adds it to the sys.path and G.BASEDIR.

Returns:

Examples

Suppose our project structure is as follows:

demo/
    foo/
        bar.air
    baz.air
    main.py

If we want to reference foo/bar.air and baz.air in main.py, we can set the project root path to ST.PROJECT_ROOT, or make sure the project root path is the current working directory. We can write:

# main.py
from airtest.core.api import *
ST.PROJECT_ROOT = r"D:\demo"  # This line can be ignored if it is the current working directory
using("foo/bar.air")
using("baz.air")

If we want to reference baz.air in foo/bar.air, we can write:

# foo/bar.air
from airtest.core.api import *
using("../baz.air")
import_device_cls(platform)[source]

lazy import device class

delay_after_operation()[source]