airtest.core.android.adb module

class ADB(serialno=None, adb_path=None, server_addr=None)[source]

Bases: object

adb client object class

status_device = 'device'
status_offline = 'offline'
SHELL_ENCODING = 'utf-8'
static builtin_adb_path()[source]

Return built-in adb executable path

Returns:adb executable path
start_server()[source]

Perform adb start-server command to start the adb server

Returns:None
kill_server()[source]

Perform adb kill-server command to kill the adb server

Returns:None
version()[source]

Perform adb version command and return the command output

Returns:command output
start_cmd(cmds, device=True)[source]

Start a subprocess with adb command(s)

Parameters:
  • cmds – command(s) to be run
  • device – if True, the device serial number must be specified by -s serialno argument
Raises:

RuntimeError – if device is True and serialno is not specified

Returns:

a subprocess

cmd(cmds, device=True, ensure_unicode=True)[source]

Run the adb command(s) in subprocess and return the standard output

Parameters:
  • cmds – command(s) to be run
  • device – if True, the device serial number must be specified by -s serialno argument
  • ensure_unicode – encode/decode unicode of standard outputs (stdout, stderr)
Raises:
  • DeviceConnectionError – if any error occurs when connecting the device
  • AdbError – if any other adb error occurs
Returns:

command(s) standard output (stdout)

devices(state=None)[source]

Perform adb devices command and return the list of adb devices

Parameters:state – optional parameter to filter devices in specific state
Returns:list od adb devices
connect(force=False)[source]

Perform adb connect command, remote devices are preferred to connect first

Parameters:force – force connection, default is False
Returns:None
disconnect()[source]

Perform adb disconnect command

Returns:None
get_status()[source]

Perform adb get-state and return the device status

Raises:AdbError – if status cannot be obtained from the device
Returns:None if status is not found, otherwise return the standard output from adb get-state command
wait_for_device(timeout=5)[source]

Perform adb wait-for-device command

Parameters:timeout – time interval in seconds to wait for device
Raises:DeviceConnectionError – if device is not available after timeout
Returns:None
start_shell(cmds)[source]

Handle adb shell command(s)

Parameters:cmds – adb shell command(s)
Returns:None
raw_shell(cmds, ensure_unicode=True)[source]

Handle adb shell command(s) with unicode support

Parameters:
  • cmds – adb shell command(s)
  • ensure_unicode – decode/encode unicode True or False, default is True
Returns:

command(s) output

shell(cmd)[source]

Run the adb shell command on the device

Parameters:cmd – a command to be run
Raises:AdbShellError – if command return value is non-zero or if any other AdbError occurred
Returns:command output
keyevent(keyname)[source]

Perform adb shell input keyevent command on the device

Parameters:keyname – key event name
Returns:None
getprop(key, strip=True)[source]

Perform adb shell getprop on the device

Parameters:
  • key – key value for property
  • strip – True or False to strip the return carriage and line break from returned string
Returns:

propery value

sdk_version

Get the SDK version from the device

Returns:SDK version
push(local, remote)[source]

Perform adb push command

Parameters:
  • local – local file to be copied to the device
  • remote – destination on the device where the file will be copied
Returns:

None

pull(remote, local)[source]

Perform adb pull command :param remote: remote file to be downloaded from the device :param local: local destination where the file will be downloaded from the device

Returns:None
forward(local, remote, no_rebind=True)[source]

Perform adb forward command

Parameters:
  • local – local tcp port to be forwarded
  • remote – tcp port of the device where the local tcp port will be forwarded
  • no_rebind – True or False
Returns:

None

get_forwards()[source]

Perform `adb forward –list`command

Yields:serial number, local tcp port, remote tcp port
Returns:None
classmethod get_available_forward_local()[source]

Generate a pseudo random number between 11111 and 20000 that will be used as local forward port

Returns:integer between 11111 and 20000

Note

use forward –no-rebind to check if port is available

setup_forward(*args, **kwargs)
remove_forward(local=None)[source]

Perform adb forward –remove command

Parameters:local – local tcp port
Returns:None
install_app(filepath, replace=False)[source]

Perform adb install command

Parameters:
  • filepath – full path to file to be installed on the device
  • replace – force to replace existing application, default is False
Returns:

command output

pm_install(filepath, replace=False)[source]

Perform adb push and adb install commands

Note

This is more reliable and recommended way of installing .apk files

Parameters:
  • filepath – full path to file to be installed on the device
  • replace – force to replace existing application, default is False
Returns:

None

uninstall_app(package)[source]

Perform adb uninstall command :param package: package name to be uninstalled from the device

Returns:command output
pm_uninstall(package, keepdata=False)[source]

Perform adb uninstall command and delete all related application data

Parameters:
  • package – package name to be uninstalled from the device
  • keepdata – True or False, keep application data after removing the app from the device
Returns:

command output

snapshot()[source]

Take the screenshot of the device display

Returns:command output (stdout)
touch(tuple_xy)[source]

Perform user input (touchscreen) on given coordinates

Parameters:tuple_xy – coordinates (x, y)
Returns:None
swipe(tuple_x0y0, tuple_x1y1, duration=500)[source]

Perform user input (swipe screen) from start point (x,y) to end point (x,y)

Parameters:
  • tuple_x0y0 – start point coordinates (x, y)
  • tuple_x1y1 – end point coordinates (x, y)
  • duration – time interval for action, default 500
Raises:

AirtestError – if SDK version is not supported

Returns:

None

logcat(grep_str='', extra_args='', read_timeout=10)[source]

Perform adb shell logcat command and search for given patterns

Parameters:
  • grep_str – pattern to filter from the logcat output
  • extra_args – additional logcat arguments
  • read_timeout – time interval to read the logcat, default is 10
Yields:

logcat lines containing filtered patterns

Returns:

None

exists_file(filepath)[source]

Check if the file exits on the device

Parameters:filepath – path to the file
Returns:True or False if file found or not
line_breaker

Set carriage return and line break property for various platforms and SDK versions

Returns:carriage return and line break string
display_info

Set device display properties (orientation, rotation and max values for x and y coordinates)

Notes: if there is a lock screen detected, the function tries to unlock the device first

Returns:device screen properties
get_display_info()[source]

Get information about device physical display (orientation, rotation and max values for x and y coordinates)

Returns:device screen properties
getPhysicalDisplayInfo()[source]
Display size and resolution to be obtained:
  1. physical display size (physical_width, physical_height) of the device - this is used by minitouch as a coordinates system
  1. screen effective resolution (width, height) - this is used by game image adaptation as a coordinates system
  1. click range resolution (max_x, max_y)
Returns:display size and resolution information
getDisplayOrientation()[source]

Another way to get the display orientation, this works well for older devices (SDK version 15)

Returns:display orientation information
get_top_activity()[source]

Perform adb shell dumpsys activity top command search for the top activity

Raises:AirtestError – if top activity cannot be obtained
Returns:top activity as a tuple
is_keyboard_shown()[source]

Perform adb shell dumpsys input_method command and search for information if keyboard is shown

Returns:True or False whether the keyboard is shown or not
is_screenon()[source]

Perform adb shell dumpsys window policy command and search for information if screen is turned on or off

Raises:AirtestError – if screen state can’t be detected
Returns:True or False whether the screen is turned on or off
is_locked()[source]

Perform adb shell dumpsys window policy command and search for information if screen is locked or not

Raises:AirtestError – if lock screen can’t be detected
Returns:True or False whether the screen is locked or not

Notes

Does not work on Xiaomi 2S

unlock()[source]

Perform adb shell input keyevent MENU and adb shell input keyevent BACK commands to attempt to unlock the screen

Returns:None

Warning

Might not work on all devices

get_package_version(package)[source]

Perform adb shell dumpsys package and search for information about given package version

Parameters:package – package name
Returns:None if no info has been found, otherwise package version
list_app(third_only=False)[source]
Perform adb shell pm list packages to print all packages, optionally only
those whose package name contains the text in FILTER.
Options
-f: see their associated file -d: filter to only show disabled packages -e: filter to only show enabled packages -s: filter to only show system packages -3: filter to only show third party packages -i: see the installer for the packages -u: also include uninstalled packages
Parameters:third_only – print only third party packages
Returns:list of packages
path_app(package)[source]

Perform adb shell pm path command to print the path to the package

Parameters:

package – package name

Raises:
  • AdbShellError – if any adb error occurs
  • AirtestError – if package is not found on the device
Returns:

path to the package

check_app(package)[source]

Perform adb shell dumpsys package command and check if package exists on the device

Parameters:package – package name
Raises:AirtestError – if package is not found
Returns:True if package has been found
start_app(package, activity=None)[source]

Perform adb shell monkey commands to start the application, if activity argument is None, then adb shell am start command is used.

Parameters:
  • package – package name
  • activity – activity name
Returns:

None

stop_app(package)[source]

Perform adb shell am force-stop command to force stop the application

Parameters:package – package name
Returns:None
clear_app(package)[source]

Perform adb shell pm clear command to clear all application data

Parameters:package – package name
Returns:None
get_ip_address()[source]
Perform several set of commands to obtain the IP address
  • adb shell netcfg | grep wlan0
  • adb shell ifconfig
  • adb getprop dhcp.wlan0.ipaddress
Returns:None if no IP address has been found, otherwise return the IP address
get_gateway_address()[source]
Perform several set of commands to obtain the gateway address
  • adb getprop dhcp.wlan0.gateway
  • adb shell netcfg | grep wlan0
Returns:None if no gateway address has been found, otherwise return the gateway address
cleanup_adb_forward()[source]