Airtest Framework Documentation

repository·master·Indexed 27 days ago

https://github.com/airtestproject/airtest

A cross-platform UI automation framework for games and mobile apps using image recognition. It provides a Python API via airtest.core.api for device connection, app management, and simulated inputs, as well as a CLI for executing .air test cases, generating reports, and extracting script metadata.

Tokens
16.1K
Snippets
41
Records
107
Agent score
92%

What's inside Airtest

  1. Perform partial screenshots and image recognition

    master

    To work with specific regions of the screen, use aircv.crop_image() to create a partial screenshot. You can then use Template.match_in() to find an image within that specific cropped area.

    Note: Coordinates returned by match_in() are relative to the cropped image. To get absolute screen coordinates, you must add the offset used during cropping.

    from airtest.core.api import *
    from airtest.aircv import *
    
    auto_setup(__file__)
    screen = G.DEVICE.snapshot()
    
    # 1. Create a partial screenshot using coordinates (x, y, width, height)
    local_screen = aircv.crop_image(screen, (0, 949, 1067, 1500))
    
    # 2. Define target and find it in the partial screenshot
    template = Template(r"png_code/settings.png")
    pos = template.match_in(local_screen)
    
    # 3. Convert relative pos to absolute screen coordinates
    # (pos[0] + offset_x, pos[1] + offset_y)
    print(pos[0] + 0, pos[1] + 949)
    from airtest.core.api import *
    from airtest.aircv import *
    auto_setup(__file__)
    
    screen = G.DEVICE.snapshot()
    # Partial screenshot
    local_screen = aircv.crop_image(screen,(0,949,1067,1500))
    
    # Set our target screenshot as a Template object
    tempalte = Template(r"png_code/settings.png")
    # Find the specified image object in the partial screenshot
    pos = tempalte.match_in(local_screen)
    
    # Return the coordinates of the image object found (the coordinates are relative to the coordinates of the local screenshot)
    print(pos)
    
    # To return the coordinates of the target in the entire screen, both x and y need to be added with the minimum x and y set during the partial screenshot
    print(pos[0]+0,pos[1]+949)
  2. Import functions from other .air scripts using using()

    master

    You can modularize your automation by writing common functions in one .air script and importing them into others. Use the using API from airtest.core.api to manage context changes, which automatically handles sys.path updates and Template search path adjustments.

    To use it, pass the path to the .air script to the using() function, then import your desired functions as standard Python modules.

    from airtest.core.api import using
    using("common.air")
    
    from common import common_function
    
    common_function()
  3. Verify Android device connection with ADB

    master

    Airtest includes adb executables for all platforms. You can use them to verify if your device is recognized.

    Windows: Navigate to airtest\airtest\core\android\static\adb\windows and run:

    E:\airtest\airtest\core\android\static\adb\windows>adb devices

    MAC: Navigate to airtest/core/android/static/adb/mac. If the adb binary lacks executable permissions, run chmod +x adb before executing:

    ./adb devices

    Devices with the status device are online and ready for use.

    List of devices attached
    c2b1c2a7        device
  4. Connect to Android devices

    master

    Airtest is compatible with most Android phones (Android version 2.3 to 11) and certain tablets.

    Xiaomi (MIUI 11 or above): To connect to Xiaomi devices running MIUI 11 or higher, you must use the cap_method=JAVACAP mode.

    If you encounter connection issues, check manufacturer-specific settings for your device.

  5. Connect to Android devices in code or CLI

    master

    Android devices are identified using the connection string format: Android://<adbhost>:<adbport>/<serialno>

    • adbhost: IP of the host where the ADB server is located (default: 127.0.0.1).
    • adbport: ADB port (default: 5037).
    • serialno: The device serial number obtained from adb devices.

    Connection Examples

    • Default (first available device): Android:///
    • Specific device: Android://127.0.0.1:5037/c2b1c2a7
    • Remote device via ADB connect: Android://127.0.0.1:5037/10.254.60.1:5555 (where the last part is the remote serial/IP).

    Usage

    In Python code:

    from airtest.core.api import *
    connect_device("Android:///c2b1c2a7")

    From Command Line:

    airtest run untitled.air --device Android:///c2b1c2a7 --log log/
    from airtest.core.api import *
    connect_device("Android:///Phone Serial Number")
  6. Connect to an Android device via ADB

    master

    To connect an Android device without AirtestIDE, ensure Developer Options and USB Debugging are enabled on the phone. You can verify the connection using the adb devices command.

    Airtest provides bundled adb executables in the following directories:

    • Windows: airtest/core/android/static/adb/windows/adb.exe
    • Mac: airtest/core/android/static/adb/mac/adb (Note: You may need to run chmod +x adb to grant execution permissions).

    If a device shows as unauthorized in the adb devices list, you must accept the USB debugging prompt on the phone's screen. If no devices appear, ensure the official manufacturer drivers are installed on your PC.

    adb devices