Master Airtest: Essential APIs for Mobile Automation Testing
This article introduces the Airtest mobile automation IDE, explains its cross‑platform and platform‑specific APIs, and provides detailed code examples for device initialization, control, app management, screen actions, and assertions, enabling developers to automate mobile testing efficiently.
Introduction
Hello, I’m “PiPi”. This article introduces the mobile automation testing IDE Airtest and walks through its core API usage.
Basic API Overview
Airtest generates script code that imports cross‑platform APIs and platform‑specific modules such as airtest.core.android, airtest.core.ios, and airtest.core.win.
Cross‑Platform API Module
from airtest.core.api import * # import all methodsDevice Initialization
Two approaches are available:
1) Initialize by device UUID
init_device(platform='Android', uuid='', cap_method=JAVACAP for Android)Example:
init_device(platform='Android', uuid='127.0.0.1:62001', cap_method='JAVACAP')2) Initialize by device URL
android:/// # connect to current Android device
android://adbhost:adbport/serial?cap_method=javacap&touch_method=adb
windows:/// # connect to local desktop program
ios:/// # connect to iOS deviceExample:
connect_device('android://127.0.0.1:5037/127.0.0.1:62001?cap_method=javacap&touch_method=adb')Device Management Functions
device() # get current active device
set_current(0) # switch device by index
set_current('127.0.0.1:62001')# switch device by serial
auto_setup(basedir=None, devices=None, logdir=None, project_root=None, compress=0)Shell and App Control
shell('ls /data/local/tmp') # execute remote shell command
start_app('package.name') # launch app
stop_app('package.name') # stop app
clear_app('package.name') # clear app data
install(filepath='path/to/apk')
uninstall('package.name')Screen Operations
snapshot(filename='1.jpg', msg='hello', quality=3) # take screenshot
wake('device') # wake and unlock
home('device') # go to home screen
click(v=Template(x, y), times=2) # single click
double_click() # double click
swipe(v1, v2) # swipe from v1 to v2
swipe(v1, vector=(x, y)) # swipe by vector
pinch(in_or_out='in', center=None, percent=0.5) # pinch/zoom
keyevent(keyname='Home') # press hardware key
text('some text') # input textTiming and Waiting
sleep(2) # pause for 2 seconds
wait(v, timeout, interval, intervalfunc) # wait for template matchVerification Functions
exists(v) # check if target exists, returns coordinates or False
find_all(v) # find all occurrences, returns list of coordinates
assert_exists(v, msg) # assert target exists
assert_not_exists(v, msg) # assert target does not exist
assert_equal(v1, v2, msg) # assert equality
assert_not_equal(v1, v2, msg)# assert inequalityImages
Conclusion
The article covers the essential Airtest operations that enable developers to automate mobile applications efficiently; the next article will demonstrate practical usage.
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.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
