Android Test common ADB command summary

Source: Internet
Author: User



For mobile Android testing, the ADB command is a very important point, you must memorize the usual ADB commands in the heart, will be a great convenience for Android testing, many of the commands will be used in automated test scripts.


Android Debug Bridge


ADB is actuallyAndroid Debug Bridge, the abbreviation of Android Debug Bridge, ADB is a c/S Architecture command-line tool, mainly consists of 3 parts:



Client running on PC: it can install, uninstall, and debug Android apps.


Tools such as ADT in Eclipse, DDMS in the SDK Tools directory, and Monitor are all equally used by ADB to interact with Android devices.

PC-side phone helper, such as 360 mobile phone helper, pea pod, app Bao, etc., in addition to the installation of third-party applications convenient, other functions, basically can be done through the ADB command, it is recommended to测试人员try not to install such mobile phone assistant on the computer, because its own ADB program may be with The ADB program under the Android SDK conflicts and the5037port is occupied, resulting in the inability to connect to the device when using the ADB command


Service running on the PC side: its management client connects to the ADB background process on the Android device


After the ADB service starts, Windows can find the Adb.exe process in Task Manager


The ADB background process running on Android devices


Executeadb shell ps | grep adbd, you can find the background process, Windows usefindstralternative grep



[xuxu:~]$ adb shell ps | grep adbd
root      23227 1     6672   832   ffffffff 00019bb4 S /sbin/adbd


Here is a place to note that the ADB uses the port number,5037it is necessary to remember

Next I'll divide the adb command into three parts,adb 命令adb shell 命令linux 命令


adb command





In the process of development or testing, we can manage multiple devices through the ADB in the general format of:



adb [-e |-D |-s < device serial number;] < sub-command >



With the environment variables in place, enter ADB help in the command window or enter ADB directly, and all the option descriptions and subcommands will be listed.



Here are some of the commands that are commonly used:



ADB devices, get device list and device status




[xuxu:~]$ adb devices
List of devices attached 
44c826a0    device  


ADB get-state, getting the status of the device




[xuxu:~]$ adb get-state  
device

The state of the device is 3 minutes, and,deviceofflineunknown

Device: normal connection of devices

Offline: Connection unexpected, device not responding

Unknown: No device connected


ADB kill-server, adb start-server, end adb service, start adb service, usually two command


Generally in connection with an exception, use ADB devices not properly list the device, device status is abnormal when using kill-server, and then run Start-server to restart the service


ADB logcat, print the Android system log, this can be taken out in terms of



ADB bugreport, prints the output of Dumpsys, Dumpstate, Logcat, and is also used for parsing errors


More output, recommended redirection to a file


adb bugreport > d:\bugreport.log


ADB install, installation application, overwrite installation is using the-r option


If you need to install the APK with Chinese name in Windows, you need to change the ADB, Baidu can find the ADB to make changes, support the Chinese command apk, please search by yourself


ADB Uninstall, uninstall the application, followed by the parameter is应用的包名, please distinguish fromapk 文件名


'-K ' means keep the data and the cache directories,-k option, saving the database and caching directory when uninstalling


ADB pull to copy files or folders on your Android device to a local

For example, copy the Pull.txt file under SDcard to the D drive:



adb pull sdcard/pull.txt d:\

If you need to rename to Rename.txt:


adb pull sdcard/pull.txt d:\rename.txt

Note the permissions, copy the system permissions in the directory of the file, requires root, and the general Android root can not use the command to copy, and need to use a similar to the RE-file browser on the phone, the system's file system is mounted to read and write, before the mobile system files on the phone copy , it is recommended to use the development version of  Mobile phone, Iuni is also good drop ~ ~


ADB push, push local files to Android device

For example, push the push.txt to SDcard under the D drive:



adb push d:\push.txt sdcard/

There is no less slash behind the SDcard, or the following error will occur:


[xuxu:~]$ adb push push.txt sdcardfailed to copy ‘push.txt‘ to ‘sdcard‘: Is a directory

Permissions issue with pull command


ADB root, adb remount, is only useful for phones similar to the  development version, you can directly have these two commands to get root permissions and mount the system file system as read-write status



ADB reboot, restart Android device


bootloader, restart device, enter FastBoot mode, with ADB reboot-bootloader command

Recovery, restart the device, enter the recovery mode, often brush the students more familiar with this mode


ADB forward, redirecting a port on a host to a port on the device



adb forward tcp:1314 tcp :8888


After executing this command, all messages sent to host port 1314 will be forwarded to the 8888 port on the Android device, so you can control the Android device remotely.



ADB connect remote Connect Android device


Mobile phone, PC is in the same network, phone root, install app Adbwireless, start the app and click the button in the middle of the interface:

Then runadb connect 192.168.1.102, you can connect the phone wirelessly, the disadvantage is that the speed is relatively slow




adb shell command




Someone asked me why I know so many orders, the answer is I love toss, here first to understand why I want to distinguish between the ADB command and the adb shell command.
To put it simply, the ADB command is a command that comes with the ADB program, while the ADB shell is a command from the Android system that is called, and these commands are placed in the System/bin directory of the Android device, for example, I then command line to knock such a a command:



[xuxu:~]$ adb shell hehe/system/bin/sh: hehe: not found


Obviously, this command does not exist in the bin directory.
I love tossing, want to see what commands, and do not want to find documents, so start the simulator, the entiresystem/bindirectory is copied out, and then one by one to try. Embarrassing ~

Opening these files will reveal that some of the commands are actually a shell script, such as opening the monkey file:




# Script to start "monkey" on the device, which has a very rudimentary
# shell.
#
base=/system
export CLASSPATH=$base/framework/monkey.jar
trap "" HUP
exec app_process $base/bin com.android.commands.monkey.Monkey $*


Another example of opening am:



#!/system/bin/sh## Script to start "am" on the device, which has a very rudimentary# shell.#base=/systemexport CLASSPATH=$base/framework/am.jarexec app_process $base/bin com.android.commands.am.Am "[email protected]"


There is also the SDK sources/android-20/com/android/commands directory:



[xuxu:...oid-20/com/android/commands]$ pwd
/Users/xuxu/utils/android/android-sdk-macosx/sources/android-20/com/android/commands
[xuxu:...oid-20/com/android/commands]$ ll   
total 0
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 am
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 bmgr
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 bu
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 content
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 ime
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 input
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 media
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 pm
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 requestsync
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 settings
drwxr-xr-x  7 xuxu  staff   238B  4  2 10:57 svc
drwxr-xr-x  6 xuxu  staff   204B  4  2 10:57 uiautomator
drwxr-xr-x  3 xuxu  staff   102B  4  2 10:57 wm











Are there any familiar commands? Am, PM, uiautomator ...



Here are some common adb shell commands (where PM, am commands are large and use level four headings)


pm


Package Manager, which can be used to get some application information installed on Android devices

PM Source Pm.java, directly run adb shell Pm can get help information to this command



PM List package Lists the apps installed on the device


Without any options: List all the app's package names (don't know how to find the app's package name for the classmates to see here)


adb shell pm list package

-S: List system applications



-3: List third-party apps


adb shell pm list package -3

-F: List the app package name and the corresponding APK name and storage location


adb shell pm list package -f

-I: Lists the app package names and their installation sources, showing examples of the results:


adb shell pm list package -i

Command Add filter: Filter keyword, can easily find the application you want


parameter combinations, for example, to find知乎the package name in a three-party app, the APK storage location, the installation source:



[xuxu:~]$ adb shell pm list package -f -3 -i zhihupackage:/data/app/com.zhihu.android-1.apk=com.zhihu.android  installer=com..market


PM Path lists the. apk location for the corresponding package name



[xuxu:~]$ adb shell pm path com.tencent.mobileqqpackage:/data/app/com.tencent.mobileqq-1.apk


PM List instrumentation, listing applications with unit test case, followed by parameter-F (as in the PM list package), and [Target-package]



PM dump, followed by the package name, lists the dump information for the specified app, with a variety of information and self-viewing



adb shell pm dump com.tencent.mobileqq



Packages:
Package [com.tencent.mobileqq] (4397f810):
userId=10091 gids=[3003, 3002, 3001, 1028, 1015]
pkg=Package{43851660 com.tencent.mobileqq}
codePath=/data/app/com.tencent.mobileqq-1.apk
resourcePath=/data/app/com.tencent.mobileqq-1.apk
nativeLibraryPath=/data/app-lib/com.tencent.mobileqq-1
versionCode=242 targetSdk=9
versionName=5.6.0
applicationInfo=ApplicationInfo{43842cc8 com.tencent.mobileqq}
flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ]
dataDir=/data/data/com.tencent.mobileqq
supportsScreens=[small, medium, large, xlarge, resizeable, anyDensity]
usesOptionalLibraries: com.google.android.media.effects
com.motorola.hardware.frontcamera
timeStamp=2015-05-13 14:04:24
firstInstallTime=2015-04-03 20:50:07
lastUpdateTime=2015-05-13 14:05:02

signatures=PackageSignatures{4397f8d8 [43980488]}
permissionsFixed=true haveGids=true installStatus=1
pkgFlags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ]
User 0:  installed=true blocked=false stopped=false notLaunched=false enabled=0
grantedPermissions:
android.permission.CHANGE_WIFI_MULTICAST_STATE
com.tencent.qav.permission.broadcast
com.tencent.photos.permission.DATA
com.tencent.wifisdk.permission.disconnect


PM install, installing the app


The target apk is stored on the PC side, please install it with ADB installation

Target apk is stored on Android device, please install it with PM install


PM Uninstall, uninstall the application, with ADB uninstall, followed by the parameters are the package name of the app



PM clear, Erase app data



PM Set-install-location, PM get-install-location, set app install location, get app install location


[0/auto]: Default is Auto

[1/internal]: default is installed inside the phone

[2/external]: Default installation on external storage

  am




Again a huge order ...



Am Start, start an Activity, started the system camera application as an example


Start the camera


[xuxu:~]$ adb shell am start -n com.android.camera/.CameraStarting: Intent { cmp=com.android.camera/.Camera }

Stop the target app before starting



[xuxu:~]$ adb shell am start -S com.android.camera/.Camera
Stopping: com.android.camera
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]     cmp=com.android.camera/.Camera }

Wait for the app to finish booting



[xuxu:~]$ adb shell am start -W com.android.camera/.Camera
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.camera/.Camera }
Status: ok
Activity: com.android.camera/.Camera
ThisTime: 500
TotalTime: 500
Complete

Start a default browser to open a Web page


[xuxu:~]$ adb shell am start -a android.intent.action.VIEW -d http://testerhome.comStarting: Intent { act=android.intent.action.VIEW dat=http://testerhome.com }

Start Dialer Dial 10086


[xuxu:~]$ adb shell am start -a android.intent.action.CALL -d tel:10086            Starting: Intent { act=android.intent.action.CALL dat=tel:xxxxx }


Am instrument, start a instrumentation, unit test or Robotium will use



Am monitor, monitor crash and ANR




[xuxu:~]$ adb shell am monitor
Monitoring activity manager...  available commands:
(q)uit: finish monitoring
** Activity starting: com.android.camera


Am Force-stop, followed by package name, end application



Am StartService, start a service



AM broadcast, send a broadcast



There are a lot of options, self-discovery ~ ~


input




This command can send key events to the Android device, and its source Input.java



Input text, send textual content, cannot send Chinese



adb shell input text test123456

If you first set the keyboard to an English keyboard


Input keyevent, Send key event, Keyevent.java



adb shell input keyevent KEYCODE_HOME

Analog Press the Home button, the source code is defined:

public static final int keycode_home = 3;

Therefore, you can replace the command with theKEYCODE_HOME3


Input tap, sends a touch event to the screen



adb shell input tap 500 500

Click on the screen where coordinates are 500 500


Input swipe, sliding event



adb shell input swipe 900 500 100 500

Swipe the screen from right to left

If the version is not less than 4.4, you can simulate long-press events


adb shell input swipe 500 500 501 501 2000

In fact, in a small distance, in a longer duration of the slide, and finally show the result is the long press action


Here you will find that what Monkeyrunner can do, through the ADB command, can be done, if encapsulated, will do better than MR.


screencap


Command



adb shell screencap -p /sdcard/screen.png

Screenshot, save to SDcard directory

Screenrecord


4.4 New Recording Commands



adb shell screenrecord sdcard/record.mp4

Action phone after command, CTRL + C end recording, record results saved to SDcard

Uiautomator


Executes UI automation tests to get control information for the current interface


Runtest:executes UI Automation tests Runtestcommand.java

Dump: Get control information, Dumpcommand.java


[xuxu:~]$ adb shell uiautomator dump   UI hierchary dumped to: /storage/emulated/legacy/window_dump.xml

When the [file] option is not added, the default is stored under SDcard

IME


Input



[xuxu:~]$ adb shell ime list -s                           com.google.android.inputmethod.pinyin/.PinyinIMEcom.baidu.input_mi/.ImeService

List input methods on a device


[xuxu:~]$ adb shell ime set com.baidu.input_mi/.ImeServiceInput method com.baidu.input_mi/.ImeService selected    

Select Input Method

WM

[xuxu:~]$ adb shell wm sizePhysical size: 1080x1920  

Get Device resolution

Monkey


adb shell monkey-p com. Android. Settings


Settings


ADB shell settings put global wifi_on 1


Dumpsys


ADB Shell Dumpsys | Findstr DUMP


Log


This command is very interesting, you can print the information you set in the Logcat, the specific use of their own thinking!



adb shell log -p d -t xuxu "test adb shell log"

-P: Priority,-t:tag, tag, followed by message



[xuxu:~]$ adb logcat -v time -s xuxu               
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
05-15 13:57:10.286 D/xuxu    (12646): test adb shell log  
Getprop


View the parameter information of the Android device, run only, and the result is displayed as aadb shell getpropkey : valuekey-value pair, such as to get the value of a key:



adb shell getprop ro.build.version.sdk

Get the SDK version of the device

Linux Commands


Operate your Android device, common to the commands, list only, unknown solution!



Cat, CD, chmod, CP, date, DF, Du, grep, kill, LN, LS, lsof, netstat, ping, PS, RM, rmdir, top, touch, redirect symbol > >>, pipe |


Some may need to use BusyBox, it is recommended to install one under WindowsCygwin, do not use Baidu encyclopedia Cygwin

END


To supplement the use of a quotation mark:
Scenario 1, execute the Monkey command on the PC and save the information to the D-disk monkey.log, which reads:



adb shell monkey -p com.android.settings 5000 > d:\monkey.log


Scenario 2, execute the Monkey command on the PC side, save the information to the phone's sdcard, which may be written like this:



adb shell monkey -p com.android.settings 5000 > sdcard/monkey.log

There will be an error here, because eventually it is written to the SDcard directory of the current directory of the PC, not to the sdcard of the phone.


You need to use the quotation marks here:



adb shell "monkey -p com.android.settings 5000 > sdcard/monkey.log"


After all these commands are familiar, then the next step is to synthesize the application of the programming language and think about how to use the language to handle these commands, making these commands more convenient for testing work.



This article was reproduced from: https://testerhome.com/topics/1462



Android Test common ADB command summary


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.