V. ADB Operations Encapsulated by Airtest¶
1.Preface¶
ADB, short for Android Debug Bridge, is a command line window used to interact with emulators or real devices through a computer.
For Android developers and testers, it is an indispensable powerful tool. So today, we will walk you through some commonly used ADB operations and some ADB APIs encapsulated by Airtest.
2.Some Scenarios for Using ADB Commands¶
As a tester, you are probably familiar with using ADB. For example, before conducting testing on a real device or emulator, it is common to use the command adb devices
to check if the device under test has established a connection with the computer.
>>> adb devices
List of devices attached
PFT4PBLF75GQHYBM device
emulator-5554 device
Another example, when using Airtest for automated testing, we often encounter errors related to ADB version conflicts:
raise AdbError(stdout, stderr)
airtest.core.error.AdbError: stdout[] stderr[adb server version (36) doesn't match this client (40); killing...
could not read ok from ADB Server
To solve the version conflict, we can use adb version
command to check the current ADB version and replace all ADBs on the computer with the same version.
>>> adb version
Android Debug Bridge version 1.0.40
Version 4986621
Installed as C:\Users\xiaojuan\adb.exe
3.Common ADB Operations and Examples¶
Of course, in addition to the previously mentioned adb devices command for querying connected devices/emulators and the adb version command for checking the ADB version, ADB can do much more.
1) App management¶
① View application list
# View all applications on the device
adb shell pm list packages
# View third-party applications on the device
adb shell pm list packages -3
# View system applications on the device
adb shell pm list packages -s
② Install APK
adb install "D:/demo/tutorial-blackjack-release-signed.apk"
③ Uninstall applications
adb uninstall com.netease.cloudmusic
④ View application details
adb shell dumpsys package com.netease.cloudmusic
2) File management¶
① Copy files from the device to the computer
# adb pull <file path on device> [directory on computer]
adb pull /sdcard/sr.mp4 ~/tmp/
② Copy files from computer to device
# adb push <file path on computer> <directory on device>
adb push ~/sr.mp4 /sdcard/
3) Simulation of keyevent¶
① Simulate the Power key: adb shell input keyevent 26
② Simulate the HOME key: adb shell input keyevent 3
③
Simulate the Back key: adb shell input keyevent 4
④ Turn on/off the screen:
# Turn on the screen
adb shell input keyevent 224
# Turn off the screen
adb shell input keyevent 223
⑤ Simulate to swipe to unlock
# 300, 1000, 300, and 500 respectively represent the starting point's x-coordinate, y-coordinate, ending point's x-coordinate, and y-coordinate.
adb shell input swipe 300 1000 300 500
⑥ Input text
adb shell input text airtest
4) Viewing device information¶
① View device model
>>> adb -s PFT4PBLF75GQHYBM shell getprop ro.product.model
OPPO A83
② Check screen resolution
>>> adb -s PFT4PBLF75GQHYBM shell wm size
Physical size: 720x1440
③ Check Android system version
>>> adb -s PFT4PBLF75GQHYBM shell getprop ro.build.version.release
7.1.1
5) More on ADB commands¶
For more details on ADB operations, we recommend reading this GitHub document: https://github.com/mzlogin/awesome-adb.
4.ADB APIs Encapsulated by Airtest and Examples¶
In the Airtest library, most of the ADB operations have already been encapsulated, so you don't need to execute ADB commands separately.
Here is an example of using the ADB APIs encapsulated by Airtest to print the device serial number and all third-party apps on the device:
It can be seen that by calling some encapsulated Airtest APIs, we can replace the full ADB commands. In addition, in the log viewer, we can see the actual ADB commands executed by this API:
# The ADB command corresponding to android.get_default_device()
adb.exe devices
# The ADB command corresponding to android.list_app(third_only=True)
adb.exe -s PFT4PBLF75GQHYBM shell pm list packages -3
In addition, Airtest also has many commonly used ADB APIs encapsulated.
1) Return the full path of the application: path_app()¶
android = Android()
android.path_app("com.netease.cloudmusic")
2) Check if the application exists on the current device: check_app()¶
android = Android()
android.check_app("com.netease.cloudmusic)
3) Stop the application from running: stop_app()¶
stop_app("com.netease.cloudmusic")
# Strat the application: start_app()
start_app("com.netease.cloudmusic")
# Clear application data: clear_app()
clear_app("com.netease.cloudmusic")
4) Install the application: install_app()¶
install(r"D:\demo\tutorial-blackjack-release-signed.apk")
# Uninstall the application: uninstall_app()
uninstall("org.cocos2dx.javascript")
5) Keyevent operation: keyevent()¶
keyevent("HOME")
keyevent("POWER")
6) Wake the device: wake()¶
wake()
7) Return to HOME: home()¶
home()
8) Input text: text()¶
text("123")
9) Check if the screen is on: is_screenon()¶
android = Android()
android.is_screenon()
10) Check if the device is locked: is_locked()¶
android = Android()
android.is_locked()
11) Get the current resolution of the device: get_current_resolution()¶
android = Android()
android.get_current_resolution()
12) Other adb shell commands: shell()¶
shell("ls")
shell("pm list packages -3")
In fact, the shell() command of Airtest helps to supplement the adb -s serialno shell part, and the following content can be passed using normal shell commands.
13) Introduction to more ADB APIs encapsulated by Airtest¶
If you want to learn more about the encapsulated ADB APIs in Airtest, you can refer to this file in your local Airtest library: airtest/core/android/android.py.