5.1 Airtest 常用API信息

在自动化测试脚本中,我们可以灵活使用Airtest提供的API完成脚本功能。

在AirtestIDE中使用Airtest相关的API不需要import,可以直接书写使用,方便使用Airtest脚本直接进行指令的编写

AirtestIDE中提供了Airtest的一些常见的API快捷功能,比如图像操作、文本输入、sleep等。我们也可以直接使用Airtest中的方法完成测试任务。

注意,本文只列举了一些常用的API,如需了解完整的API信息可以查阅API文档

5.1.1 Airtest 提供的方法

Airtest提供的方法主要分为3大类:

  • 设备连接和设备管理
  • 设备操纵相关
  • 断言相关

5.1.2 Airtest的跨平台API

This module contains the Airtest Core APIs.

init_device(platform='Android', uuid=None, **kwargs)[source]

Initialize device if not yet, and set as current device.

Parameters:
  • platform – Android, IOS or Windows
  • uuid – uuid for target device, e.g. serialno for Android, handle for Windows, uuid for iOS
  • kwargs – Optional platform specific keyword args, e.g. cap_method=JAVACAP for Android
Returns:

device instance

connect_device(uri)[source]

Initialize device with uri, and set as current device.

Parameters:

uri – an URI where to connect to device, e.g. android://adbhost:adbport/serialno?param=value&param2=value2

Returns:

device instance

Example:
  • android:/// # local adb device using default params
  • android://adbhost:adbport/1234566?cap_method=javacap&touch_method=adb # remote device using custom params
  • windows:/// # local Windows application
  • ios:/// # iOS device
device()[source]

Return the current active device.

Returns:current device instance
set_current(idx)[source]

Set current active device.

Parameters:idx – uuid or index of initialized device instance
Raises:IndexError – raised when device idx is not found
Returns:None
Platforms:Android, iOS, Windows
auto_setup(basedir=None, devices=None, logdir=None, project_root=None)[source]

Auto setup running env and try connect android device if not device connected.

shell(cmd)[source]

Start remote shell in the target device and execute the command

Parameters:cmd – command to be run on device, e.g. “ls /data/local/tmp”
Returns:the output of the shell cmd
Platforms:Android
start_app(package, activity=None)[source]

Start the target application on device

Parameters:
  • package – name of the package to be started, e.g. “com.netease.my”
  • activity – the activity to start, default is None which means the main activity
Returns:

None

Platforms:

Android, iOS

stop_app(package)[source]

Stop the target application on device

Parameters:package – name of the package to stop, see also start_app
Returns:None
Platforms:Android, iOS
clear_app(package)[source]

Clear data of the target application on device

Parameters:package – name of the package, see also start_app
Returns:None
Platforms:Android, iOS
install(filepath)[source]

Install application on device

Parameters:filepath – the path to file to be installed on target device
Returns:None
Platforms:Android, iOS
uninstall(package)[source]

Uninstall application on device

Parameters:package – name of the package, see also start_app
Returns:None
Platforms:Android, iOS
snapshot(filename=None, msg='')[source]

Take the screenshot of the target device and save it to the file.

Parameters:
  • filename – name of the file where to save the screenshot. If the relative path is provided, the default location is ST.LOG_DIR
  • msg – short description for screenshot, it will be recorded in the report
Returns:

absolute path of the screenshot

Platforms:

Android, iOS, Windows

wake()[source]

Wake up and unlock the target device

Returns:None
Platforms:Android, iOS

Note

Might not work on some models

home()[source]

Return to the home screen of the target device.

Returns:None
Platforms:Android, iOS
touch(v, times=1, **kwargs)[source]

Perform the touch action on the device screen

Parameters:
  • v – target to touch, either a Template instance or absolute coordinates (x, y)
  • times – how many touches to be performed
  • kwargs – platform specific kwargs, please refer to corresponding docs
Returns:

finial position to be clicked

Platforms:

Android, Windows, iOS

click(v, times=1, **kwargs)

Perform the touch action on the device screen

Parameters:
  • v – target to touch, either a Template instance or absolute coordinates (x, y)
  • times – how many touches to be performed
  • kwargs – platform specific kwargs, please refer to corresponding docs
Returns:

finial position to be clicked

Platforms:

Android, Windows, iOS

double_click(v)[source]
swipe(v1, v2=None, vector=None, **kwargs)[source]

Perform the swipe action on the device screen.

There are two ways of assigning the parameters
  • swipe(v1, v2=Template(...)) # swipe from v1 to v2
  • swipe(v1, vector=(x, y)) # swipe starts at v1 and moves along the vector.
Parameters:
  • v1 – the start point of swipe, either a Template instance or absolute coordinates (x, y)
  • v2 – the end point of swipe, either a Template instance or absolute coordinates (x, y)
  • vector – a vector coordinates of swipe action, either absolute coordinates (x, y) or percentage of screen e.g.(0.5, 0.5)
  • **kwargs

    platform specific kwargs, please refer to corresponding docs

Raises:

Exception – general exception when not enough parameters to perform swap action have been provided

Returns:

Origin position and target position

Platforms:

Android, Windows, iOS

pinch(in_or_out='in', center=None, percent=0.5)[source]

Perform the pinch action on the device screen

Parameters:
  • in_or_out – pinch in or pinch out, enum in [“in”, “out”]
  • center – center of pinch action, default as None which is the center of the screen
  • percent – percentage of the screen of pinch action, default is 0.5
Returns:

None

Platforms:

Android

keyevent(keyname, **kwargs)[source]

Perform key event on the device

Parameters:
  • keyname – platform specific key name
  • **kwargs

    platform specific kwargs, please refer to corresponding docs

Returns:

None

Platforms:

Android, Windows, iOS

text(text, enter=True)[source]

Input text on the target device. Text input widget must be active first.

Parameters:
  • text – text to input, unicode is supported
  • enter – input Enter keyevent after text input, default is True
Returns:

None

Platforms:

Android, Windows, iOS

sleep(secs=1.0)[source]

Set the sleep interval. It will be recorded in the report

Parameters:secs – seconds to sleep
Returns:None
Platforms:Android, Windows, iOS
wait(v, timeout=None, interval=0.5, intervalfunc=None)[source]

Wait to match the Template on the device screen

Parameters:
  • v – target object to wait for, Template instance
  • timeout – time interval to wait for the match, default is None which is ST.FIND_TIMEOUT
  • interval – time interval in seconds to attempt to find a match
  • intervalfunc – called after each unsuccessful attempt to find the corresponding match
Raises:

TargetNotFoundError – raised if target is not found after the time limit expired

Returns:

coordinates of the matched target

Platforms:

Android, Windows, iOS

exists(v)[source]

Check whether given target exists on device screen

Parameters:v – target to be checked
Returns:False if target is not found, otherwise returns the coordinates of the target
Platforms:Android, Windows, iOS
find_all(v)[source]

Find all occurrences of the target on the device screen and return their coordinates

Parameters:v – target to find
Returns:list of coordinates, [(x, y), (x1, y1), …]
Platforms:Android, Windows, iOS
assert_exists(v, msg='')[source]

Assert target exists on device screen

Parameters:
  • v – target to be checked
  • msg – short description of assertion, it will be recorded in the report
Raises:

AssertionError – if assertion fails

Returns:

coordinates of the target

Platforms:

Android, Windows, iOS

assert_not_exists(v, msg='')[source]

Assert target does not exist on device screen

Parameters:
  • v – target to be checked
  • msg – short description of assertion, it will be recorded in the report
Raises:

AssertionError – if assertion fails

Returns:

None.

Platforms:

Android, Windows, iOS

assert_equal(first, second, msg='')[source]

Assert two values are equal

Parameters:
  • first – first value
  • second – second value
  • msg – short description of assertion, it will be recorded in the report
Raises:

AssertionError – if assertion fails

Returns:

None

Platforms:

Android, Windows, iOS

assert_not_equal(first, second, msg='')[source]

Assert two values are not equal

Parameters:
  • first – first value
  • second – second value
  • msg – short description of assertion, it will be recorded in the report
Raises:

AssertionError – if assertion

Returns:

None

Platforms:

Android, Windows, iOS

5.1.3 Airtest API列表