airtest.core.helper module

class DeviceMetaProperty[源代码]

基类:type

property DEVICE
class G[源代码]

基类: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)[源代码]

Add device instance in G and set as current device.

示例

G.add_device(Android())

参数:

dev – device to init

返回:

None

classmethod register_custom_device(device_cls)[源代码]
set_logdir(dirpath)[源代码]

set log dir for logfile and screenshots.

参数:

dirpath – directory to save logfile and screenshots

Returns:

log(arg, timestamp=None, desc='', snapshot=False)[源代码]

Insert user log, will be displayed in Html report.

参数:
  • 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

返回:

None

示例

>>> 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)[源代码]

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

参数:

f – The function being decorated

返回:

The decorated function

示例

Add foo() to the airtest report html page:

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

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

参数:

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:

示例

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)[源代码]

lazy import device class

delay_after_operation()[源代码]