一、API常用的一些方法
导入包:airtest.core.android.android.Android
其他包
windows:airtest.core.win.win.Windows
ios:airtest.core.ios.ios.IOS
linux:airtest.core.linux.linux.Linux
这些包其实都是继承:airtest.core.device.Device
以Android为例:
1、get_default_device:获取本地默认连接的设备
2、uuid:获取当前device 的UUID
3、list_app:列举所有的app
4、path_app:打印出某个app 的完整路径
5、check_app:检查某个app是否在设备上
6、start_app:启动某个app
7、start_app_timing: 启动某个app 后计算时间
8、stop_app:停止某个app
9、clear_app:清空某个app 的所有的数据
10、install_app:安装某个app
11、install_multiple_app: 安装多个app
12、uninstall_app:卸载某个app
13、snapshot:屏幕截图
14、shell :获取ADB Shell 执行结果
15、keyevent:执行键盘操作
16、wake :唤醒当前设备
17、home: 点击返回home界面
18、text:向设备里输入内容
19、touch:点击屏幕某处的位置
20、double_click: 双击屏幕的位置
21、swipe:滑动屏幕,由一点到另一点
22、pinch :手指捏合操作
23、logcat:日志记录操作
24、getprop:获得某个属性的值
25、get_ip_address:获取ip地址
26、get_top_activity:获取当前的activity
27、get_top_activity_name_and_pid:获取当前activity的名称和进程
28、get_top_activity_name:获取当前activity的名称
29、is_keyboard_shown:判断当前键盘是否出现
30、is_locked:设备是否锁定
31、unlock:解锁设备
32、display_info:显示当前信息,如:屏幕亮度
33、get_display_info :同display_info
34、get_current_resolution:获取当前设备的分辨率
35、get_render_resolution:获取当前渲染分辨率
36、start_recording:开始录制
37、stop_recording:结束录制
39、adjust_all_screen:调整屏幕适配分辨率
其他详情参考接口文档:airtest.core.android.android module — airtest 文档
手机连接
from airtest.core.android import Android
from airtest.core.api import *
#模拟器地址
# uri = "android://127.0.0.1:5037/127.0.0.1:21503?cap_method=JAVACAP&&ori_method=MINICAPORI&&touch_method=ADBTOUCH"
#连接真机
uri="Android://127.0.0.1:5037/9cbf4c3d?cap_method=JAVACAP&&ori_method=MINICAPORI&&touch_method=ADBTOUCH"
#9cbf4c3d真机的设备号
device: Android = connect_device(uri)
print(device)
z
一些部分其他操作(详情请参考接口文档或上面的方法):
app_list=device.list_app()# 获取所有安装的包
print(app_list)
uuid = device.uuid#获取设备号
print(uuid)
device.wake()#唤醒当前设备
print(device.is_locked())#是否锁定
device.unlock()#解锁设备
print(device.get_display_info())#当前设备信息
执行的结果:
所有的安装包:['com.android.providers.telephony', 'com.android.providers.calendar', 'com.android.providers.media', 'com.android.wallpapercropper', 'com.android.documentsui', 'com.android.externalstorage', 'com.android.htmlviewer', 'com.android.providers.downloads', 'com.android.browser', 'com.android.defcontainer', 'com.android.providers.downloads.ui', 'com.android.pacprocessor', 'com.netease.nie.yosemite', 'com.android.certinstaller', 'com.android.carrierconfig', 'android', 'jp.co.cyberagent.stf.rotationwatcher', 'com.android.backupconfirm', 'com.android.statementservice', 'com.android.providers.settings', 'com.android.sharedstoragebackup', 'com.android.webview', 'com.android.inputdevices', 'com.xunmeng.pinduoduo', 'com.kok.kuailong', 'android.ext.shared', 'com.android.server.telecom', 'com.android.keychain', 'com.android.printservice.recommendation', 'android.ext.services', 'com.android.calllogbackup', 'com.android.packageinstaller', 'com.android.proxyhandler', 'com.cyanogenmod.filemanager', 'com.android.smspush', 'com.netease.open.pocoservice', 'com.android.storagemanager', 'com.android.bookmarkprovider', 'com.android.settings', 'com.taobao.idlefish', 'com.netease.open.pocoservice.test', 'com.android.vpndialogs', 'com.android.phone', 'com.android.shell', 'com.android.wallpaperbackup', 'com.android.providers.blockednumber', 'com.android.providers.userdictionary', 'com.android.location.fused', 'com.android.systemui', 'com.android.bluetoothmidiservice', 'com.android.providers.contacts', 'com.android.captiveportallogin']
设备号(模拟器):127.0.0.1:21503
是否锁定:False
获取当前设备的(获取当前显示信息,如屏幕宽高等):{'width': 576, 'height': 1024, 'density': 1.19375, 'orientation': 0, 'rotation': 0, 'max_x': 576, 'max_y': 1024}
查询所有连接的设备:
def device():
"""
Return the current active device.
:return: current device instance
"""
return G.DEVICE
G.DEVICE_LSIT 返回的是一个list,G是一个全局变量
print(G.DEVICE_LIST)
[<airtest.core.android.android.Android object at 0x00000150EC2E21C0>]
切换device
使用set_current(idx)方法切换当前连接设备的device,传入的是index
def set_current(idx):
"""
Set current active device.
:param idx: uuid or index of initialized device instance
:raise IndexError: raised when device idx is not found
:return: None
:platforms: Android, iOS, Windows
"""
执行shell 命令
@logwrap
def shell(cmd):
"""
Start remote shell in the target device and execute the command
:param cmd: command to be run on device, e.g. "ls /data/local/tmp"
:return: the output of the shell cmd
:platforms: Android
"""
return G.DEVICE.shell(cmd)
直接调用 adb 命令就好了,例如获取内存信息就可以使用如下命令:
result = shell('cat /proc/meminfo')
print(result)
执行结果:
MemTotal: 3110972 kB
MemFree: 2363620 kB
MemAvailable: 2802780 kB
Buffers: 14020 kB
Cached: 461232 kB
SwapCached: 0 kB
Active: 375556 kB
Inactive: 325864 kB
Active(anon): 228404 kB
Inactive(anon): 9076 kB
Active(file): 147152 kB
Inactive(file): 316788 kB
Unevictable: 352 kB
Mlocked: 352 kB
HighTotal: 2236360 kB
HighFree: 1707624 kB
LowTotal: 874612 kB
LowFree: 655996 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 226520 kB
Mapped: 336756 kB
Shmem: 11324 kB
Slab: 23436 kB
SReclaimable: 8584 kB
SUnreclaim: 14852 kB
KernelStack: 4952 kB
PageTables: 6348 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1555484 kB
Committed_AS: 13280604 kB
VmallocTotal: 122880 kB
VmallocUsed: 55368 kB
VmallocChunk: 36588 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 4096 kB
DirectMap4k: 16376 kB
DirectMap4M: 892928 kB
停止和启动app
@logwrap
def start_app(package, activity=None):
"""
Start the target application on device
:param package: name of the package to be started, e.g. "com.netease.my"
:param activity: the activity to start, default is None which means the main activity
:return: None
:platforms: Android, iOS
"""
G.DEVICE.start_app(package, activity)
@logwrap
def stop_app(package):
"""
Stop the target application on device
:param package: name of the package to stop, see also `start_app`
:return: None
:platforms: Android, iOS
"""
G.DEVICE.stop_app(package)
执行“快龙app” start_app 和stop_app
package ="com.kok.kuailong"
start_app(package)
sleep(10)
stop_app(package)
执行结果:
安装和卸载
安装和卸载也是一样,也是调用了 device 的 install 和 uninstall 方法,定义如下:
@logwrap
def install(filepath, **kwargs):
"""
Install application on device
:param filepath: the path to file to be installed on target device
:param kwargs: platform specific `kwargs`, please refer to corresponding docs
:return: None
:platforms: Android
"""
return G.DEVICE.install_app(filepath, **kwargs)
@logwrap
def uninstall(package):
"""
Uninstall application on device
:param package: name of the package, see also `start_app`
:return: None
:platforms: Android
"""
return G.DEVICE.uninstall_app(package)
截图
截图使用 snapshot 即可完成,可以设定存储的文件名称,图片质量等。 定义如下:
ef snapshot(filename=None, msg="", quality=ST.SNAPSHOT_QUALITY):
"""
Take the screenshot of the target device and save it to the file.
:param filename: name of the file where to save the screenshot. If the relative path is provided, the default
location is ``ST.LOG_DIR``
:param msg: short description for screenshot, it will be recorded in the report
:param quality: The image quality, integer in range [1, 99]
:return: absolute path of the screenshot
:platforms: Android, iOS, Windows
"""
对kuailong app 运行进行截图
package ="com.kok.kuailong"
start_app(package)
sleep(3)
# stop_app(package)
snapshot("kuailong.png",quality=30)
截图如下
其他操作事件就一一介绍了详情参考上面连接接口文档(点击屏幕、滑动、放大缩小、键盘事件、输入内容、等待和判断、断言)