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.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Airtest: Essential APIs for Mobile Automation Testing

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 methods

Device 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 device

Example:

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 text

Timing and Waiting

sleep(2)                         # pause for 2 seconds
wait(v, timeout, interval, intervalfunc)  # wait for template match

Verification 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 inequality

Images

Conclusion

The article covers the essential Airtest operations that enable developers to automate mobile applications efficiently; the next article will demonstrate practical usage.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythontestingAirtestAPIMobile Automationdevice control
Python Crawling & Data Mining
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.