掌握ADB:详解操作命令及完整用法指南(二)
前言
ADB,全名Android Debug Bridge,是Android提供的一个通用的调试工具,是一个C/S架构的命令行工具,通过这个工具,使得我们的PC能够和Android设备来进行通信。之前一篇文章我们介绍了adb安装以及一些adb的基础命令,本文我们将介绍一些我们在进行app自动化测试时经常使用到的命令。
adb shell am命令
am
为Activity Manager
的缩写,即活动管理,我们可以使用am
来控制设备执行各种操作。如下:
# 使用am命令来启动淘宝App adb shell am start com.taobao.taobao/com.taobao.tao.TBMainActivity
还可以用来获取应用的启动时间,如下:
adb shell am start -W com.qiniu.pili.droid.shortvideo.effect.demo/com.qiniu.pili.droid.shortvideo.demo.activity.MainActivity Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.qiniu.pili.droid.shortvideo.effect.demo/com.qiniu.pili.droid.shortvideo.demo.activity.MainActivity } Status: ok Activity: com.qiniu.pili.droid.shortvideo.effect.demo/com.qiniu.pili.droid.shortvideo.demo.activity.MainActivity ThisTime: 289 TotalTime: 289 WaitTime: 311 Complete 以毫秒为单位 点击图标然后松开,这个时间是系统时间(分配内存和CPU的时间),应用application等待资源启动的时间 ThisTime:为界面启动耗时 TotalTime:应用自身启动耗时=ThisTime + 应用application等资源启动时间 WaitTime:系统启动耗时=TotalTime + 系统资源启动时间
获取包名和界面名
adb shell dumpsys window | grep mFocusedApp 或者 adb shell "dumpsys window | grep mCurrentFocus"
注:App的包名是唯一的,不可重复的。
adb模拟手机按键
在使用adb模拟手机按键之前,我们需要先做好设置,设置路径为:设置——开发者选项——指针位置
,我们可以先运行adb shell input
命令来查看如何模拟手机按键:
# 首先连接设备 adb connect 127.0.0.1:7555 connected to 127.0.0.1:7555 # 执行命令 adb shell input Usage: input [<source>] <command> [<arg>...] The sources are: mouse keyboard joystick touchnavigation touchpad trackball stylus dpad touchscreen gamepad The commands and default sources are: text <string> (Default: touchscreen) keyevent [--longpress] <key code number or name> ... (Default: keyboard) tap <x> <y> (Default: touchscreen) swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen) press (Default: trackball) roll <dx> <dy> (Default: trackball)
操作示例如下:
- 模拟点击事件 adb shell input tap x坐标 y坐标 adb shell input tap 528 1539 - 输入文本 首先需要把光标移到输入框,然后执行以下命令 adb shell input text zengzengzeng - 模拟滑动事件 adb shell swip <起点x> <起点y> <终点x> <终点y> <滑动时长> adb shell input swipe 520 331 520 1017 2000 2000为滑动时间,单位是毫秒 - 返回键 adb shell input keyevent 4 - 返回home键(置应用于后台) adb shell input keyevent 3 - 音量放大 adb shell input keyevent 24 - 音量缩小 adb shell input keyevent 25
资源占用情况
我们也可以使用adb命令查看设备的CPU、内存等资源使用情况,如下:
# 查看当前系统CPU使用情况 adb shell dumpsys cpuinfo 获取的是一段时间的均值 # 进程占用情况 adb shell top adb shell top | grep "包名" —— 查看某个包的一些性能 adb shell top -d 1 |grep "包名" # 内存使用情况 adb shell dumpsys meminfo
总结
本文主要介绍了adb的一些高级用法,adb shell am
以及相关命令对我们在使用appium进行App自动化测试时有很大的帮助,可以帮我们快速定位应用及活动名称,一些性能指标查看命令也可以帮助我们更好地执行App测试,希望本文能够帮到大家。