ADB automated testing framework

1. Introduction

        The full name of adb is Android Debug Bridge, which is to play the role of a debugging bridge. The premise of using the adb tool is to open the usb debugging on the mobile phone, and then connect to the computer through the data cable. Use the command mode on the computer to operate the phone: restart, enter recovery, enter fastboot, push file functions, etc. To put it simply, adb is a bridge connecting Android phones and PCs, allowing users to fully operate the phone on the computer.

2. Installation and configuration

        Download adb tools, address: Android SDK - Download  , decompress and put it on the D drive; configure environment variables, add the path in the adb tools just now in Path; in the command line window, enter adb, and a message appears to prove that it has been successful.


3. Commonly used commands

        The command with shell indicates the operating system of Linux itself, and the command without shell is the command of adb itself; 

基础命令:
0、adb connect IP:Port  连接设备(-s:可指定连接设别)
1、adb devices 查看连接的手机设备
2、adb install packsge(包路径) 安装包
3、adb install -r packages(保留数据及缓存文件安装新包)
4、adb uninstall package (卸载包)
5、adb kill-server 关闭adb服务
6、adb start-server 启动adb服务
7、adb shell 进入shell ,进入shell后可执行命令。
7、adb shell pm list packages 查看手机里面所有包名
8、adb shell pm list packages -3查看手机里面所有第三方包名
9、adb shell /system/bin/screencap -p /sdcard/xx.png 截频并保存文件在手机上为xx.Png
10、adb pull /sdcard/xx.png D:/xx.png 将手机xx.png文件保存到D盘文件下 ,下载文件
11、adb push D:/xx.text /adcard/xx.text 将电脑文件导入手机 ,上传文件
12、adb shell pm clear com.ablesky.ui.activity 清除缓存数据
13、adb shell am start -n com.ablesky.ui.activity/com.ablesky.activity.SplashActivity 启动程序
14、adb shell am force-stop com.ablesky.simpleness.activity 强制停止运行程序
15、adb logcat 查看日志
16、adb logcat > D:\log.txt 将日志打印到电脑上
17、netstat -aon|findstr "5037" 查看被占用端口对应的PID
18、 tasklist /fi “PID eq PID号” 查看端口号对应的进程
19、 taskkill /pid 2027 /f  杀掉某进程
20、adb shell monkey -p 包名 100    指定APP产生随机事件100次 
21、adb shell monkey -p 包名 -v -v 100    指定APP产生随机事件100次并发送详细的activity信息  
22、adb root 获取root权限
23、adb remount 获取system文件的读写权限
24、shell rm *.apk 删除系统应用
25、adb install d:\\xxx.apk 通过命令安装安卓应用
26、adb shell dumpsys activity top | find "ACTIVITY"     查看启动的app的包名
27、adb shell dumpsys activity activities | findstr "Run"    查看所有启动的应用的包名
28、adb shell dumpsys window w |findstr \/ |findstr name=    查看当前启动应用的包名
29、adb -s 127.0.0.1:62001 shell pm list package  多台设备,需指定设备名;查看所有的按照应用包名
30、adb shell am start -n 主包名/包名+类名     启动应用程序
31、adb shell am force-stop 主包名       强制关闭程序
32、adb shell ps | findstr browser     ps 是查看当前启动的成效,管道输入,变成查看browse这程序是否在运行



进阶命令:
1、获取手机系统信息( CPU,厂商名称等)
adb shell "cat /system/build.prop | grep "product""
2、获取手机系统版本
adb shell getprop ro.build.version.release
3、获取手机系统api版本
adb shell getprop ro.build.version.sdk
4、获取手机设备型号
adb -d shell getprop ro.product.model
5、获取手机厂商名称
adb -d shell getprop ro.product.brand
6、获取手机的序列号
有两种方式
1、  adb get-serialno
2、  adb shell getprop ro.serialno
7、获取手机的IMEI
有三种方式,由于手机和系统的限制,不一定获取到
1、 adb shell dumpsys iphonesubinfo其中Device ID即为IMEI号
2、 adb shell getprop gsm.baseband.imei
3、 service call iphonesubinfo 1
此种方式,需要自己处理获取的信息得到
8、获取手机mac地址
adb shell cat /sys/class/net/wlan0/address
9、获取手机内存信息
adb shell cat /proc/meminfo
10、获取手机存储信息
adb shell df
获取手机内部存储信息:
魅族手机: adb shell df /mnt/shell/emulated
其他: adb shell df /data
获取sdcard存储信息:
adb shell df /storage/sdcard

11、获取手机分辨率  ,竖线 是管道,前面语句的输出,当做后面语句的输入;过滤某条命令的输出
adb shell "dumpsys window | grep mUnrestrictedScreen"
12、获取手机物理密度
adb shell wm density
13、获取手机包名
1.查看指定apk的包名:aapt dump badging +包的全路径
2.查看所有apk包名:adb shell pm list package -f

        Test commonly used

#查看当前界面的app的包名
adb shell dumpsys window windows | findstr mFocusedApp

#查看启动的app的包名
adb shell dumpsys activity top | find "ACTIVITY"

#查看所有启动的应用的包名
adb shell dumpsys activity activities | findstr "Run"

#查看当前启动应用的包名
adb shell dumpsys window w |findstr \/ |findstr name=

#通过应用查看包名
aapt dump badging D:\test\xxx.apk(APK的全名)

#查看电池状况
adb shell dumpsys battery

#查看分辨率
adb shell wm size

#查看屏幕密度
adb shell wm density

#查看显示屏参数
adb shell dumpsys window displays

#查看IMEI
adb shell dumpsys iphonesubinfo    

4. Linking Devices

        Connect the Android device with a USB cable and let the Android device enter the debugging mode: Developer Mode -> Turn on USB debugging; connect the PC to the Android device through the USB cable, enter the window terminal, and enter adb devices: (Note: The Yeshen emulator may report doesn't match this The fault of client (41); the main reason is that the SDK version does not match; need: copy adb.exe under AndroidSDK\platform-tools to the bin path of the Yeshen Simulator, and rename it to nox_adb.exe; restart the Yeshen Simulator Can.)

Five, input event

        input can be used to simulate input operations of various input devices.      

        Input text : Simulate input text, mainly used to input content in the input box, the premise of input is to get the focus; special characters such as space, &, '' cannot be input directly, you can only use keywvent to input; move left and right, delete during the input process etc. must use keyevent.

adb shell input text  helloworld

        Simulated keys : keyevent is mainly used to simulate keyboard input, so it is only used where the keyboard is used.

#通过键值模拟按键操作:模拟按back键
adb shell input keyevent 4
或 adb shell input keyevent KEYCODE_BACK
adb shell input keyevent 26 # 电源键
adb shell input keyevent 82 # 菜单键
adb shell input keyevent 3 # HOME 键
adb shell input keyevent 4 # 返回键
adb shell input keyevent 24 # 音量+
adb shell input keyevent 25 # 音量-
adb shell input keyevent 164 # 静音 
adb shell input keyevent 85 # 播放/暂停
adb shell input keyevent 86 # 停止播放
adb shell input keyevent 87 # 播放下一首
adb shell input keyevent 88 # 播放上一首
adb shell input keyevent 126 # 恢复播放
adb shell input keyevent 127 # 暂停播放

        Click tap : used to simulate the touch of a finger on the screen

#通过坐标模拟点击操作:点击横坐标300纵坐标500的点
adb shell input tap 300 500

        other:

#通过坐标模拟滑动操作:从坐标(200,200)滑动到(300,300),滑动时间100ms
adb shell input swipe 200 200 300 300 100

#通过坐标模拟拖动操作:从坐标(100,200)拖动到(300,400),滑动时间2000ms
adb shell input draganddrop 100 200 300 400 2000

#通过坐标模拟长按操作:从坐标(200,300)的点滑到(400,500),滑动时间2000ms,由于划动距离短,在一个图标之内则主观显示为长按操作
adb shell input swipe 200 300 400 500 2000

#通过坐标/键值模拟同时按下两个键,用and连接:电源键亮屏并点击菜单
adb shell input keyevent KEYCODE_POWER and KEYCODE_MENU


#获取当前页面的控件信息文件,可用于查找元素的属性;生成的是 xml文件
adb shell uiautomator dump

6. Case study

Seven, os module

        os refers to operate system. The os module is a module in the python standard library for accessing operating system functions. Using the interface provided in the os module, cross-platform access can be achieved. General operations: 1. Obtain platform information, 2. Operate directories, and 3. Judgment operations. In the test, the os.system(cmd) method is used the most. By passing in the cmd command, it is equivalent to directly entering the command on the command line.
 

Guess you like

Origin blog.csdn.net/guanrongl/article/details/124787781