This article from the official public project number: AirtestProject the original address: airtest.doc.io.netease.com/tutorial/4_… Copyright notice: permission to reprint, but reprint must retain the original link

preface

By reading this tutorial, you will know the following:

  • How do I specify the phone in the script code, while the script is running
  • How to fill in--device Android:///The content of the
  • How can I easily invoke ADB directives or Android-specific interfaces in scripts

Connecting to An Android Phone

Connect to an Android phone in the AirtestIDE

To automate testing an Android app using the AirtestIDE, the first step is to connect to an Android device.

After installing the driver, opening the developer option in your phone, and allowing USB debugging, connect your phone with a USB cable and try to connect it in the AirtestIDE.

If you encounter problems, please refer to the Android Connection FAQ document and follow the self-check steps provided in it to troubleshoot problems. Some brands of mobile phones have exclusive options that need to be opened separately (such as Xiaomi, Vivo/Oppo), please refer to the corresponding brand precautions to avoid problems.

Device support status

At present, we support almost the vast majority of Android phones and Android emulators on the market (please refer to the simulator connection tutorial here), and may also support a small number of special hardware devices based on Android system (due to the variety of such devices, we cannot test and support one by one, if you have compatible requirements, please contact the development team).

After successfully connecting to your phone, you can tap into the AirtestIDE to view your phone’s video, like the one below, before you can start writing a script.

Special option Settings for device connections

On a few non-mobile Android devices (smart TVS, smart mirrors, etc.), you can’t connect directly by clicking the Connect button, but you can try to connect by checking the options in the Connect drop-down menu.

For example, our default connection method does not support emulators, so we need to check the Use Javacap option (some branded emulators also check Use ADB orientation) before connecting to the emulator. The three options in the CONNECT drop-down menu are alternatives to screenshots, rotations, and clicks, which may support the device if the default option doesn’t work.

Note: some phones don’t support a specific feature, just because the option is not enabled. For example, a Xiaomi phone must be enabled to simulate clicking in order to tap your phone in the AirtestIDE using the default mode (faster and better). If it is a normal mobile device, please consult the documentation to troubleshoot the problem first. Each of these alternatives is less efficient than the default, and only some special Android devices need to use the alternative.

How do I specify the phone when running the script

When you connect your phone to the AirtestIDE and write a script, when you click the “run the script” button, you will use the phone you have connected to in the AirtestIDE command line by default, like this:

"D:\AirtestIDE\AirtestIDE" runner "untitled.air"- device Android: / / 127.0.0.1:5037 / F8UDU16409004135 - log"D:\log"
Copy the code

Among them – device Android: / / 127.0.0.1:5037 / F8UDU16409004135 is currently using the local mobile phone, it will be our the device number for F8UDU16409004135 phone name tell Airtest, let it understand that we need to use the phone.

We can also use — Device Android:/// to run on the currently connected Android device, regardless of the device number, without specifying the device number.

If you leave –device completely blank on the command line, the code will run without any device by default, and an error will be reported when you run code that requires a device to run (for example, a touch statement must be connected to a device to run).

If you want to use code to connect devices in your scripts, you can use the CONNECt_device interface or pass a device parameter to the auto_setup interface. For details, see Airtest.

How to write a device connection string

The –device argument used on the command line is passed as a device string. In the case of an Android device, the string is fully defined as follows:

Android://<adbhost[localhost]>:<adbport[5037]>/<serialno>
Copy the code

Where, adbhost is the IP address of the host where adb Server resides. The default is 127.0.0.1, adb port is 5037, and serialno is the serial number of the Android phone.

Here are some common examples for your reference:

# Select the first phone in the current connection by default
Android:///
Connect to a phone with device number 79D03FA connected to the default port of the deviceAndroid: / / 127.0.0.1:5037/79 d03fa10.254.60.1:5555 is serialnoAndroid: / / 127.0.0.1:5037/10.254.60.1:5555Simulator and other special devices, using connection parameters:
The Use Javacap mode is checkedAndroid: / / 127.0.0.1:5037/127.0.0.1:7555? cap_method=JAVACAP# all options are checked for the device to be connected later, using && to concatenate multiple parameter stringsAndroid: / / 127.0.0.1:5037/79 d03fa? cap_method=JAVACAP&&ori_method=ADBORI&&touch_method=ADBTOUCHCopy the code

More notes, other platform examples, what to do with ampersand on the command line, and more can be found in documentation about device strings.

Machine more collaboration

Airtest supports multiple phones in a script, but it does not automatically make the script run across multiple phones. Instead, the script can be used to achieve something similar to multi-phone collaboration (for example, two phones can log into the same APP and “friend” each other).

If you are using the AirtestIDE to write a script and have connected to multiple mobile phones at the same time, when you run the script, the AirtestIDE will automatically add multiple –device parameters to the command line to tell the script all the phones you are currently connected to.

Alternatively, you can directly use multiple connect_device statements in the script, passing in the phone connection string information:

from airtest.core.api import connect_device
dev1 = connect_device("Android: / / 127.0.0.1:5037 / serialno1")  # Connect your first phone
dev2 = connect_device("Android: / / 127.0.0.1:5037 / serialno2")  # Second phone
Copy the code

After connecting multiple phones, we can see all currently connected devices in Airtest’s global variable G.device_list, and can switch between devices using the set_current interface.

print(G.DEVICE_LIST)  [dev1, dev2]

Pass in the number 0 to switch the current operating phone to the first phone
set_current(0)

Switch the current mobile phone to the serial number serialno2
set_current("serialno2")

Get the device Android object in the current connection using the device() interface
current_dev = device()
Copy the code

Android platform features and interfaces

In Airtest’s introduction to Scripting – Platform-specific interfaces, we mentioned that each interface may support different platforms, and Android supports the most comprehensive and rich interfaces.

The airtest.core. API interface (document address) can be used directly on the Android platform, for example:

Clear application data
clear_app("org.cocos2d.blackjack")
# Start an app
start_app("org.cocos2d.blackjack")
Pass in a key response
keyevent("BACK")
Copy the code

Android Device Interface

In addition to the cross-platform interfaces provided in airtest.core. API, Android device objects have a number of built-in interfaces that can be called, We can crash at airtest. Core. Android. Android module in this document refer to the android device object has a method, and then call something like this:

dev = device()  Get the Android object for the current device
print(dev.get_display_info())  # View the display information of the current device
print(dev.list_app())  Print a list of currently installed apps
Copy the code

ADB directive call

In test scripts for Android devices, we sometimes need to enter ADB directives. If we want to call ADB directives in ordinary Python scripts, we may need to use a module such as subprocess to start a separate process and run the command line to do so.

But in Airtest scripts, calling ADB directives is a very simple matter:

Execute adb shell ls on the current device
print(shell("ls"))

Execute ADB instructions for specific devices
dev = connect_device("Android:///device1")
dev.shell("ls")

Switch to a device and execute adb commands
set_current(0)
shell("ls")
Copy the code

Mobile assistant convenient function

Using the AirtestIDE to connect to an Android phone, we’ve also provided a simple phone assistant feature, which you can use here.

After connecting the Android phone, click the tool icon in the upper right corner of the device window and select Show Assistant Dialog from the drop-down menu to open the Android Phone Assistant.

In Android Phone Assistant, we offer the following features:

  • Install/uninstall/list view of mobile apps
  • Common quick operations: open the website, input text, switch input method, volume adjustment, etc
  • Mobile Shell debugging window

We hope that our Android assistant will play a good auxiliary role when you use Android phones. If you have any bugs or suggestions, please feedback to Github.

For more information about the tutorials and the project, please visit our official account AirtestProject to check out the previous tutorials: