III. Airtest Script for Device Connection¶
1.Preface¶
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://127.0.0.1:5037/SJE5T17B17","Android://127.0.0.1:5037/SJE5T17B18"])
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://127.0.0.1:5037/SJE5T17B17")
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://127.0.0.1:5037/serialno1")
# Connect to the second mobile phone
dev2 = connect_device("Android://127.0.0.1:5037/serialno2")
# Switch to the phone that is currently operated and whose serial number is serialno1
set_current("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:
init_device(platform="Android",uuid="SJE5T17B17")
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:///127.0.0.1:5037/79d03fa 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
Android:///
# Connect to the mobile device which is connected to the default port of the local machine and whose device string is 79d03fa
Android://127.0.0.1:5037/79d03fa
# Connect to a remote device via ADB that has been connected to this local machine via ADB before. Keep in mind that 10.254.60.1:5555 represents the serialno.
Android://127.0.0.1:5037/10.254.60.1:5555
# Connect to a Nox Player emulator (with 127.0.0.1:62001 being its port number)
Android://127.0.0.1:5037/127.0.0.1:62001?cap_method=JAVACAP
2) Example of connecting an Android device¶
# Use auto_setup
auto_setup(__file__, devices=["Android://127.0.0.1:5037/SJE5T17B17"])
# Use connect_device
connect_device("Android:///SJE5T17B17?cap_method=javacap&touch_method=adb")
# 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
iOS:///http://127.0.0.1:8100
# 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
auto_setup(__file__,devices=["iOS:///http://127.0.0.1:8100"])
# Use connect_device
connect_device("iOS:///http://127.0.0.1:8100")
# Use init_device
init_device(platform="IOS",uuid="http://127.0.0.1:8100")
5.Connecting a Windows Window/Desktop¶
1) URI strings for Windows window/desktop¶
# Connect to a Windows window with a handle of 123456
Windows:///123456
# Connect to a Windows window with a name that matches a specific regular expression
Windows:///?title_re=Unity.*
# Connect to a Windows desktop without specifying any windows
Windows:///
2) Example of connecting to a Windows window/desktop¶
# Use auto_setup
auto_setup(__file__,devices=["Windows:///"])
# Use connect_device
connect_device("Windows:///?title_re=Unity.*")
# Use init_device
init_device(platform="Windows",uuid="123456")
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.