airtest.core.api module

This module contains the Airtest Core APIs.

connect_device(uri)[source]

Initialize device with uri and set the device as the current one.

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 adb device using custom params
  • windows:/// # local Windows application
  • ios:/// # iOS device
Platforms:

Android, iOS, Windows

device()[source]

Return the current active device.

Returns:current device instance
Platforms:Android, iOS, Windows
set_current(*args, **kwargs)[source]

Set current active device.

Parameters:index – index of initialized device instance
Raises:IndexError – raised when device index is out of device list
Returns:None
Platforms:Android, iOS, Windows
auto_setup(filepath)[source]

Auto setup G.BASEDIR and try connect android device if not connected to any device.

shell(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[source]

Install application on device

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

Uninstall application on device

Parameters:package – name of the package, see also start_app
Returns:None
Platforms:Android, iOS
snapshot(*args, **kwargs)[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(*args, **kwargs)[source]

Wake up and unlock the target device

Returns:None
Platforms:Android, iOS

Note

Might not work on some models

home(*args, **kwargs)[source]

Return to the home screen of the target device.

Returns:None
Platforms:Android, iOS
touch(*args, **kwargs)[source]

Perform the touch action on the device screen

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

None

Platforms:

Android, Windows, iOS

swipe(*args, **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:

None

Platforms:

Android, Windows, iOS

pinch(*args, **kwargs)[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(*args, **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(*args, **kwargs)[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(*args, **kwargs)[source]

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

Parameters:secs – seconds to sleep
Returns:None
Platforms:Android, Windows, iOS
wait(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[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(*args, **kwargs)[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