Automated Testing on Android Phones-part 1
By reading this tutorial, you will know the following content:
- How to specify phone in script code when running script
- How to fill in the contents of
--device Android: ///
- How to easily call ADB instructions or Android-specific interfaces in scripts
Connect Android phones¶
Connect Android phone in AirtestIDE¶
When using AirtestIDE for automated testing of Android applications, the first step is to connect an Android device.
Please refer to the instructions in our Device Connection Document for operation. After installing the driver, turning on the
Developer Options and
Allow USB debugging options in the phone, you can connect the phone with a USB cable and try to connect in AirtestIDE.
If you encounter problems, please consult the document Android Connection FAQ and follow the self-checking steps provided to troubleshoot the problem. Some brands of mobile phones have exclusive options that need to be turned on separately (such as Xiaomi, Vivo / Oppo). Please also check the precautions of the corresponding brand to avoid problems.
Support for Devices¶
At present, we support almost the majority of Android phones and Android emulators on the market (see here for tutorials on emulator connection). At the same time, we may also support a small number of special Android-based hardware devices (because of the wide variety of such devices, we cannot test and support them one by one. if you have compatibility requirements, please contact the development team).
After successfully connecting to the phone, we can operate the phone screen in AirtestIDE. Just like the picture below, we can start writing scripts with this phone.
Special option settings for device connection¶
There are a few non-mobile Android devices (such as smart TVs, smart rearview mirrors, etc.). You cannot directly connect by clicking the
connect button, but you can try to connect by checking the option in the
connect drop-down menu .
For example, our default connection method does not support the simulator, so when connecting to the simulator, you need to check the
Use Javacap option (some brand simulators also need to check
Use ADB orientation). The three options of the
connect drop-down menu are three alternatives for taking screenshots, rotating and clicking. When the default scheme does not take effect, using these 3 alternatives may support the device.
Note : Some brand phones do not support a certain feature. This may be just because the option is not turned on. For example, Xiaomi mobile phones must be enabled with
Allow simulated clicks to be able to click the phone in AirtestIDE by default (faster and better). If it is a normal mobile device, please try to consult the documentation to troubleshoot the problem first. The efficiency of these alternatives is lower than the default. Only some special Android devices need to use the alternative.
How to specify the phone when running the script¶
After connecting the phone in AirtestIDE and writing the script, we click the Run Script button, and then the program will use the phone currently connected in AirtestIDE by default, like this:
"D:\AirtestIDE\AirtestIDE" runner "untitled.air" --device Android://127.0.0.1:5037/F8UDU16409004135 --log "D:\log"
--device Android: //127.0.0.1: 5037 / F8UDU16409004135 is the current local mobile phone. It tells Airtest that the mobile phone name of our device is
F8UDU16409004135, so that it understands that we need to use this mobile phone.
In the command line, if we do not fill in the specific mobile device number, we can use something like
--device Android:/// to indicate run on an Android device currently connected .In this way we don't care what its device number is.
If you do not fill in
--device at the command line, no device will be connected to run the code by default. And you will get an error when running code that requires a device to run (For example , the
touch statement must be connected to the device to run)
If you want to connect devices through code in the script, you can use the connect_device interface or pass the
devices parameter in the
Please refer to the related content in How to use Airtest in Python script.
How to write device connection strings¶
--device parameter used in the command line just passed a device string. Taking an Android device as an example, the complete definition of the string is as follows:
Among them, adbhost is the IP of the host where the adb server is located. The default is 127.0.0.1. The adb port is 5037 by default, and serialno is the serial number of the android phone.
Here are some common filling examples for your reference:
# If nothing is filled in, the first phone in the current connection will be selected by default Android:/// # Connect a mobile phone with the device number 79d03fa connected to the default port of the machine Android://127.0.0.1:5037/79d03fa # Use the local adb to connect a remote device that has been connected by adb. Note that 10.254.60.1:5555 is actually serialno Android://127.0.0.1:5037/10.254.60.1:5555 # When connection parameters are used by special devices such as simulators: # Use javacap mode is checked when connecting to the simulator Android://127.0.0.1:5037/127.0.0.1:7555?cap_method=JAVACAP # Check all the options before connecting the device. Multiple parameter strings must be concatenated with && Android://127.0.0.1:5037/79d03fa?cap_method=JAVACAP&&ori_method=ADBORI&&touch_method=ADBTOUCH
More notes, examples of other platforms, how the
& symbol should be used in the command line, etc. can be found in the document about device string.
Airtest supports multiple phones connected to a script, but it should be noted that this script cannot be run automatically on multiple phones. Instead, you can use these phones in a script to achieve some effects similar to multi-machine collaboration (such as letting two phones log in to the same APP and "add friends" to each other).
Suppose we are using AirtestIDE to write scripts and have connected to multiple phones at the same time. When running the script, AirtestIDE will automatically add multiple
--device parameters to the command line to tell the script all the currently connected phones, no additional operations required.
Or you can directly use multiple
connect_device statements in the script and pass in the phone connection string information:
from airtest.core.api import connect_device dev1 = connect_device("Android://127.0.0.1:5037/serialno1") # Connect to your first phone dev2 = connect_device("Android://127.0.0.1:5037/serialno2") # the second phone
After connecting multiple phones, we can see all devices currently connected in Airtest global variable
G.DEVICE_LIST. We can also use the
set_current interface to switch between multiple devices.
print(G.DEVICE_LIST) # The device list is [dev1, dev2] # Pass in the number 0 to switch the currently operating phone to the first set_current(0) # Switch the currently operating phone to a phone with serial number serialno2 set_current("serialno2") # Use the device () interface to get the Android object in the current connection current_dev = device()
Exclusive functions and interfaces of the Android platform¶
In the Airtest Introduction-Platform-Related Interfaces section, we mentioned that the platforms supported by each interface may be different, and Android supports the most comprehensive and abundant interface.
Basically, the interfaces in
airtest.core.api can be used directly on the Android platform, for example (click here to jump to document address):
# Clean up application data clear_app("org.cocos2d.blackjack") # Launch an application start_app("org.cocos2d.blackjack") # Incoming a key response keyevent("BACK")
Interfaces for Android devices¶
In addition to the cross-platform interfaces provided in
airtest.core.api, Android device objects have many built-in interfaces to call. We can look up the methods owned by the Android device object in the airtest.core.android.android module document, and then call it like this:
dev = device() # Get the Android object of the current device print(dev.get_display_info()) # View the display information of the current device print(dev.list_app()) # Print out a list of currently installed apps
Call ADB instruction¶
In Android device test scripts, sometimes we need to enter some
ADB instructions. If you want to call the
ADB instruction in a normal Python script, you may need to use modules such as
subprocess to start the process separately and run the command line.
But in the Airtest script, calling the ADB instruction is very simple:
# Execute command adb shell ls on current device print(shell("ls")) # Execute adb instruction on specific device dev = connect_device("Android:///device1") dev.shell("ls") # Switch to a device and execute the adb command set_current(0) shell("ls")
Convenient Features of Mobile Assistant¶
When using AirtestIDE to connect to an Android phone, we also provide a simple mobile assistant function. The usage method is here.
After connecting to an Android phone, click the tool icon in the upper right corner of the device window, and in the expansion drop-down menu, select
Show assistant dialog to open the Android phone assistant.
In Android Phone Assistant, we provide the following functions:
- Mobile application installation / uninstallation / list view
- Common shortcuts: open web address, enter text, switch input methods, adjust volume, etc.
- Mobile Shell Debug Window
I hope our Android phone assistant plays a good supporting role when everyone uses Android phones. If there are bugs or feature suggestions, please feedback to Github.