III. Multi-Device Collaboration in AirtestIDE¶
1.Connecting Multiple Devices in the Device Screen Window of AirtestIDE¶
IDE boasts the ability to connect multiple mobile devices. Take Android devices as an example. First, we need to ensure that the phone is properly connected to the computer. Then, in the IDE mobile device connection panel, click on the Refresh ADB button to find out the devices connected to the computer.
Click on the connect button of any device. After it is connected, two buttons will appear in the upper-right corner of the device screen. The button on the left is for switching to another device or back to the device connection panel.
Click on the Connection Panel button to return to the device connection panel and continue connecting another device.
Once both devices are successfully connected to the IDE, we can use the button with which we just switched the connection panel to freely switch between different devices.
After writing the script in AirtestIDE, click on the Run Script button and IDE will automatically add multiple --device parameters to the command line, informing the script of all connected devices without any additional operations required.
2.Switching Between Multiple Device with set_current¶
After connecting multiple phones, we can see all the currently connected devices in the global variable G.DEVICE_LIST in Airtest.
print(G.DEVICE_LIST)
# The current device list includes [dev1, dev2].
You can also use the set_current
API to switch between multiple devices, in two different ways:
# The first way: Pass in numbers 0, 1, 2, etc. to switch the current operating phone to the 1st, 2nd, 3rd, etc. phone connected to Airtest.
set_current(0)
set_current(1)
set_current(2)
# The second way: Switch the current operating device to phones with serial numbers serialno1 and serialno2.
set_current("serialno1")
set_current("serialno2")
3.Connecting Multiple Devices via Command Line¶
IDE support switching the current connected phone via the set_current API, allowing users to call multiple phones in a single script and write complex scripts involving multi-device interaction.
When running the script on the command line, add --device Android:/// in the command line, for example:
airtest run untitled.air --device Android:///serialno1 --device Android:///serialno2 --device Android:///serialno1
4.Connecting Multiple Devices via Script¶
Apart from directly connecting multiple devices through IDE, you can also use multiple connect_device statements in the script, passing in the device strings separately.
from airtest.core.api import *
# Connect to the first mobile phone
dev1 = connect_device("Android://127.0.0.1:5037/serialno1")
# Connect the second mobile phone
dev2 = connect_device("Android://127.0.0.1:5037/serialno2")
Breaking down the device string, 127.0.0.1 represents the local machine IP, 5037 is the default adb port, and serialno is the serial number of the Android phone.
5.Multi-Machine Collaboration in IDE with Adding WeChat Friends as an Example¶
Once we figure out how to connect multiple devices and switch to the current operating phone, we can easily write a script for adding friends on WeChat with each other on different phones. The general idea of the script goes like: ① Connect two Android devices ② Switch the current operating phone to device A ③ Send a friend request on device A ④ Switch the current operating phone to device B ⑤ Accept the friend request on device B ⑥ Two phones successfully added each other as friends on WeChat.
# -*- encoding=utf8 -*-
__author__ = "xiaoming"
from airtest.core.api import *
auto_setup(__file__)
# Connect to the first mobile phone (device A)
dev1 = connect_device("Android://127.0.0.1:5037/SJE5T17B170xxxxx")
# Connect to the second mobile phone (Device B)
dev2 = connect_device("Android://127.0.0.1:5037/2b3ab9axxxx")
# Switch the current operating phone to device A
set_current("SJE5T17B170xxxxx")
# Send a friend request on device A
pass
# Switch current operating phone to device B
set_current("2b3ab9axxxx")
# Accept the friend request on device B
pass
The script execution results can be viewed in the video below.