airtest.core.android.android module
- class Android(serialno=None, host=None, cap_method='MINICAP', touch_method='MINITOUCH', ime_method='YOSEMITEIME', ori_method='MINICAPORI', display_id=None, input_event=None, adb_path=None, name=None)[source]
Bases:
Device
Android Device Class
- property touch_proxy
Perform touch operation according to self.touch_method
Module:
airtest.core.android.touch_methods.touch_proxy.TouchProxy
- Returns:
TouchProxy
Examples
>>> dev = Android() >>> dev.touch_proxy.touch((100, 100)) # If the device uses minitouch, it is the same as dev.minitouch.touch >>> dev.touch_proxy.swipe_along([(0,0), (100, 100)])
- property touch_method
In order to be compatible with the previous dev.touch_method
为了兼容以前的`dev.touch_method`
- Returns:
“MINITOUCH” or “MAXTOUCH”
Examples
>>> dev = Android() >>> print(dev.touch_method) # "MINITOUCH"
- property cap_method
In order to be compatible with the previous dev.cap_method
为了兼容以前的`dev.cap_method`
- Returns:
“MINICAP” or “JAVACAP”
Examples
>>> dev = Android() >>> print(dev.cap_method) # "MINICAP"
- property screen_proxy
Similar to touch_proxy, it returns a proxy that can automatically initialize an available screenshot method, such as Minicap
Afterwards, you only need to call
self.screen_proxy.get_frame()
to get the screenshot类似touch_proxy,返回一个代理,能够自动初始化一个可用的屏幕截图方法,例如Minicap
后续只需要调用 ``self.screen_proxy.get_frame()``即可获取到屏幕截图
Returns: ScreenProxy(Minicap())
Examples
>>> dev = Android() >>> img = dev.screen_proxy.get_frame_from_stream() # dev.minicap.get_frame_from_stream() is deprecated
- get_deprecated_var(old_name, new_name)[source]
Get deprecated class variables
When the airtest version number>=1.1.2, the call device.minicap/device.javacap is removed, and relevant compatibility is made here, and DeprecationWarning is printed
airtest版本号>=1.1.2时,去掉了device.minicap/device.javacap这样的调用,在此做了相关的兼容,并打印DeprecationWarning
Usage: Android.minicap=property(lambda self: self.get_deprecated_var(“minicap”, “screen_proxy”))
- Parameters:
old_name – “minicap”
new_name – “screen_proxy”
- Returns:
New implementation of deprecated object, e.g self.minicap -> self.screen_proxy
dev.minicap.get_frame_from_stream() -> dev.screen_proxy.get_frame_from_stream()
Examples
>>> dev = Android() >>> isinstance(dev.minicap, ScreenProxy) # True >>> dev.minicap.get_frame_from_stream() # --> dev.screen_proxy.get_frame_from_stream()
- get_default_device(adb_path=None)[source]
Get local default device when no serialno
- Returns:
local device serialno
- property uuid
Serial number
- Returns:
- list_app(third_only=False)[source]
Return list of packages
- Parameters:
third_only – if True, only third party applications are listed
- Returns:
array of applications
- path_app(package)[source]
Print the full path to the package
- Parameters:
package – package name
- Returns:
the full path to the package
- check_app(package)[source]
Check if package exists on the device
- Parameters:
package – package name
- Returns:
True if package exists on the device
- Raises:
AirtestError – raised if package is not found
- start_app(package, activity=None)[source]
Start the application and activity
- Parameters:
package – package name
activity – activity name
- Returns:
None
- start_app_timing(package, activity)[source]
Start the application and activity, and measure time
- Parameters:
package – package name
activity – activity name
- Returns:
app launch time
- clear_app(package)[source]
Clear all application data
- Parameters:
package – package name
- Returns:
None
- install_app(filepath, replace=False, install_options=None)[source]
Install the application on the device
- Parameters:
filepath – full path to the apk file to be installed on the device
replace – True or False to replace the existing application
install_options – list of options, default is []
- Returns:
output from installation process
- install_multiple_app(filepath, replace=False, install_options=None)[source]
Install multiple the application on the device
- Parameters:
filepath – full path to the apk file to be installed on the device
replace – True or False to replace the existing application
install_options – list of options, default is []
- Returns:
output from installation process
- uninstall_app(package)[source]
Uninstall the application from the device
- Parameters:
package – package name
- Returns:
output from the uninstallation process
- snapshot(filename=None, ensure_orientation=True, quality=10, max_size=None)[source]
Take the screenshot of the display. The output is send to stdout by default.
- Parameters:
filename – name of the file where to store the screenshot, default is None which is stdout
ensure_orientation – True or False whether to keep the orientation same as display
quality – The image quality, integer in range [1, 99]
max_size – the maximum size of the picture, e.g 1200
- Returns:
screenshot output
- shell(*args, **kwargs)[source]
Return adb shell interpreter
- Parameters:
*args – optional shell commands
**kwargs – optional shell commands
- Returns:
None
- keyevent(keyname, **kwargs)[source]
Perform keyevent on the device
- Parameters:
keyname – keyevent name
**kwargs – optional arguments
- Returns:
None
- text(text, enter=True, **kwargs)[source]
Input text on the device
- Parameters:
text – text to input, will automatically replace single quotes with double quotes
enter – True or False whether to press Enter key
search – True or False whether to press Search key on IME after input
- Returns:
None
- touch(pos, duration=0.01)[source]
Perform touch event on the device
- Parameters:
pos – coordinates (x, y)
duration – how long to touch the screen
Examples
>>> dev = Android() # or dev = device() >>> dev.touch((100, 100)) # absolute coordinates >>> dev.touch((0.5, 0.5)) # relative coordinates
- Returns:
(x, y) # The coordinate position of the actual click
- swipe(p1, p2, duration=0.5, steps=5, fingers=1)[source]
Perform swipe event on the device
- Parameters:
p1 – start point
p2 – end point
duration – how long to swipe the screen, default 0.5
steps – how big is the swipe step, default 5
fingers – the number of fingers. 1 or 2.
Examples
>>> dev = Android() # or dev = device() >>> dev.swipe((100, 100), (200, 200)) # absolute coordinates >>> dev.swipe((0.1, 0.1), (0.2, 0.2)) # relative coordinates
- Returns:
(pos1, pos2)
- pinch(center=None, percent=0.5, duration=0.5, steps=5, in_or_out='in')[source]
Perform pinch event on the device, only for minitouch and maxtouch
- Parameters:
center – the center point of the pinch operation
percent – pinch distance to half of screen, default is 0.5
duration – time interval for swipe duration, default is 0.8
steps – size of swipe step, default is 5
in_or_out – pinch in or pinch out, default is ‘in’
- Returns:
None
- Raises:
TypeError – An error occurred when center is not a list/tuple or None
- swipe_along(coordinates_list, duration=0.8, steps=5)[source]
Perform swipe event across multiple points in sequence, only for minitouch and maxtouch
- Parameters:
coordinates_list – list of coordinates: [(x1, y1), (x2, y2), (x3, y3)]
duration – time interval for swipe duration, default is 0.8
steps – size of swipe step, default is 5
- Returns:
None
- two_finger_swipe(tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50))[source]
Perform two finger swipe action, only for minitouch and maxtouch
- Parameters:
tuple_from_xy – start point
tuple_to_xy – end point
duration – time interval for swipe duration, default is 0.8
steps – size of swipe step, default is 5
offset – coordinate offset of the second finger, default is (0, 50)
- Returns:
None
- logcat(*args, **kwargs)[source]
Perform `logcat`operations
- Parameters:
*args – optional arguments
**kwargs – optional arguments
- Returns:
logcat output
- getprop(key, strip=True)[source]
Get properties for given key
- Parameters:
key – key name
strip – True or False whether to strip the output or not
- Returns:
property value(s)
- 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
- is_keyboard_shown()[source]
Return True or False whether soft keyboard is shown or not
Notes
Might not work on all devices
- Returns:
True or False
- is_screenon()[source]
Return True or False whether the screen is on or not
Notes
Might not work on all devices
- Returns:
True or False
- is_locked()[source]
Return True or False whether the device is locked or not
Notes
Might not work on some devices
- Returns:
True or False
- property display_info
Return the display info (width, height, orientation and max_x, max_y)
- Returns:
display information
- get_display_info()[source]
Return the display info (width, height, orientation and max_x, max_y)
- Returns:
display information
- get_current_resolution()[source]
Return current resolution after rotation
- Returns:
width and height of the display
- get_render_resolution(refresh=False, package=None)[source]
Return render resolution after rotation
- Parameters:
refresh – whether to force refresh render resolution
package – package name, default to the package of top activity
- Returns:
offset_x, offset_y, offset_width and offset_height of the display
- start_recording(max_time=1800, output=None, fps=10, mode='yosemite', snapshot_sleep=0.001, orientation=0, bit_rate_level=None, bit_rate=None, max_size=None, *args, **kwargs)[source]
Start recording the device display
- Parameters:
max_time – maximum screen recording time, default is 1800
output – ouput file path
mode – the backend write video, choose in [“ffmpeg”, “yosemite”] yosemite: (default method) High clarity but large file size and limited phone compatibility. ffmpeg: Lower image quality but smaller files and better compatibility.
fps – frames per second will record
snapshot_sleep – (mode=”ffmpeg” only) sleep time for each snapshot.
orientation – 1: portrait, 2: landscape, 0: rotation, default is 0
bit_rate_level – (mode=”yosemite” only) bit_rate=resolution*level, 0 < level <= 5, default is 1
bit_rate – (mode=”yosemite” only) the higher the bitrate, the clearer the video
max_size – (mode=”ffmpeg” only) max size of the video frame, e.g.800, default is None. Smaller sizes lead to lower system load.
- Returns:
path of video file
- Return type:
save_path
Examples
Record 30 seconds of video and export to the current directory test.mp4:
>>> from airtest.core.api import connect_device, sleep >>> dev = connect_device("Android:///") >>> save_path = dev.start_recording(output="test.mp4") >>> sleep(30) >>> dev.stop_recording() >>> print(save_path)
>>> # the screen is portrait >>> portrait_mp4 = dev.start_recording(output="portrait.mp4", orientation=1) # or orientation="portrait" >>> sleep(30) >>> dev.stop_recording()
>>> # the screen is landscape >>> landscape_mp4 = dev.start_recording(output="landscape.mp4", orientation=2) # or orientation="landscape"
In ffmpeg mode, you can specify max_size to limit the video’s maximum width/length. Smaller video sizes result in lower CPU load.
>>> dev.start_recording(output="test.mp4", mode="ffmpeg", max_size=800)
- stop_recording(output=None, is_interrupted=None)[source]
Stop recording the device display. Recoding file will be kept in the device.
- get_clipboard()[source]
Get the clipboard content
- Returns:
clipboard content
Examples
>>> dev = Android() >>> dev.set_clipboard("hello world") >>> dev.get_clipboard() 'hello world' >>> dev.paste() # paste the clipboard content
- property javacap
- property maxtouch
- property minicap
- property minitouch