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'¶
-
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
Raises: 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
Raises: DeviceConnectionError
– if any error occurs when connecting the deviceAdbError
– if any other adb error occurs
返回: command(s) standard output (stdout)
-
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
-
get_status
()[源代码]¶ Perform adb get-state and return the device status
Raises: 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 Raises: DeviceConnectionError
– if device is not available after timeout返回: 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 Raises: 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
-
sdk_version
¶
-
push
(local, remote)[源代码]¶ Perform adb push command
参数: - local – local file to be copied to the device
- remote – destination on the device where the file will be copied
返回: None
-
pull
(remote, local)[源代码]¶ 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
返回: 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
Yields: 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
(**kwargs)¶
-
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)[源代码]¶ 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
返回: 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
-
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
Raises: 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
Yields: 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 Raises: AdbShellError if no such file
-
line_breaker
¶ Set carriage return and line break property for various platforms and SDK versions
返回: 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
返回: 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
Raises: 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
Raises: 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
Raises: 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
Raises: AdbShellError
– if any adb error occursAirtestError
– if package is not found on the device
返回: path to the package
-
check_app
(package)[源代码]¶ Perform adb shell dumpsys package command and check if package exists on the device
参数: package – package name Raises: 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
-
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_device_info
()[源代码]¶ Get android device information, including: memory/storage/display/cpu/gpu/model/manufacturer…
返回: Dict of info
-
get_display_of_all_screen
(info)[源代码]¶ Perform adb shell dumpsys window windows commands to get window display of application.
参数: info – device screen properties 返回: None if adb command failed to run, otherwise return device screen properties(portrait mode) eg. (offset_x, offset_y, screen_width, screen_height)
-