airtest app進階(一)純python調api 的應用


一、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)

截圖如下

其他操作事件就一一介紹了詳情參考上面連接接口文檔(點擊屏幕、滑動、放大縮小、鍵盤事件、輸入內容、等待和判斷、斷言)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM