airtest.core.android.adb module

class ADB(serialno=None, adb_path=None, server_addr=None, display_id=None, input_event=None)[源代码]

基类:object

adb client object class

status_device = 'device'
status_offline = 'offline'
SHELL_ENCODING = 'utf-8'
static get_adb_path()[源代码]

Returns the path to the adb executable.

Checks if adb process is running, returns the running process path.

If adb process is not running, checks if ANDROID_HOME environment variable is set. Constructs the adb path using ANDROID_HOME and returns it if set.

If adb process is not running and ANDROID_HOME is not set, uses built-in adb path.

返回:

The path to the adb executable.

返回类型:

str

static builtin_adb_path()[源代码]

Return built-in adb executable path

返回:

adb executable path

start_server()[源代码]

Perform adb start-server command to start the adb server

返回:

None

kill_server()[源代码]

Perform adb kill-server command to kill the adb server

返回:

None

version()[源代码]

Perform adb version command and return the command output

返回:

command output

start_cmd(cmds, device=True)[源代码]

Start a subprocess with adb command(s)

参数:
  • cmds – command(s) to be run

  • device – if True, the device serial number must be specified by -s serialno argument

抛出:

RuntimeError – if device is True and serialno is not specified

返回:

a subprocess

cmd(cmds, device=True, ensure_unicode=True, timeout=None)[源代码]

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

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

  • timeout – timeout in seconds

抛出:
返回:

command(s) standard output (stdout)

close_proc_pipe(proc)[源代码]

close stdin/stdout/stderr of subprocess.Popen.

devices(state=None)[源代码]

Perform adb devices command and return the list of adb devices

参数:

state – optional parameter to filter devices in specific state

返回:

list od adb devices

connect(force=False)[源代码]

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

参数:

force – force connection, default is False

返回:

None

disconnect()[源代码]

Perform adb disconnect command

返回:

None

get_status()[源代码]

Perform adb get-state and return the device status

抛出:

AdbError – if status cannot be obtained from the device

返回:

None if status is not found, otherwise return the standard output from adb get-state command

wait_for_device(timeout=5)[源代码]

Perform adb wait-for-device command

参数:

timeout – time interval in seconds to wait for device

抛出:

DeviceConnectionError – if device is not available after timeout

返回:

None

start_shell(cmds)[源代码]

Handle adb shell command(s)

参数:

cmds – adb shell command(s)

返回:

None

raw_shell(cmds, ensure_unicode=True)[源代码]

Handle adb shell command(s) with unicode support

参数:
  • cmds – adb shell command(s)

  • ensure_unicode – decode/encode unicode True or False, default is True

返回:

command(s) output

shell(cmd)[源代码]

Run the adb shell command on the device

参数:

cmd – a command to be run

抛出:

AdbShellError – if command return value is non-zero or if any other AdbError occurred

返回:

command output

keyevent(keyname)[源代码]

Perform adb shell input keyevent command on the device

参数:

keyname – key event name

返回:

None

getprop(key, strip=True)[源代码]

Perform adb shell getprop on the device

参数:
  • key – key value for property

  • strip – True or False to strip the return carriage and line break from returned string

返回:

propery value

property sdk_version

Get the SDK version from the device

返回:

SDK version

push(local, remote)[源代码]

Perform adb push command

备注

If there is a space (or special symbol) in the file name, it will be forced to add escape characters, and the new file name will be added with quotation marks and returned as the return value

注意:文件名中如果带有空格(或特殊符号),将会被强制增加转义符,并将新的文件名添加引号,作为返回值返回

参数:
  • local – local file to be copied to the device

  • remote – destination on the device where the file will be copied

返回:

The file path saved in the phone may be enclosed in quotation marks, eg. ‘“testfile.txt”’

示例

>>> adb = device().adb
>>> adb.push("test.txt", "/data/local/tmp")
>>> new_name = adb.push("test space.txt", "/data/local/tmp")  # test the space in file name
>>> print(new_name)
"/data/local/tmp/test\ space.txt"
>>> adb.shell("rm " + new_name)
pull(remote, local)[源代码]

Perform adb pull command

参数:
  • remote – remote file to be downloaded from the device

  • local – local destination where the file will be downloaded from the device

备注

If <=PY3, the path in Windows cannot be the root directory, and cannot contain symbols such as /g in the path 注意:如果低于PY3,windows中路径不能为根目录,并且不能包含/g等符号在路径里

返回:

None

forward(local, remote, no_rebind=True)[源代码]

Perform adb forward command

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

返回:

None

get_forwards()[源代码]

Perform `adb forward –list`command

生成器:

serial number, local tcp port, remote tcp port

返回:

None

classmethod get_available_forward_local()[源代码]

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

返回:

integer between 11111 and 20000

备注

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

setup_forward(device_port, no_rebind=True)[源代码]

Generate pseudo random local port and check if the port is available.

参数:
  • device_port – it can be string or the value of the function(localport), e.g. “tcp:5001” or “localabstract:{}”.format

  • no_rebind – adb forward –no-rebind option

返回:

local port and device port

remove_forward(local=None)[源代码]

Perform adb forward –remove command

参数:

local – local tcp port

返回:

None

install_app(filepath, replace=False, install_options=None)[源代码]

Perform adb install command

参数:
  • filepath – full path to file to be installed on the device

  • replace

    force to replace existing application, default is False

    e.g.[“-t”, # allow test packages

    ”-l”, # forward lock application, “-s”, # install application on sdcard, “-d”, # allow version code downgrade (debuggable packages only) “-g”, # grant all runtime permissions

    ]

返回:

command output

install_multiple_app(filepath, replace=False, install_options=None)[源代码]

Perform adb install-multiple command

参数:
  • filepath – full path to file to be installed on the device

  • replace – force to replace existing application, default is False

  • install_options

    list of options e.g.[“-t”, # allow test packages

    ”-l”, # forward lock application, “-s”, # install application on sdcard, “-d”, # allow version code downgrade (debuggable packages only) “-g”, # grant all runtime permissions “-p”, # partial application install (install-multiple only)

    ]

返回:

command output

pm_install(filepath, replace=False, install_options=None)[源代码]

Perform adb push and adb install commands

备注

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

参数:
  • filepath – full path to file to be installed on the device

  • replace – force to replace existing application, default is False

  • install_options

    list of options e.g.[“-t”, # allow test packages

    ”-l”, # forward lock application, “-s”, # install application on sdcard, “-d”, # allow version code downgrade (debuggable packages only) “-g”, # grant all runtime permissions

    ]

返回:

None

uninstall_app(package)[源代码]

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

返回:

command output

pm_uninstall(package, keepdata=False)[源代码]

Perform adb uninstall command and delete all related application data

参数:
  • package – package name to be uninstalled from the device

  • keepdata – True or False, keep application data after removing the app from the device

返回:

command output

snapshot()[源代码]

Take the screenshot of the device display

返回:

command output (stdout)

touch(tuple_xy)[源代码]

Perform user input (touchscreen) on given coordinates

参数:

tuple_xy – coordinates (x, y)

返回:

None

swipe(tuple_x0y0, tuple_x1y1, duration=500)[源代码]

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

参数:
  • tuple_x0y0 – start point coordinates (x, y)

  • tuple_x1y1 – end point coordinates (x, y)

  • duration – time interval for action, default 500

抛出:

AirtestError – if SDK version is not supported

返回:

None

logcat(grep_str='', extra_args='', read_timeout=10)[源代码]

Perform adb shell logcat command and search for given patterns

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

生成器:

logcat lines containing filtered patterns

返回:

None

exists_file(filepath)[源代码]

Check if the file exits on the device

参数:

filepath – path to the file

返回:

True or False if file found or not

file_size(filepath)[源代码]

Get the file size

参数:

filepath – path to the file

返回:

The file size

抛出:

AdbShellError if no such file

property line_breaker

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

返回:

carriage return and line break string

property 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

返回:

device screen properties

get_display_info()[源代码]

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

返回:

device screen properties e.g {

’width’: 1440, ‘height’: 2960, ‘density’: 4.0, ‘orientation’: 3, ‘rotation’: 270, ‘max_x’: 4095, ‘max_y’: 4095

}

getMaxXY()[源代码]

Get device display maximum values for x and y coordinates

返回:

max x and max y coordinates

getRestrictedScreen()[源代码]

Get value for mRestrictedScreen (without black border / virtual keyboard)`

返回:

screen resolution mRestrictedScreen value as tuple (x, y)

getPhysicalDisplayInfo()[源代码]

Get value for display dimension and density from mPhysicalDisplayInfo value obtained from dumpsys command.

返回:

physical display info for dimension and density

getDisplayOrientation()[源代码]

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

返回:

display orientation information

update_cur_display(display_info)[源代码]

Some phones support resolution modification, try to get the modified resolution from dumpsys adb shell dumpsys window displays | find “cur=”

本方法虽然可以更好地获取到部分修改过分辨率的手机信息 但是会因为cur=(d+)x(d+)的数值在不同设备上width和height的顺序可能不同,导致横竖屏识别出现问题 airtest不再使用本方法作为通用的屏幕尺寸获取方法,但依然可用于部分设备获取当前被修改过的分辨率

示例

>>> # 部分三星和华为设备,若分辨率没有指定为最高,可能会导致点击偏移,可以用这个方式强制修改:
>>> # For some Samsung and Huawei devices, if the resolution is not specified as the highest,
>>> # it may cause click offset, which can be modified in this way:
>>> dev = device()
>>> info = dev.display_info
>>> info2 = dev.adb.update_cur_display(info)
>>> dev.display_info.update(info2)
参数:

display_info – the return of self.getPhysicalDisplayInfo()

返回:

display_info

get_top_activity()[源代码]

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

抛出:

AirtestError – if top activity cannot be obtained

返回:

(package_name, activity_name, pid)

返回类型:

top activity as a tuple

is_keyboard_shown()[源代码]

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

返回:

True or False whether the keyboard is shown or not

is_screenon()[源代码]

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

抛出:

AirtestError – if screen state can’t be detected

返回:

True or False whether the screen is turned on or off

is_locked()[源代码]

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

抛出:

AirtestError – if lock screen can’t be detected

返回:

True or False whether the screen is locked or not

unlock()[源代码]

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

返回:

None

警告

Might not work on all devices

get_package_version(package)[源代码]

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

参数:

package – package name

返回:

None if no info has been found, otherwise package version

list_app(third_only=False)[源代码]
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

参数:

third_only – print only third party packages

返回:

list of packages

path_app(package)[源代码]

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

参数:

package – package name

抛出:
返回:

path to the package

check_app(package)[源代码]

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

参数:

package – package name

抛出:

AirtestError – if package is not found

返回:

True if package has been found

start_app(package, activity=None)[源代码]

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

参数:
  • package – package name

  • activity – activity name

返回:

None

start_app_timing(package, activity)[源代码]

Start the application and activity, and measure time

参数:
  • package – package name

  • activity – activity name

返回:

app launch time

stop_app(package)[源代码]

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

参数:

package – package name

返回:

None

clear_app(package)[源代码]

Perform adb shell pm clear command to clear all application data

参数:

package – package name

返回:

None

text(content)[源代码]

Use adb shell input for text input

参数:

content – text to input

返回:

None

示例

>>> dev = connect_device("Android:///")
>>> dev.text("Hello World")
>>> dev.text("test123")
get_ip_address()[源代码]

Perform several set of commands to obtain the IP address.

  • adb shell netcfg | grep wlan0

  • adb shell ifconfig

  • adb getprop dhcp.wlan0.ipaddress

返回:

None if no IP address has been found, otherwise return the IP address

get_gateway_address()[源代码]
Perform several set of commands to obtain the gateway address.
  • adb getprop dhcp.wlan0.gateway

  • adb shell netcfg | grep wlan0

返回:

None if no gateway address has been found, otherwise return the gateway address

get_memory()[源代码]
get_storage()[源代码]
get_cpuinfo()[源代码]
get_cpufreq()[源代码]
get_cpuabi()[源代码]
get_gpu()[源代码]
get_model()[源代码]
get_manufacturer()[源代码]
get_device_info()[源代码]

Get android device information, including: memory/storage/display/cpu/gpu/model/manufacturer…

返回:

Dict of info

get_display_of_all_screen(info, package=None)[源代码]

Perform adb shell dumpsys window windows commands to get window display of application.

参数:
  • info – device screen properties

  • package – package name, default to the package of top activity

返回:

None if adb command failed to run, otherwise return device screen properties(portrait mode) eg. (offset_x, offset_y, screen_width, screen_height)

cleanup_adb_forward()[源代码]