2.3 Android Emulator Connection¶
If you don't have a real Android device at hand but want to perform automated testing for Android applications, you can use the Android emulator automation testing function in AirtestIDE.
AirtestIDE supports emulators including Android Virtual Device (AVD) and other commonly used emulators in the market (such as Mumu Player).
The emulator can be connected to AirtestIDE just like a real device. If a black screen appears, please select the Use javacap option in the dropdown menu under the connect button. Then click on the connect button to connect the emulator. If the connection still fails, try selecting the use adb orientation option before the connection.
1. Connecting to an Emulator¶
Here is an example of the connection process for an emulator.
- First, similar to connecting a mobile phone, you need to activate the Developer Options and enable USB debugging in the emulator. Some emulators may require
- clicking on Settings > About Phone to access the Developer Options.
Select the "remote device connection" in the AirtestIDE device window, enter adb
- connect 127.0.0.1:7555 (the port number is related to the emulator brand, see the list of emulator connection ports), and click on the connect button.
When the device with IP address 127.0.0.1:7555 appears in the device list, click open the dropdown menu under the Connect button, check the Javacap mode, and then click on the Connect button (if Javacap mode is not checked, you will see a black screen on the device screen window). If the connection fails, you can check the use adb orientation option and try connecting again.
- The connection is completed.
2. List of Emulator Connection Ports¶
The device connection code for mainstream emulators is as follows:
Emulators | ADB Connection Code |
---|---|
NetEase Mumu Plyer | adb connect 127.0.0.1:7555 |
NoxPlayer | adb connect 127.0.0.1:62001 |
Memu Play | adb connect 127.0.0.1:21503 |
iTools | adb connect 127.0.0.1:54001 |
TT Emulator | adb connect 127.0.0.1:6555 |
Droid4X | adb connect 127.0.0.1:26744 |
BlueStacks | adb connect 127.0.0.1:5555 |
These emulators can be connected by referring to the example above.
3.Android Virtual Device (AVD)¶
-
Set up Android environment: Set up Android Studio, then select Tools > Android > AVD Manager in it.
-
Create a virtual device:
-
Create your virtual device:
- Choose a device definition:
- Choose a system:
- Fill in the configuration page as follows:
-
The creation is completed. Now start the virtual device:
-
Connect the virtual device:
-
When the emulator-555x device appears in the AirtestIDE device window, click on the Connect button.
The connection is completed.
4.Some Emulator-Related Issues¶
-
To avoid a black screen, you need to select the use javacap option before connecting to the emulator.
-
For some versions of emulator, even if the use javacap option is selected, the connection may still fail. In this case, you can try selecting the use adb orientation option before reconnecting.
-
Since the above options need to be checked when connecting to the emulator, if you are running the script on an emulator, you also need to pass these parameters into the script. Please refer to the following content for details.
-
Some emulator brands may have issues with click functionality. In this case, you can try checking the use ADBtouch option before connecting again.
-
Some emulator brands (such as NoxPlayer) may not be able to call the Yosemite input method because they default to using keyboard input, causing the text() API to fail. In this case, check if the Hardware > Physical keyboard option in the emulator settings is turned on. Turn it off and then try again.
If the issue persists (the physical keyboard option keeps being enabled automatically), you can try opening the emulator settings in the upper right corner and selecting the soft keyboard option.
If you still cannot input, you can consider using Poco to input or directly use ADB command to input English content. Please refer to this document for details.
-
Failure to install yosemite.apk can also lead to failure to connect an emulator. Typically, the following error messages will appear. Please check if yosemite.apk has been installed automatically on the emulator. If not, check whether the "installation of applications via USB" in Developer Options is turned off by default. Turn it on and the IDE will automatically install the apk on the emulator during the connection process. If the installation still fails, you can search for this apk file in the IDE directory and manually install it.
5.Passing Emulator Device Parameters¶
Passing emulator device parameters in the command line¶
When running a script via the command line with an emulator device connected, the Use javacap and Use ADB orientation options may be selected during the connection process. Therefore, when connecting the device via command line, these two parameters also need to be appended to the device string:
airtest run D:/test/moniqi_test.air --device Android://127.0.0.1:5037/127.0.0.1:62001?cap_method=JAVACAP^&^&ori_method=ADBORI --log E:/log_test
1.The definition of the emulator device string is:
Android://<adbhost[localhost]>:<adbport[5037]>/emulator port number
Among them, adbhost is the IP address of the host where the adb server is located, with the local machine default to 127.0.0.1 and the adb port to 5037.
- The device string is followed by multiple parameters that need to be connected with &&, which needs to be escaped to take effect. On Windows, it should be rewritten as ^&^&, and on MAC, it should be rewritten as /&/&.
Writing the connect_device statement for emulator in the script¶
If you don't want to pass device parameters in the command line, you can write a connect_device statement in the script to connect to the emulator instead.
# -*- encoding=utf8 -*-
__author__ = "xiaoming"
from airtest.core.api import *
dev = connect_device("Android://127.0.0.1:5037/127.0.0.1:62001?cap_method=JAVACAP^&^&ori_method=ADBORI")
auto_setup(__file__)
This way, you don't need to add the parameters for connecting to the emulator when running the script via the command line.
Passing emulator device parameters into a pure .py script¶
If you are using a pure .py script, you also need to pass in the corresponding emulator device parameters during initialization.
# -*- encoding=utf8 -*-
__author__ = "xiaoming"
from airtest.core.api import *
from airtest.cli.parser import cli_setup
if not cli_setup():
auto_setup(__file__, logdir=True, devices=[
"Android://127.0.0.1:5037/127.0.0.1:62001?cap_method=JAVACAP^&^&ori_method=ADBORI",
])