Airtest Automation API Reference: Initialization, Device Connection, App Operations, and Common Interactions
This article provides a comprehensive reference for Airtest's automation APIs, covering script initialization functions, device connection methods, application lifecycle commands, and frequently used interaction utilities such as screenshot, touch, swipe, text input, and key events, each illustrated with Python code examples.
Script initialization interfaces
Airtest offers two initialization functions that should be called at the beginning of a test script to set up global configurations, import required modules, and configure device connections.
For .air scripts:
from airtest.core.api import auto_setup auto_setup()For .py scripts:
from airtest.core.api import cli_setup cli_setup()Device connection and usage interfaces
These functions manage the connection to real devices or emulators, allowing parallel testing and device switching.
Connect a device by URI:
from airtest.core.api import connect_device connect_device("Android:///")Initialize device connection (alternative entry point):
from airtest.core.api import init_device init_device()Get the current device object:
from airtest.core.api import device current_device = device()Set the active device when multiple devices are connected:
from airtest.core.api import set_current set_current("Android:///")Application operation interfaces
Functions to control the lifecycle of the target app on the device.
Start an app:
from airtest.core.api import start_app start_app("com.example.myapp")Stop an app:
from airtest.core.api import stop_app stop_app("com.example.myapp")Clear app data (cache, config, etc.):
from airtest.core.api import clear_app clear_app("com.example.myapp")Install an APK:
from airtest.core.api import install install("path/to/app.apk")Uninstall an app:
from airtest.core.api import uninstall uninstall("com.example.myapp")Common simulated actions
These utilities emulate user interactions and verification steps during automated tests.
Take a screenshot:
from airtest.core.api import snapshot snapshot("screenshot.png")Simulate a tap at coordinates (x, y):
from airtest.core.api import touch touch((100, 200))Swipe from a start point to an end point:
from airtest.core.api import swipe swipe((100, 200), (300, 400))Enter text into an input field:
from airtest.core.api import text text("Hello, World!")Pause execution for a number of seconds:
from airtest.core.api import sleep sleep(2)Wait for a specific image to appear on screen:
from airtest.core.api import wait wait("target_screenshot.png")Check whether an element exists on the screen:
from airtest.core.api import exists if exists("element_screenshot.png"):
print("元素存在")
else:
print("元素不存在")Find all matching elements on the screen:
from airtest.core.api import find_all elements = find_all("element_screenshot.png")
for element in elements:
print("找到元素,坐标:", element)Send a key event (e.g., BACK button):
from airtest.core.api import keyevent keyevent("BACK")Wake the device screen:
from airtest.core.api import wake wake()Return to the home screen:
from airtest.core.api import home home()Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.