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)[源代码]
基类:
Device
Android Device Class
- property touch_proxy
根据self.touch_method的类型,执行对应的触摸操作
Module:
airtest.core.android.touch_methods.touch_proxy.TouchProxy
- 返回:
TouchProxy
示例
>>> 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`
- 返回:
“MINITOUCH” or “MAXTOUCH”
示例
>>> dev = Android() >>> print(dev.touch_method) # "MINITOUCH"
- property cap_method
In order to be compatible with the previous dev.cap_method
为了兼容以前的`dev.cap_method`
- 返回:
“MINICAP” or “JAVACAP”
示例
>>> 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())
示例
>>> 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)[源代码]
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”))
- 参数:
old_name – “minicap”
new_name – “screen_proxy”
- 返回:
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()
示例
>>> dev = Android() >>> isinstance(dev.minicap, ScreenProxy) # True >>> dev.minicap.get_frame_from_stream() # --> dev.screen_proxy.get_frame_from_stream()
- property uuid
Serial number
- 返回:
- check_app(package)[源代码]
检查package在设备中是否存在
- 参数:
package – package name
- 返回:
如果存在,返回True
- 抛出:
AirtestError – raised if package is not found
- start_app(package, activity=None)[源代码]
启动应用
- 参数:
package – package name
activity – activity name
- 返回:
None
- start_app_timing(package, activity)[源代码]
启动应用,并且返回启动耗费时间
- 参数:
package – package name
activity – activity name
- 返回:
app启动时间
- install_app(filepath, replace=False, install_options=None)[源代码]
将应用安装到手机上
- 参数:
filepath – apk 文件在PC上的完整路径
replace – 如果应用已存在,是否替换
install_options – install命令的额外选项,默认是[]
- 返回:
安装进程的输出内容
- install_multiple_app(filepath, replace=False, install_options=None)[源代码]
Install multiple the application on the device
- 参数:
filepath – apk 文件在PC上的完整路径
replace – 如果应用已存在,是否替换
install_options – install命令的额外选项,默认是[]
- 返回:
安装进程的输出内容
- snapshot(filename=None, ensure_orientation=True, quality=10, max_size=None)[源代码]
截取一张当前手机画面,默认会发送到stdout
- 参数:
filename – 保存截图的文件名,默认为None的话将会发送到stdout
ensure_orientation – 截图方向是否要与当前显示情况一致,默认为True
quality – The image quality, integer in range [1, 99]
max_size – the maximum size of the picture, e.g 1200
- 返回:
截图输出
- shell(*args, **kwargs)[源代码]
返回 adb shell 解释器
- 参数:
*args – optional shell commands
**kwargs – optional shell commands
- 返回:
None
- keyevent(keyname, **kwargs)[源代码]
在设备上执行keyevent
- 参数:
keyname – keyevent name
**kwargs – optional arguments
- 返回:
None
- text(text, enter=True, **kwargs)[源代码]
向设备中输入字符串
- 参数:
text – text to input, will automatically replace single quotes with double quotes
enter – 是否按下`Enter`键
search – 是否要按下输入法键盘中的search键
- 返回:
None
- touch(pos, duration=0.01)[源代码]
在设备上执行点击
- 参数:
pos – coordinates (x, y)
duration – 点击屏幕的时长
示例
>>> dev = Android() # or dev = device() >>> dev.touch((100, 100)) # absolute coordinates >>> dev.touch((0.5, 0.5)) # relative coordinates
- 返回:
(x, y) # The coordinate position of the actual click
- swipe(p1, p2, duration=0.5, steps=5, fingers=1)[源代码]
在设备上执行滑动操作
- 参数:
p1 – 开始坐标
p2 – 结束坐标
duration – 在屏幕上滑动的时长,默认是0.5
steps – 滑动过程中的步数,默认为5
fingers – 滑动的手指数量,1或者2,默认为1
示例
>>> 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
- 返回:
(pos1, pos2)
- pinch(center=None, percent=0.5, duration=0.5, steps=5, in_or_out='in')[源代码]
在设备上执行pinch操作(放大缩小),仅适用于minitouch和maxtouch
- 参数:
center – pinch操作的中心点
percent – pinch操作捏合屏幕的距离,默认是0.5
duration – 滑动过程中的时间间隔,默认是0.8
steps – 滑动过程中的步数,默认为5
in_or_out – 向内捏合in或者向外捏合out,默认是’in’
- 返回:
None
- 抛出:
TypeError – An error occurred when center is not a list/tuple or None
- swipe_along(coordinates_list, duration=0.8, steps=5)[源代码]
执行一段连续的滑动操作,仅适用于minitouch和maxtouch
- 参数:
coordinates_list – 一个坐标的列表:[(x1, y1), (x2, y2), (x3, y3)]
duration – 滑动过程中的时间间隔,默认是0.8
steps – 滑动过程中的步数,默认为5
- 返回:
None
- two_finger_swipe(tuple_from_xy, tuple_to_xy, duration=0.8, steps=5, offset=(0, 50))[源代码]
执行两个手指一起滑动的操作,仅适用于minitouch和maxtouch
- 参数:
tuple_from_xy – 开始坐标
tuple_to_xy – 结束坐标
duration – 滑动过程中的时间间隔,默认是0.8
steps – 滑动过程中的步数,默认为5
offset – 第二个手指相对于第一个手指的偏移坐标,默认是(0, 50)
- 返回:
None
- logcat(*args, **kwargs)[源代码]
执行 logcat
- 参数:
*args – optional arguments
**kwargs – optional arguments
- 返回:
logcat 输出
- getprop(key, strip=True)[源代码]
根据传入的key获取properties内容
- 参数:
key – key name
strip – 是否对输出内容进行strip
- 返回:
property value(s)
- get_ip_address()[源代码]
执行以下几种命令行来获取IP地址
adb shell netcfg | grep wlan0
adb shell ifconfig
adb getprop dhcp.wlan0.ipaddress
- 返回:
如果获取IP失败,返回None,否则返回IP地址
- property display_info
返回显示信息(width, height, orientation 和 max_x, max_y)
- 返回:
显示信息
- get_render_resolution(refresh=False, package=None)[源代码]
返回旋转后的渲染分辨率
- 参数:
refresh – 是否强制刷新渲染分辨率
package – package name, default to the package of top activity
- 返回:
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)[源代码]
开始对设备画面进行录制
- 参数:
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.
- 返回:
path of video file
- 返回类型:
save_path
示例
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)
- get_clipboard()[源代码]
Get the clipboard content
- 返回:
clipboard content
示例
>>> 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