Appium+python automation (16) - ADB command, whether you know it or not, it should be a must know (super detailed explanation)

Introduction

  The Android Debug Bridge (adb) is a multipurpose tool that helps you manage the state of your device or emulator.

  adb (Android Debug Bridge) is a general-purpose command-line tool that allows you to communicate with an emulator instance or a connected Android device. It facilitates various device operations such as installation and commissioning of applications.

  Tips: Find the adb tool in android_sdk/platform-tools/, and then configure the environment variable according to its specific path. Then start cmd and enter 'adb' to see if the configuration is successful.

Some friends may ask, why do I need to learn and master these things, and why do I have to share them briefly, because such problems are often encountered in real specific work, here is a short story to illustrate.

QA: "Who is that? I just operated this APP as I did last time. I can't afford to run the test script." question")

DEV: "Huh? There is still such a problem. Then, can you check if your mobile phone is connected to the computer? (Inner monologue: the data cable must not be connected properly)"

QA: This, this... "Hey, what do you think of this? (Internal monologue: I'm going, how do I know how to read it? If you don't tell me, I ask my phone and computer, and they don't answer me)"

DEV: "Look at its port again, whether it is occupied, and whether there is any error in the log. (Inner monologue: I despise it, I don't understand anything, how simple this is)"

a while passed...

DEV: "Balabala, there are a lot of questions that I want you to check (inner monologue: small sample, come and ask me if you don't read anything)"

QA: "Stars are popping up on my head, my baby is suffering, but my baby doesn't say anything! (Inner monologue: this. Alas, I don't even know)"

After you understand and master the adb command:

QA: "So who, I just operated this APP as last time, and I can't run the test script. I also check the connection status of the device, and the package and activity of the installation package, etc., and I The Appium Desired Capabilities parameters configured in the code are all correct. Let’s see what the problem is. Oh, yes, I checked the log. It should be an error somewhere. Go to the place I mentioned and take a look. Yes or no"

DEV: "That's awesome Word! Let me check this, if there is something wrong with the package installed, please wait a moment. (Inner monologue: It's so awesome, I've checked everything, and helped me locate the problem) "

End!

There are several ways to join adb:

  • Run shell commands on the device
  • Manage emulators or devices with port forwarding
  • Copy files to and from the simulator or device

The following introduces adb and describes common uses.

1, overview

Android debugging system is a customer service system, including three components:

  • A client that runs on the computer you use to develop programs. You can use the adb command to start the client through the shell. Other Android tools such as the ADT plugin and DDMS can also generate adb clients.
  • A server that runs as a background process on the machine you use to send. This server is responsible for managing the communication between the client and the adb daemon running on the emulator or device. .
  • A daemon that runs as a background process on the emulator or device. .

When you start an adb client, the client first confirms whether there is an adb service process running. If not, start the service process. When the server is running, the adb server binds to the local TCP port 5037 and listens for commands from the adb client—all adb clients use port 5037 to talk to the adb server.

The server then connects all running emulator or device instances. It locates all emulators or devices by scanning all odd-numbered ports in the range 5555 to 5585. Once the server finds the adb daemon, it establishes a connection to that port. Note that any emulator or device instance will get two consecutive ports - an even port for console connections, and an odd port for adb connections. For example:

emulator 1, console: port 5554
emulator 1, adb port 5555
console: port 5556
adb port 5557...

As shown above, the emulator instance connects to adb through port 5555, just like using port 5554 to connect to the console.

Once the server is connected to all emulator instances, the instance can be controlled and accessed using adb commands. Because the server manages connections to emulator/device instances, and controls handling commands from multiple adb clients, you can control any emulator or device instance from any client (or script).

The following sections describe using adb with commands and managing the state of the emulator/device. It should be noted that if you use Eclipse with the ADT plug-in to develop Android programs, you don't need to use adb through the command line. The ADT plug-in has transparently integrated adb into Eclipse. Of course, you can still use adb directly if necessary, such as debugging.

2. Issue the adb command

Issue Android commands: You can issue Android commands on the command line or script on your development machine, using:

adb [-d|-e|-s <serialNumber>] <command> 

 When you issue a command, the system enables the Android client. Clients are not tied to emulator instances, so if dual servers/device are running, you need to use  -d options to identify the target instance for the command that should be controlled. For more information on using this option, see Emulator/Device Instance Terminology Control Commands. 

3. Check the adb version

adb  version

4. Connect to the Yeshen simulator (the connection between the simulator and the real machine is demonstrated here respectively)

adb connect 127.0.0.1:62001

Tips:

The ports of the Yeshen simulator are regular, the first simulator port is 62001, the second simulator port is 62025, the third is 62025+1, and so on.

  • Emulator 1: Android 4.4.2 Address: 127.0.0.1:62001
  • Emulator 2: Andriod 5.1.1 Address: 127.0.0.1:62025

More details: Detailed explanation of adb commands for Yeshen Android emulator

5. Query the simulator/device instance

Before issuing adb commands, it is necessary to know what emulator/device instance is connected to the adb server. devices A list of associated emulators/devices can be obtained by using the command:

adb devices

• In response, adb produces the corresponding status information for each instance:

  • <type>-<consolePort> • Serial Number - A string created by adb that uniquely identifies an emulator/device instance via its own control port . The following is an example of a serial number: emulator-5554
  • There are three states of the connection state of the instance:
    • offline — This instance is not connected to adb or is not responding.
    • device — This instance is connecting with the adb server. Note that this state does not 100% indicate that the Android system is running and operating, so this instance is connected to adb when the system is running. However, after the system boots, it is a normal running state of an emulator/device state.

The output of each instance has the following fixed format:

[serialNumber] [state]

Here is an devices example showing the command and output:

$ adb devicesList of devices attached emulator-5554  deviceemulator-5556  deviceemulator-5558  device

adb returns if no emulator/device is currently running  no device . 

6. Send commands to specific emulator/device instances

If you have multiple emulator/device instances running, you need to specify a target instance when issuing adb commands. To do so, use -s the option command. The options in use -s are

adb -s <serialNumber> <command> 

As shown above, a command is given a target instance, which uses the serial number assigned by adb. You can  devices get the serial number of a running emulator/device instance using the command

Examples are as follows:

adb -s emulator-5556 install helloWorld.apk

Note that  -s adb will generate an error if this command is executed without specifying a target emulator/device instance. 

7. Install the software

You can use adb to copy an application from your development computer and install it on an emulator/device instance. To do so, use install the command. This install command requires you to specify the path of the .apk file you want to install:

adb install <path_to_apk>

For more information on how to create an .apk file that can be installed on an emulator/device instance, see Android Asset Packaging Tool (aapt).

It should be noted that if you are using the Eclipse IDE and have installed the ADT plugin, then there is no need to use adb (or aapt) directly to install the application on the emulator/device. Otherwise, the ADT plugin handles the packaging and installation of the application on your behalf. 

8. Port forwarding

forward Arbitrary port forwarding can be done using  commands - forwarding requests from a specific host port of one emulator/device instance to a different port. The following demonstrates how to establish a forwarding from host port 6100 to emulator/device port 7100.

adb forward tcp:6100 tcp:7100

Similarly, you can use adb to create a named abstract UNIX domain socket, the above process is as follows:

adb forward tcp:6100 local:logd  

9. Copy in or out files from the simulator/device

 Files can be copied to or from a data file of an emulator/device instance using the adb pull , command.  Unlike the command which only copies an .apk file to a specific location, the  and   command lets you copy arbitrary directories and files to any location on an emulator/device instance.pushinstallpullpush

To copy a file or directory from an emulator or device, use (command as follows):

adb pull <remote> <local>

To copy a file or directory to an emulator or device, use (command below)

adb push <local> <remote>

In these commands,  <local> and <remote> refer to the path to the target file/directory on your own development machine (local) and emulator/device instance (remote), respectively

Below is an example::

adb push foo.txt /sdcard/foo.txt 

10. Adb command list

The following table lists all commands supported by adb, and explains their meaning and usage. 

Category Command Description Comments
Options -d Only manage abd through USB interface. Returns an error if not just using the USB interface for management.
-e Manage adb through emulator instance only. Returns an error if not managed only by the emulator instance.
-s <serialNumber> Send commands to manage adb with the allowed command number of the emulator/device (eg: "emulator-5556"). If no number is specified, an error will be reported.
General devices See a list of all facilities connected to the simulator/device. See Querying for Emulator/Device Instances for more information.
help View all commands supported by adb. .
version Check the version serial number of adb.
Debug logcat [<option>] [<filter-specs>] Output log data to the screen.
bugreport View bug reports such as dumpsys , dumpstate , and logcat info.
jdwp View available JDWP information for the specified facility. Port mapping information can be used  forward jdwp:<pid> to connect to the specified JDWP process. For example: 
adb forward tcp:8000 jdwp:472 
jdb -attach localhost:8000
Data install <path-to-apk> Install Android as (you can specify the full path to emulator/facilities-data-file.apk).
pull <remote> <local> Copies the specified file from the emulator/installation to the computer.
push <local> <remote> Copy the specified file from the computer to the emulator/device.
Ports and Networking forward <local> <remote> Use the locally specified port to remotely connect to the emulator/facility via the socket method The port needs to describe the following information:
  • tcp:<portnum>
  • local:<UNIX domain socket name>
  • dev:<character device name>
  • jdwp:<pid>
ppp <tty> [parm]... Run ppp via USB:
  • <tty> — the tty for PPP stream. For exampledev:/dev/omap_csmi_ttyl.
  • [parm]...  &mdash zero or more PPP/PPPD options, such as defaultroute ,local , notty , etc.

Need to remind you that the PDP connection cannot be started automatically.

Scripting get-serialno View the serial number of the adb instance. See Querying for Emulator/Device Instances for more information.
get-state View the current status of the simulator/facility.
wait-for-device 如果设备不联机就不让执行,--也就是实例状态是 device 时. 你可以提前把命令转载在adb的命令器中,在命令器中的命令在模拟器/设备连接之前是不会执行其它命令的. 示例如下:
adb wait-for-device shell getprop
需要提醒的是这些命令在所有的系统启动启动起来之前是不会启动adb的 所以在所有的系统启动起来之前你也不能执行其它的命令. 比如:运用install 的时候就需要Android包,这些包只有系统完全启动。例如:
adb wait-for-device install <app>.apk
上面的命令只有连接上了模拟器/设备连接上了adb服务才会被执行,而在Android系统完全启动前执行就会有错误发生.
Server start-server 选择服务是否启动adb服务进程.
kill-server 终止adb服务进程.
Shell shell 通过远程shell命令来控制模拟器/设备实例. 查看 获取更多信息 for more information.
shell [<shellCommand>] 连接模拟器/设施执行shell命令,执行完毕后退出远程shell端l.

 启动shell命令

Adb 提供了shell端,通过shell端你可以在模拟器或设备上运行各种命令。这些命令以2进制的形式保存在本地的模拟器或设备的文件系统中:

/system/bin/...

不管你是否完全进入到模拟器/设备的adb远程shell端,你都能 shell 命令来执行命令.

当没有完全进入到远程shell的时候,这样使用shell 命令来执行一条命令:

adb [-d|-e|-s {<serialNumber>}] shell <shellCommand>

在模拟器/设备中不用远程shell端时,这样使用shell 命 :

adb [-d|-e|-s {<serialNumber>}] shell

通过操作CTRL+D 或exit 就可以退出shell远程连接.

下面一些就将告诉你更多的关于shell命令的知识. 

11、通过远程shell端运行sqllite3连接数据库 

通过adb远程shell端,你可以通过Android软sqlite3 命令程序来管理数据库。sqlite3 工具包含了许多使用命令,比如:.dump 显示表的内容,.schema 可以显示出已经存在的表空间的SQL CREATE结果集。Sqlite3还允许你远程执行sql命令.

通过sqlite3 , 按照前几节的方法登陆模拟器的远程shell端,然后启动工具就可以使用sqlite3 命令。当sqlite3 启动以后,你还可以指定你想查看的数据库的完整路径。模拟器/设备实例会在文件夹中保存SQLite3数据库. /data/data/<package_name>/databases/ .

示例如下:

$ adb -s emulator-5554 shell# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.dbSQLite version 3.3.12Enter ".help" for instructions.... enter commands, then quit...sqlite> .exit 

当你启动sqlite3的时候,你就可以通过shell端发送 sqlite3 ,命令了。用exit 或 CTRL+D 退出adb远程shell端.

12、UI/软件 试验程序 Monkey

当Monkey程序在模拟器或设备运行的时候,如果用户出发了比如点击,触摸,手势或一些系统级别的事件的时候,它就会产生随机脉冲,所以可以用Monkey用随机重复的方法去负荷测试你开发的软件.

最简单的方法就是用用下面的命令来使用Monkey,这个命令将会启动你的软件并且触发500个事件.

$ adb shell monkey -v -p your.package.name 500

更多的关于命令Monkey的命令的信息,可以查看UI/Application Exerciser Monkey documentation page.

文档页面

13、其它的shell命令

下面的表格列出了一些adbshell命令,如果需要全部的命令和程序,可以启动模拟器实例并且用adb -help 命令 .

adb shell ls /system/bin

对大部门命令来说,help都是可用的.

Shell Command Description Comments
dumpsys 清除屏幕中的系统数据n. Dalvik Debug Monitor Service (DDMS)工具提供了完整的调试、.
dumpstate 清除一个文件的状态.
logcat [<option>]... [<filter-spec>]... 启动信息日志并且但因输出到屏幕上.
dmesg 输出主要的调试信息到屏幕上.
start 启动或重启一个模拟器/设备实例.
stop 关闭一个模拟器/设备实例.

14、启用logcat日志

Android日志系统提供了记录和查看系统调试信息的功能。日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命令来查看和使用. 

14.1使用logcat命令

你可以用 logcat 命令来查看系统日志缓冲区的内容:

[adb] logcat [<option>] ... [<filter-spec>] ... 

请查看Listing of logcat Command Options ,它对logcat命令有详细的描述 .

你也可以在你的电脑或运行在模拟器/设备上的远程adb shell端来使用logcat 命令,也可以在你的电脑上查看日志输出。

$ adb logcat

你也这样使用:

# logcat 

14.2过滤日志输出

每一个输出的Android日志信息都有一个标签和它的优先级.

  • 日志的标签是系统部件原始信息的一个简要的标志。(比如:“View”就是查看系统的标签).
  • 优先级有下列集中,是按照从低到高顺利排列的:
    • V — Verbose (lowest priority)
    • D — Debug
    • I — Info
    • W — Warning
    • E — Error
    • F — Fatal
    • S — Silent (highest priority, on which nothing is ever printed)

在运行logcat的时候在前两列的信息中你就可以看到 logcat 的标签列表和优先级别,它是这样标出的:<priority>/<tag> .

下面是一个logcat输出的例子,它的优先级就似乎I,标签就是ActivityManage:

I/ActivityManager(  585): Starting activity: Intent { action=android.intent.action...}

为了让日志输出能体现管理的级别,你还可以用过滤器来控制日志输出,过滤器可以帮助你描述系统的标签等级.

过滤器语句按照下面的格式描tag:priority ... , tag 表示是标签, priority 是表示标签的报告的最低等级. 从上面的tag的中可以得到日志的优先级. 你可以在过滤器中多次写tag:priority .

这些说明都只到空白结束。下面有一个列子,例子表示支持所有的日志信息,除了那些标签为”ActivityManager”和优先级为”Info”以上的和标签为” MyApp”和优先级为” Debug”以上的。 小等级,优先权报告为tag.

adb logcat ActivityManager:I MyApp:D *:S

上面表达式的最后的元素 *:S ,,是设置所有的标签为"silent",所有日志只显示有"View" and "MyApp"的,用 *:S 的另一个用处是 能够确保日志输出的时候是按照过滤器的说明限制的,也让过滤器也作为一项输出到日志中.

下面的过滤语句指显示优先级为warning或更高的日志信息:

adb logcat *:W

如果你电脑上运行logcat ,相比在远程adbshell端,你还可以为环境变量ANDROID_LOG_TAGS :输入一个参数来设置默认的过滤

export ANDROID_LOG_TAGS="ActivityManager:I MyApp:D *:S"

需要注意的是ANDROID_LOG_TAGS 过滤器如果通过远程shell运行logcat 或用adb shell logcat 来运行模拟器/设备不能输出日志. 

14.3控制日志输出格式

日志信息包括了许多元数据域包括标签和优先级。可以修改日志的输出格式,所以可以显示出特定的元数据域。可以通过 -v 选项得到格式化输出日志的相关信息.

  • brief — Display priority/tag and PID of originating process (the default format).
  • process — Display PID only.
  • tag — Display the priority/tag only.
  • thread — Display process:thread and priority/tag only.
  • raw — Display the raw log message, with no other metadata fields.
  • time — Display the date, invocation time, priority/tag, and PID of the originating process.
  • long — Display all metadata fields and separate messages with a blank lines.

当启动了logcat ,你可以通过-v 选项来指定输出格式:

[adb] logcat [-v <format>]

下面是用 thread 来产生的日志格式:

[adb] logcat [-v <format>]

需要注意的是你只能-v 选项来规定输出格式 option. 

14.4查看可用日志缓冲区

Android日志系统有循环缓冲区,并不是所有的日志系统都有默认循环缓冲区。为了得到日志信息,你需要通过-b 选项来启动logcat 。如果要使用循环缓冲区,你需要查看剩余的循环缓冲期:

  • radio — 查看缓冲区的相关的信息.
  • events — 查看和事件相关的的缓冲区.
  • main — 查看主要的日志缓冲区

-b 选项使用方法:

[adb] logcat [-b <buffer>]

下面的例子表示怎么查看日志缓冲区包含radio 和 telephony信息:

adb logcat -b radio 

14.5查看stdout 和stderr

在默认状态下,Android系统有stdout 和 stderr (System.out 和System.err )输出到/dev/null ,在运行Dalvik VM的进程中,有一个系统可以备份日志文件。在这种情况下,系统会用stdout 和stderr 和优先级 I.来记录日志信息

通过这种方法指定输出的路径,停止运行的模拟器/设备,然后通过用 setprop 命令远程输入日志

$ adb shell stop$ adb shell setprop log.redirect-stdio true$ adb shell start

系统直到你关闭模拟器/设备前设置会一直保留,可以通过添加/data/local.prop 可以使用模拟器/设备上的默认设置 

14.6Logcat命令列表

Option Description
-b <buffer> 加载一个可使用的日志缓冲区供查看,比如event 和radio . 默认值是main 。具体查看Viewing Alternative Log Buffers.
-c 清楚屏幕上的日志.
-d 输出日志到屏幕上.
-f <filename> 指定输出日志信息的<filename> ,默认是stdout .
-g 输出指定的日志缓冲区,输出后退出.
-n <count> 设置日志的最大数目<count> .,默认值是4,需要和 -r 选项一起使用。
-r <kbytes> <kbytes> 时输出日志,默认值为16,需要和-f 选项一起使用.
-s 设置默认的过滤级别为silent.
-v <format> 设置日志输入格式,默认的是brief 格式,要知道更多的支持的格式,参看Controlling Log Output Format .

15、Stopping the adb Server

在某些情况下,你可能需要终止Android 调试系统的运行,然后再重新启动它。 例如,如果Android 调试系统不响应命令,你可以先终止服务器然后再重启,这样就可能解决这个问题.

kill-server 可以终止adb server。你可以用adb发出的任何命令来重新启动服务器.

16、小结

以上介绍那么多是不是都需要掌握,答案是:NO,学习android测试,adb是必学的,以下是几个常用的指令需要熟练掌握

一、检查设备

1.如何检查手机(或模拟器)是连上电脑的,在cmd输入:

>adb devices

2.一定要看到上图红色区域的,设备名称,然后接着是device(如果看到这里是offline,那就是adb端口被占了)

二、安装app

1.如何给电脑上的android模拟器安装app呢?

第一步:先下载apk文件包放到桌面上

第二步:cmd端口输入adb install app的路径

第三步:把apk包拖到cmd窗口,回车就能安装了

2.安装成功后看到Success,模拟器上会出现淘宝的app

三、卸载app

1.在cmd输入以下指令,可以删除app

>adb uninstall 包名

(这里是app的包名,不是文件名,包名用前面的aapt工具查看)

2.以淘宝为例,淘宝的包名是:com.taobao.taobao

四、其它的几个指令

1.杀掉adb进程

  adb kill-server
2.重启adb服务
  adb start-server

3.重启手机 adb reboot

4.进shell模式

 adb shell

5.挂载

 adb remount

6.从电脑发文件到手机

adb push <本地路径> <远程路径>

7.从手机下载文件到本地

adb pull <远程路径> <本地路径>

8.输出日志

第一种:输出到手机存储卡

adb logcat > /sdcard/mylogcat.txt

第二种:输出到电脑上
adb logcat > D:/Temp/1.txt(1.txt必须在电脑上存在,才能写入logcat内容)

 好了各位小伙伴们,今天就分享到这里了


              【下面是我整理的2023年最全的软件测试工程师学习知识架构体系图】


一、Python编程入门到精通

二、接口自动化项目实战

三、Web自动化项目实战


四、App自动化项目实战

五、一线大厂简历


六、测试开发DevOps体系

七、常用自动化测试工具


Eight, JMeter performance test

9. Summary (little surprise at the end)

life is long so add oil. Every effort will not be let down, as long as you persevere, there will be rewards in the end. Cherish your time and pursue your dreams. Don't forget the original intention, forge ahead. Your future is in your hands!

Life is short, time is precious, we cannot predict what will happen in the future, but we can grasp the present moment. Cherish every day and work hard to make yourself stronger and better. Firm belief, persistent pursuit, success will eventually belong to you!

Only by constantly challenging yourself can you constantly surpass yourself. Persist in pursuing your dreams and move forward bravely, and you will find that the process of struggle is so beautiful and worthwhile. Believe in yourself, you can do it!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/131829333