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_setupauto_setup()For .py scripts:
from airtest.core.api import cli_setupcli_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_deviceconnect_device("Android:///")Initialize device connection (alternative entry point):
from airtest.core.api import init_deviceinit_device()Get the current device object:
from airtest.core.api import devicecurrent_device = device()Set the active device when multiple devices are connected:
from airtest.core.api import set_currentset_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_appstart_app("com.example.myapp")Stop an app:
from airtest.core.api import stop_appstop_app("com.example.myapp")Clear app data (cache, config, etc.):
from airtest.core.api import clear_appclear_app("com.example.myapp")Install an APK:
from airtest.core.api import installinstall("path/to/app.apk")Uninstall an app:
from airtest.core.api import uninstalluninstall("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 snapshotsnapshot("screenshot.png")Simulate a tap at coordinates (x, y):
from airtest.core.api import touchtouch((100, 200))Swipe from a start point to an end point:
from airtest.core.api import swipeswipe((100, 200), (300, 400))Enter text into an input field:
from airtest.core.api import texttext("Hello, World!")Pause execution for a number of seconds:
from airtest.core.api import sleepsleep(2)Wait for a specific image to appear on screen:
from airtest.core.api import waitwait("target_screenshot.png")Check whether an element exists on the screen:
from airtest.core.api import existsif exists("element_screenshot.png"):
print("元素存在")
else:
print("元素不存在")Find all matching elements on the screen:
from airtest.core.api import find_allelements = find_all("element_screenshot.png")
for element in elements:
print("找到元素,坐标:", element)Send a key event (e.g., BACK button):
from airtest.core.api import keyeventkeyevent("BACK")Wake the device screen:
from airtest.core.api import wakewake()Return to the home screen:
from airtest.core.api import homehome()Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
