Skip to content

III. Airtest Script for Device Connection


In the Introduction to Airtest, we have mentioned that Airtest supports connecting to devices such as Android, iOS, and Windows. In this section, we will explain in detail how to use Airtest scripts to connect to the devices.

2.Introduction to APIs for Device Connection

Airtest provides various APIs for connecting to devices.

1) auto_setup

The first is the familiar initialization API auto_setup which can automatically configure the runtime environment. If no device is connected during the initialization, it will attempt to connect to an Android device by default. Of course, it also supports passing in a device connection string "list" to connect to one or more devices.

auto_setup(__file__, devices=["Android://","Android://"])


2) connect_device

By passing the URI string of the device in the connect_device API, you can connect one device.

dev = connect_device("Android://")

If you want to connect multiple devices, you can write multiple connect_device scripts and use set_current to switch to the currently used device.

# Connect to the first mobile phone

dev1 = connect_device("Android://")

# Connect to the second mobile phone

dev2 = connect_device("Android://")

# Switch to the phone that is currently operated and whose serial number is serialno1



3) init_device

With the init_device API, you only need to pass in the device platform and device uuid. For parameter details, refer to the following picture:



3.Connecting Android Devices

After introducing the various APIs provided by Airtest for connecting devices, let's now dive into how to write URI strings for different devices. (For example, Android:/// mentioned above is a URI string.)

1) URI strings of Android devices

# If no UR string is filled in, the first phone in the connection list will be selected by default


# Connect to the mobile device which is connected to the default port of the local machine and whose device string is 79d03fa


# Connect to a remote device via ADB that has been connected to this local machine via ADB before. Keep in mind that represents the serialno.


# Connect to a Nox Player emulator (with being its port number)


2) Example of connecting an Android device

# Use auto_setup

auto_setup(__file__, devices=["Android://"])

# Use connect_device


# Use init_device

init_device(platform="Android",uuid="SJE5T17B17", cap_method="JAVACAP")

4.Connecting iOS Devices

1) URI string of iOS devices

# Connect to a real iOS device deployed on the local machine

# When using tidevice to connect an iOS device, DeviceIdentifier can be found in the startup information http+usbmux://DeviceIdentifier

2) Example of connecting an iOS device

#Use auto_serup

# Use connect_device

# Use init_device

5.Connecting a Windows Window/Desktop

1) URI strings for Windows window/desktop

# Connect to a Windows window with a handle of 123456

# Connect to a Windows window with a name that matches a specific regular expression

# Connect to a Windows desktop without specifying any windows

2) Example of connecting to a Windows window/desktop

# Use auto_setup

# Use connect_device

# Use init_device

6.Notes for Writing Scripts to Connect Devices

1) When to write a script to connect the device?

When running the .air script in AirtestIDE, the IDE will automatically retrieve the device currently connected to the device window to execute the script. However, for all other situations, you need to write a script to connect the device:

  • When running .py scripts in AirtestIDE (You may use the initialization API auto_setup to connect devices.), you need to write a script to connect the device.
  • When running .air and .py scripts outside of AirtestIDE, you need to connect the device to be tested in the script.
  • When running the script from the command line with the "airtest run" command, but without passing the --device parameter, you need to write a script for device connection.

2) Where to place the script for connecting the device?

It is recommended to place the script for connecting devices inside the auto_setup initialization API. If other statements are used to connect the script, try to place them after the initialization API. If the device connection statement is placed after the operation script, it may result in an error such as "'NoneType' object has no attribute 'snapshot'" which means that the device is not connected before operation.