At the end of the article, we left a tail: How to start Airtest’s wireless mode and control your phone without using a USB cable?
This article will talk about specific practices. There are two ways to do this: the first is to control the phone in Airtest’s IDE. The second is to control the remote phone in Python code.
Start Enables the ADB port on the phone
Either way, to control the phone remotely, you first need to turn on the ADB port on the phone. To do this, you must first connect your phone to your computer with a USB cable.
Execute command in terminal:
adb tcpip 48887
Copy the code
The port number 48887 can be set to another port but cannot conflict with an existing port.
Once the command is executed, you can unplug the USB cable. The next step is to remotely control the phone.
Remotely control a mobile phone in the AirtestIDE
Open Airtest and click the remote Connection highlighted in the red box below:
In the input box that pops up, type:
Adb Connect Mobile IP: portCopy the code
The IP address of the phone can be found in the wireless router as well as in the system Settings of the phone. The port is the port set by the previous command.
Such as:
The adb connect 192.168.0.102:48887Copy the code
As shown in the figure below.
Click Connect and the phone will appear in the list of devices above. Double-click on it to connect wirelessly to the phone and see its screen.
Control the phone in Python
First of all, there is a problem with the official documentation of Airtest. If you write code following the documentation, you will fail.
The official document, in airtest. Readthedocs. IO/zh_CN/lates… Here’s an example of how to connect to a remote Android phone:
Local device# connect a remote device using custom params
connect_device("android://adbhost:adbport/1234566? cap_method=javacap&touch_method=adb")
Copy the code
This code looks like you might write Python code as:
from airtest.core.api import *
device_1 = connect_device('android: / / 192.168.0.100:48887 / mobile phone number list? cap_method=javacap&touch_method=adb')
Copy the code
If you write like this, you can’t connect to your phone.
The correct code is:
from airtest.core.api import *
device_1 = connect_device('android: / / / 192.168.0.100:48887? cap_method=javacap&touch_method=adb')
Copy the code
The only way to control the phone is the way I wrote it here. As shown in the GIF below.
The complete code involved in the GIF is:
from airtest.core.api import *
import time
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
device_1 = connect_device('android: / / / 192.168.0.100:48887? cap_method=javacap&touch_method=adb')
poco = AndroidUiautomationPoco(device_1, use_airtest_input=True, screenshot_each_action=False)
poco(text="Zhihu").click()
poco(name="com.zhihu.android:id/input").click()
poco(name="com.zhihu.android:id/input").set_text('Ancient Sword of Legend iii')
time.sleep(2)
poco(name='com.zhihu.android:id/magi_title', textMatches='*$').click()
poco.swipe([0.5.0.8], [0.5.0.2])
Copy the code