> 技术文档 > 【全网最全】鸿蒙 HDC 命令合集 (awesome-hdc)_鸿蒙hdc

【全网最全】鸿蒙 HDC 命令合集 (awesome-hdc)_鸿蒙hdc

最近在搞鸿蒙Next自动化,HDC命令作为最基础的调试工具,发现全网没有比较完整的命令介绍,于是整理了这篇文档。

由于鸿蒙生态还处于初期,官方提供的hdc命令还在不断修改中,这篇文档更新不及时,请以github为准https://github.com/codematrixer/awesome-hdc
如果有问题欢迎去提PR和Issue,也欢迎点Star ⭐️ ⭐️ ⭐️ ⭐️ ⭐️
https://github.com/codematrixer/awesome-hdc

HDC(OpenHarmony Device Connector) 是为鸿蒙开发/测试人员提供的用于设备调试的命令行工具,类似Android端的ADB工具。

HDC主要有三部分组成

  1. hdc client:运行于电脑上的客户端,用户可以在电脑命令终端(windows cmd/linux shell)下请求执行相应的hdc命令。
  2. hdc server:作为后台进程也运行于电脑上,server管理client和设备端daemon之间通信包括连接的复用、数据通信包的收发,以及个别本地命令的直接处理。
  3. hdc daemon:daemon部署于OpenHarmony设备端作为守护进程按需运行,负责处理来自client端请求。
    在这里插入图片描述

整体架构和Android系统的ADB架构非常类似。

文章目录

  • HDC安装
  • 基本用法
    • 基本语法
  • 设备连接管理
    • 查看HDC版本
    • 启动/停止 HDC Server
    • 查询设备列表
    • 查询设备UDID
    • 重启手机
  • 查看设备信息
    • 名称
    • Brand
    • Model
    • 系统版本
    • OS版本
    • CPU架构
    • 分辩率
    • wlan IP
    • 电量/温度
    • 查看屏幕信息
    • 查看屏幕旋转状态
    • 查看屏幕亮屏状态
    • 点亮屏幕(唤醒)
    • 查看网络状态
  • 应用管理
    • 安装应用
    • 卸载应用
    • 获取应用列表
    • 启动应用
    • 退出应用
    • 获取应用版本
    • Dump应用信息
      • 获取应用 Ability信息
      • 获取应用详情
    • 清除应用数据
      • 清除应用缓存
      • 清除应用数据
    • 显示可调试应用列表
  • 端口转发
    • 显示端口转发列表
    • 本地端口转发到手机
    • 删除端口转发任务
  • 无线调试
  • 文件传输
    • 从本地电脑发送文件至手机
    • 从手机拷贝文件至本地电脑
  • UI模拟操作(点击滑动等)
  • 屏幕截图
  • 屏幕录屏
  • 打开Scheme (URL)
  • 获取页面布局信息(控件树)
  • 录制用户操作
  • 系统日志(log)
  • 导出日志
  • 导出crash日志
  • hidumper工具
    • list system abilities
    • RenderService
    • DisplayManagerService
    • PowerManagerService
    • BatteryService
    • NetConnManager
    • StorageManager
  • aa工具
    • start
    • stop-service
    • force-stop
    • test
    • attach
    • detach
    • appdebug
  • bm工具
    • install
    • uninstall
    • dump
    • clean
    • enable
    • disable
    • get
  • param工具
  • Instrument Test
  • 性能工具
  • 参考链接

HDC安装

  • 下载 Command Line Tools 并解压

  • hdc文件在command-line-tools/sdk/HarmonyOS-NEXT-DB2/openharmony/toolchains目录下

  • 配置电脑环境变量,以macOS为例,在~/.bash_profile 或者 ~/.zshrc文件中添加如下内容:

export HM_SDK_HOME=\"/Users/develop/command-line-tools/sdk/HarmonyOS-NEXT-DB2\" //请以sdk实际安装目录为准export PATH=$PATH:$HM_SDK_HOME/hms/toolchains:$HM_SDK_HOME/openharmony/toolchainsexport HDC_SERVER_PORT=7035

也可以自行编译安装:参考鸿蒙官方gitree文档

基本用法

基本语法

hdc -t  

如果只有一个设备/模拟器连接时,可以省略掉-t 这一部分,直接使用hdc 。在多个设备/模拟器连接的情况下需要指定-t 参数connectKey可以通过hdc list targets命令获取,对应Android里的adb devices获取的serialNumber

$ hdc list targets127.0.0.1:5555 //:形式的connectKey ,一般为无线连接的设备或模拟器FMR0223C13000649

比如给FMR0223C13000649 这个设备安装应用:

$ hdc -t FMR0223C13000649 install entry-default-signed.hap[Info]App install path:/Users/develop/entry-default-signed.hap, queuesize:0, msg:install bundle successfully.AppMod finish

注意事项

  • 使用hdc,如果出现异常,可以尝试通过hdc kill -r命令杀掉并重启hdc服务。
  • 如果出现hdc list targets获取不到设备信息的情况,可以通过任务管理器查看是否有hdc进程存在。若进程存在,则通过hdc kill -r命令杀掉该进程。

设备连接管理

查看HDC版本

$ hdc -vVer: 2.0.0a

启动/停止 HDC Server

停止

$ hdc killKill server finish

重启

$ hdc start -r

查询设备列表

$ hdc list targets127.0.0.1:5555FMR0223C13000649

-v 选项 显示详细信息

$ hdc list targets -v127.0.0.1:5555 TCP Connected localhostFMR0223C13000649USB Connected unknown...

输出的内容第一列为设备的connectKey, 第二列是设备连接方式,第三列为设备连接状态,第四列暂时未知

查询设备UDID

$ hdc shell bm get --udidudid of current device is :C46284C052AE01BBD2358FE44B279524B508FC959AAB5F4B0B74E42A06569B7E

这个udid在用开发者账号打包时,需要添加这个udid到对应的profile文件中

重启手机

$ hdc target boot

查看设备信息

名称

$ hdc shell param get const.product.name  HUAWEI Mate 60 Pro

Brand

$ hdc shell param get const.product.brandHUAWEI 

Model

$ hdc shell param get const.product.modelALN-AL00 

系统版本

$ hdc shell param get const.product.software.version ALN-AL00 5.0.0.22(SP35DEVC00E22R4P1log) 

OS版本

$ hdc shell param get const.ohos.apiversion 12 

CPU架构

$ hdc shell param get const.product.cpu.abilist arm64-v8a 

分辩率

$ hdc shell hidumper -s RenderService -a screen-------------------------------[ability]-----------------------------------------------------------------RenderService----------------------------------- ScreenInfoscreen[0]: id=0, powerstatus=POWER_STATUS_OFF, backlight=51, screenType=EXTERNAL_TYPE, render size: 1260x2720, physical screen resolution: 1260x2720, isvirtual=false, skipFrameInterval_:1 supportedMode[0]: 1260x2720, refreshrate=120 supportedMode[1]: 1260x2720, refreshrate=90 supportedMode[2]: 1260x2720, refreshrate=60 supportedMode[3]: 1260x2720, refreshrate=30 activeMode: 1260x2720, refreshrate=60 capability: name=, phywidth=72, phyheight=156,supportlayers=12, virtualDispCount=0, propCount=0, type=DISP_INTF_HDMI, supportWriteBack=false

执行上述命令后,解析返回内容,可以通过正则表达式提取1260x2720

wlan IP

$ hdc shell ifconfiglo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:99055 errors:0 dropped:0 overruns:0 frame:0 TX packets:99055 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:5889697 TX bytes:5889697wlan0 Link encap:Ethernet HWaddr ea:f9:7d:21:52:31 inet addr:172.31.125.111 Bcast:172.31.125.255 Mask:255.255.254.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1232924 errors:0 dropped:0 overruns:0 frame:0 TX packets:2061202 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:877179224 TX bytes:2570352818p2p0 Link encap:Ethernet HWaddr d2:0d:f7:cc:12:fb UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 TX bytes:0chba0 Link encap:Ethernet HWaddr ec:11:05:fb:18:66 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 TX bytes:0

注意:这个命令在Beta3版本之前,会提示Cannot open netlink socket: Permission denied,需要升级系统。

电量/温度

$ hdc shell hidumper -s BatteryService -a -i -------------------------------[ability]-----------------------------------------------------------------BatteryService---------------------------------Current time: 2024-05-30 12:08:37.419capacity: 100 batteryLevel: 1 chargingStatus: 3 healthState: 1 pluggedType: 1 voltage: 4496732 present: 1 technology: Li-poly nowCurrent: 123 currentAverage: 83 totalEnergy: 5203 remainingEnergy: 5207 remainingChargeTime: 0 temperature: 280 chargeType: 1 

查看屏幕信息

$ hdc shell hidumper -s DisplayManagerService -a -a-------------------------------[ability]-----------------------------------------------------------------DisplayManagerService------------------------------------------------ DMS Multi User Info --------------[oldScbPid:][userId:] 100[ScbPid:] 4438---------------- Screen ID: 0 ----------------FoldStatus:  UNKNOWN[SCREEN SESSION]Name: UNKNOWNRSScreenId:  0activeModes: 0, 1260, 2720, 120SourceMode:  0ScreenCombination: 0Orientation:  0Rotation:  0ScreenRequestedOrientation: 0[RS INFO]SupportedColorGamuts: 0, 4, 6ScreenColorGamut: 4GraphicPixelFormat:  0SupportedScreenHDRFormat: 2, 1, 3ScreenHDRFormat:  2SupportedColorSpaces: 0, 2294273, 2294278ScreenColorSpace: 2294273[CUTOUT INFO]WaterFall_L: 0, 0, 0, 0WaterFall_T: 0, 0, 0, 0WaterFall_R: 0, 0, 0, 0WaterFall_B: 0, 0, 0, 0BoundingRects: [494, 36, 273, 72][SCREEN INFO]VirtualWidth:  387VirtualHeight: 836LastParentId:  18446744073709551615ParentId:  1IsScreenGroup: 0VirtualPixelRatio: 3.25Rotation:  0Orientation:  0SourceMode:  0ScreenType:  1[SCREEN PROPERTY]Rotation:  0Density:3.25DensityInCurResolution: 3.25PhyWidth:  72PhyHeight:  156RefreshRate:  60VirtualPixelRatio: 3.25ScreenRotation:  0Orientation:  0DisplayOrientation:  0GetScreenType: 1ReqOrientation:  0DPI:  444.5, 442.871Offset:  0, 0Bounds:  0, 0, 1260, 2720,PhyBounds:  0, 0, 1260, 2720,AvailableArea 0, 0, 1260, 2720,DefaultDeviceRotationOffset 0

执行上述命令后,解析返回内容,通过正则提取需要的信息,比如屏幕尺寸分辨率Bounds,VirtualWidth,VirtualHeight,PhyWidth,PhyHeight,ScreenRotation

查看屏幕旋转状态

$ hdc shell hidumper -s DisplayManagerService -a -a

通过上面的查看屏幕信息命令,通过正则提取ScreenRotation字段即可,ScreenRotation有四个值:

  • 0:未旋转
  • 90:顺时针旋转90度
  • 180:顺时针旋转180度
  • 270:顺时针旋转270度

备注:目前旋转状态只能查看,不支持设置

查看屏幕亮屏状态

$ hdc shell hidumper -s PowerManagerService -a -s-------------------------------[ability]-----------------------------------------------------------------PowerManagerService----------------------------------POWER STATE DUMP:Current State: AWAKE Reason: 20 Time: 521085695ScreenOffTime: Timeout=600000msDUMP DETAILS:Last Screen On: 521248337Last Screen Off: 467804783Last SuspendDevice: 0Last WakeupDevice: 464729447Last Refresh: 521248337DUMP EACH STATES:State: AWAKE Reason: POWER_KEY Time: 521085695 Failure: INIT Reason: From: AWAKE Time: 0State: FREEZE Reason: INIT Time: 0 Failure: INIT Reason: From: AWAKE Time: 0State: INACTIVE Reason: TIMEOUT Time: 467805109 Failure: INIT Reason: From: AWAKE Time: 0State: STAND_BY Reason: INIT Time: 0 Failure: INIT Reason: From: AWAKE Time: 0State: DOZE Reason: INIT Time: 0 Failure: INIT Reason: From: AWAKE Time: 0State: SLEEP Reason: TIMEOUT Time: 467810120 Failure: INIT Reason: From: AWAKE Time: 0State: HIBERNATE Reason: INIT Time: 0 Failure: INIT Reason: From: AWAKE Time: 0State: SHUTDOWN Reason: INIT Time: 0 Failure: INIT Reason: From: AWAKE Time: 0State: DIM Reason: TIMEOUT Time: 467797280 Failure: TIMEOUT Reason: Blocked by running lock From: AWAKE Time: 447180354

屏幕状态有这几种:

  • INACTIVE
  • SLEEP
  • AWAKE

点亮屏幕(唤醒)

$ hdc shell power-shell wakeupWakeupDevice is called

查看网络状态

联网状态

$ hdc shell hidumper -s NetConnManager-------------------------------[ability]-----------------------------------------------------------------NetConnManager----------------------------------Net connect Info:defaultNetSupplier_ is nullptrSupplierId:NetId: 0ConnStat: 0IsAvailable:IsRoaming: 0Strength: 0Frequency: 0LinkUpBandwidthKbps: 0LinkDownBandwidthKbps: 0Uid: 0Dns result Info:netId: 0totalReports: 2failReports: 2

wifi信息

$ hdc shell hidumper -s WifiDevice-------------------------------[ability]-----------------------------------------------------------------WifiDevice----------------------------------WiFi active state: activatedWiFi connection status: connected Connection.ssid: K-Lab Connection.bssid: cc:d0:**:**:**:e2 Connection.rssi: -45 Connection.band: 2.4GHz Connection.frequency: 2462 Connection.linkSpeed: 156 Connection.macAddress: ea:f9:**:**:**:31 Connection.isHiddenSSID: false Connection.signalLevel: 4Country Code: CN

应用管理

安装应用

$ hdc app install entry-default-signed.hap[Info]App install path:/Users/develop/entry-default-signed.hap, queuesize:0, msg:install bundle successfully.AppMod finish

或者

$ hdc install entry-default-signed.hap[Info]App install path:/Users/develop/entry-default-signed.hap, queuesize:0, msg:install bundle successfully.AppMod finish

卸载应用

$ hdc app uninstall com.kk.hmscrcpy[Info]App uninstall path:, queuesize:0, msg:uninstall bundle successfully.AppMod finish

或者

$ hdc uninstall com.kk.hmscrcpy[Info]App uninstall path:, queuesize:0, msg:uninstall bundle successfully.AppMod finish

获取应用列表

$ hdc shell bm dump -aID: 100:com.huawei.associateassistantcom.huawei.batterycarecom.huawei.hmos.AutoRegServicecom.huawei.hmos.advisorcom.huawei.hmos.advsecmodecom.huawei.hmos.aibasecom.huawei.hmos.aidataservicecom.huawei.hmos.aidispatchservicecom.huawei.hmos.ailifecom.huawei.hmos.ailifesvccom.huawei.hmos.audioaccessorymanagercom.huawei.hmos.authcredmgr ...

启动应用

通过启动Ability来拉起APP

hdc shell aa start -a {abilityName} -b {bundleName} 
  • 其中bundleName可以通过hdc shell bm dump -a获取

  • 其中abilityName可以通过如下命令获取(查看当前任务栈的ability信息)

$ hdc shell aa dump -l # 运行命令前需要手动打开appUser ID #100 current mission lists:{ Mission ID #139 mission name #[#com.kuaishou.hmapp:kwai:EntryAbility] lockedState #0 mission affinity #[] AbilityRecord ID #55 app name [com.kuaishou.hmapp] main name [EntryAbility] bundle name [com.kuaishou.hmapp] ability type [PAGE] state #FOREGROUND start time [152523] app state #FOREGROUND ready #1 window attached #0 launcher #0 callee connections: isKeepAlive: false }

里面的EntryAbility就是你要打开app的Ability名称

退出应用

强制退出应用

hdc shell aa force-stop {bundleName} 
  • 其中bundleName可以通过hdc shell bm dump -a获取

获取应用版本

$ hdc shell bm dump -n {bundleName}

执行上述命令后,再解析json, 提取versionName字段即可

Dump应用信息

aa dump

$ hdc shell aa dump -husage: aa dump options list: -h, --help  list available commands -a, --all  dump all abilities -l, --mission-list  dump mission list -i, --ability dump abilityRecordId -e, --extension  dump elementName (FA: serviceAbilityRecords,Stage: ExtensionRecords) -p, --pending dump pendingWantRecordId -r, --process dump process -d, --data  dump the data abilities -u, --userId  userId -c, --client  client -c, -u are auxiliary parameters and cannot be used alone

bm dump

$ hdc shell bm dump -husage: bm dump options list: -h, --help list available commands -a, --all list all bundles in system -n, --bundle-name  list the bundle info by a bundle name -s, --shortcut-info  list the shortcut info -d, --device-id  specify a device id -u, --user-id   specify a user id

获取应用 Ability信息

$ hdc shell aa dump -l //运行命令前需要手动打开appUser ID #100 current mission lists:{ Mission ID #139 mission name #[#com.kuaishou.hmapp:kwai:EntryAbility] lockedState #0 mission affinity #[] AbilityRecord ID #55 app name [com.kuaishou.hmapp] main name [EntryAbility] bundle name [com.kuaishou.hmapp] ability type [PAGE] state #FOREGROUND start time [152523] app state #FOREGROUND ready #1 window attached #0 launcher #0 callee connections: isKeepAlive: false }

获取应用详情

查询该应用的详细信息

$ hdc shell bm dump -n com.kuaishou.hmappcom.kuaishou.hmapp:{ \"appId\": \"com.kuaishou.hmapp_BIS88rItfUAk+V9Y4WZp2HgIZ/JeOgvEBkwgB/YyrKiwrWhje9Xn2F6Q7WKFVM22RdIR4vFsG14A7ombgQmIIxU=\", \"appIdentifier\": \"5765880207853819885\", \"appIndex\": 0, \"applicationInfo\": { ... \"applicationReservedFlag\": 0, \"arkNativeFileAbi\": \"\", \"arkNativeFilePath\": \"\", \"asanEnabled\": false, \"asanLogPath\": \"\", \"associatedWakeUp\": false, \"bundleName\": \"com.kuaishou.hmapp\", \"bundleType\": 0, \"cacheDir\": \"\", \"codePath\": \"/data/app/el1/bundle/public/com.kuaishou.hmapp\", \"compileSdkType\": \"HarmonyOS\", \"compileSdkVersion\": \"4.1.0.73\", \"cpuAbi\": \"arm64-v8a\", \"crowdtestDeadline\": -1, \"dataBaseDir\": \"/data/app/el2/database/com.kuaishou.hmapp\", \"dataDir\": \"\", \"debug\": true, \"description\": \"\", \"descriptionId\": 0, \"descriptionResource\": { \"bundleName\": \"com.kuaishou.hmapp\", \"id\": 0, \"moduleName\": \"kwai\" }, \"deviceId\": \"PHONE-001\", \"distributedNotificationEnabled\": true, \"enabled\": true, \"entityType\": \"unspecified\", \"entryDir\": \"\", \"entryModuleName\": \"\", \"fingerprint\": \"96C4B0B051421A56EC9117BC6E3CF093C428B6B6D59DA13205C29C9BDD39AE7C\", ... \"minCompatibleVersionCode\": 999999, \"moduleInfos\": [ { \"moduleName\": \"kwai\", \"moduleSourceDir\": \"\", \"preloads\": [] } ],... \"userDataClearable\": true, \"vendor\": \"快手\", \"versionCode\": 999999, \"versionName\": \"12.2.40\" }, \"compatibleVersion\": 40100011, \"cpuAbi\": \"\", \"defPermissions\": [], \"description\": \"\", \"entryInstallationFree\": false, \"entryModuleName\": \"kwai\", \"gid\": 20020014, \"hapModuleInfos\": [ ... ], \"reqPermissions\": [ \"ohos.permission.ACCELEROMETER\", \"ohos.permission.GET_NETWORK_INFO\", \"ohos.permission.GET_WIFI_INFO\", \"ohos.permission.INTERNET\", \"ohos.permission.SET_NETWORK_INFO\", \"ohos.permission.STORE_PERSISTENT_DATA\" ],... \"vendor\": \"快手\", \"versionCode\": 999999, \"versionName\": \"12.2.40\"}

通过这个命令可以获取到很多应用的关键信息,比如reqPermissionsversionabilities等等

清除应用数据

$ hdc shell bm clean -husage: bm clean options list: -h, --help list available commands -n, --bundle-name  bundle name -c, --cache  clean bundle cache files by bundle name -d, --data clean bundle data files by bundle name -u, --user-id  specify a user id

清除应用缓存

$ hdc shell bm clean -n {bundleName} -cclean bundle cache files successfully.

其中bundleName可以通过hdc shell bm dump -a获取, 比如com.kuaishou.hmapp

清除应用数据

$ hdc shell bm clean -n {bundleName} -d 

显示可调试应用列表

$ hdc jpid257126332638265826662691282533103804397730178$ hdc track-jpid0000
  • jpid显示可调试应用列表
  • track-jpid动态显示可调试应用列表。

端口转发

命令 说明 fport ls 展示全部“端口转发主机端口转发数据到设备侧端口”的转发任务 fport local remote 端口转发主机端口转发数据到设备侧端口 fport rm local remote 删除指定“端口转发主机端口转发数据到设备侧端口”的转发任务 rport ls 展示全部“端口转发设备侧端口转发数据到主机端口”的转发任务 rport local remote 端口转发设备侧端口转发数据到主机端口 rport rm local remote 删除指定“端口转发设备侧端口转发数据到主机端口”的转发任务

显示端口转发列表

展示电脑端口转发到手机端口的列表

$ hdc fport lsFMR0223C13000649 tcp:7912 tcp:7912 [Forward]

本地端口转发到手机

将本地电脑的7913端口转发到手机7912端口

$ hdc fport tcp:7913 tcp:7912Forwardport result:OK

这个命令非常实用,比如我再手机上实现了一个 http服务,没有这个命令前需要通过手机ip:port来访问,这就需要提前知道手机的wlanIP,执行这个命令后可以直接通过localhost:localPort来访问手机里的服务。

删除端口转发任务

$ hdc fport rm tcp:7913 tcp:7912Remove forward ruler success, ruler:tcp:7913 tcp:7912$ hdc fport ls[Empty]

同理,rport命令表示手机端口转发到电脑端口,我就不一一举例了.

无线调试

  1. 在手机上开启5555端口 hdc -t {SERIAL} tmode port {PORT}
  2. 连接手机上的端口 hdc -t {SERIAL} tconn {WLANIP}:{PORT}
  3. 恢复手机USB连接 hdc -t {SERIAL} tmode usb

示例

$ hdc tmode port 5555$ hdc tconn 172.31.124.84:5555Connect OK$ hdc list targets172.31.124.84:5555$ hdc tmode usb Set device run mode successful.

不过目前这个无线调试,会导致该手机USB连接方式断开,导致无法进行端口转发,每次进行无线调试时,需要知道手机的wlanip才行。
这个问题也在和鸿蒙方沟通,待解决。
记个TODO.

文件传输

命令 说明 file send local remote 从本地发送文件至远端设备 file recv remote local 从远端设备发送文件至本地

从本地电脑发送文件至手机

$ hdc file send ~/layout_407568854.json /data/local/tmp/layout_407568854.jsonFileTransfer finish, Size:71792, File count = 1, time:24ms rate:2991.33kB/s

从手机拷贝文件至本地电脑

$ hdc file recv /data/local/tmp/layout_407568854.json ~/layout_407568854.json[I][2024-05-28 20:15:45] HdcFile::TransferSummary successFileTransfer finish, Size:71792, File count = 1, time:12ms rate:5982.67kB/s

UI模拟操作(点击滑动等)

支持操作类型:点击 双击 长按 慢滑 快滑 拖拽 输入文字 KeyEvent

配置参数名 配置参数含义 配置参数取值 示例 click 模拟单击操作 point_x (必选参数,点击x坐标点)
point_y (必选参数,点击y坐标点) hdc shell uitest uiInput click point_x point_y doubleClick 模拟双击操作 point_x (必选参数,双击x坐标点)
point_y (必选参数,双击y坐标点) hdc shell uitest uiInput doubleClick point_x point_y longClick 模拟长按操作 point_x (必选参数,长按x坐标点)
point_y (必选参数,长按y坐标点) hdc shell uitest uiInput longClick point_x point_y fling 模拟快滑操作 from_x (必选参数,滑动起点x坐标)
from_y(必选参数,滑动起点y坐标)
to_x(必选参数,滑动终点x坐标)
to_y(必选参数,滑动终点y坐标)
swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s)
stepLength(可选参数,滑动步长,默认值:滑动距离/50, 单位: px) hdc shell uitest uiInput fling from_x from_y to_x to_y swipeVelocityPps_ stepLength swipe 模拟慢滑操作 from_x (必选参数,滑动起点x坐标)
from_y(必选参数,滑动起点y坐标)
to_x(必选参数,滑动终点x坐标)
to_y(必选参数,滑动终点y坐标)
swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s) hdc shell uitest uiInput swipe from_x from_y to_x to_y swipeVelocityPps_ drag 模拟拖拽操作 from_x (必选参数,拖拽起点x坐标)
from_y(必选参数,拖拽起点y坐标)
to_x(必选参数,拖拽终点x坐标)
to_y(必选参数,拖拽终点y坐标)
swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s) hdc shell uitest uiInput drag from_x from_y to_x to_y swipeVelocityPps_ dircFling 模拟指定方向滑动操作 direction (可选参数,滑动方向,可选值: [0,1,2,3], 滑动方向: [左,右,上,下],默认值: 0)
swipeVelocityPps_ (可选参数,滑动速度,取值范围: 200-40000, 默认值: 600, 单位: px/s)
stepLength(可选参数,滑动步长,默认值:滑动距离/50, 单位: px) hdc shell uitest uiInput dircFling direction swipeVelocityPps_ stepLength inputText 模拟输入框输入文本操作 point_x (必选参数,输入框x坐标点)
point_y (必选参数,输入框y坐标点)
input(输入文本) hdc shell uitest uiInput inputText point_x point_y text keyEvent 模拟实体按键事件(如:键盘,电源键,返回上一级,返回桌面等),以及组合按键操作 keyID (必选参数,实体按键对应ID)
keyID2 (可选参数,实体按键对应ID) hdc shell uitest uiInput keyEvent keyID

举例

//点击hdc shell uitest uiInput click 100 100//双击hdc shell uitest uiInput doubleClick 100 100//长按hdc shell uitest uiInput longClick 100 100//快滑hdc shell uitest uiInput fling 10 10 200 200 500//慢滑hdc shell uitest uiInput swipe 10 10 200 200 500//拖拽hdc shell uitest uiInput drag 10 10 100 100 500//左滑hdc shell uitest uiInput dircFling 0 500//右滑hdc shell uitest uiInput dircFling 1 600//上滑hdc shell uitest uiInput dircFling 2//下滑hdc shell uitest uiInput dircFling 3//输入框输入hdc shell uitest uiInput inputText 100 100 hello//返回主页hdc shell uitest uiInput keyEvent Home//返回上一步hdc shell uitest uiInput keyEvent Back//组合键粘贴操作hdc shell uitest uiInput keyEvent 2072 2038

keyEvent映射表可以参考这个文档:https://docs.openharmony.cn/pages/v4.1/en/application-dev/reference/apis-input-kit/js-apis-keycode.md

屏幕截图

hdc提供了两种截图命令

方式一

$ hdc shell uitest screenCap// 默认存储路径:/data/local/tmp,文件名:时间戳 + .png。$ hdc shell uitest screenCap -p /data/local/tmp/1.png// 指定存储路径和文件名。

【推荐】方式二

$ hdc shell snapshot_display -f /data/local/tmp/2.jpeg// 截图完成后可以通过 hdc file recv 命令导入到本地

方式二的截图性能效率远远高于方式一

屏幕录屏

相关hdc命令还未支持,官方在开发中。。。

我这边通过python脚本实现了录屏功能,使用方法如下

cd awesome-hdc/scriptspip3 install -r requirements.txtpython3 screen_recroding.py

打开Scheme (URL)

$ hdc shell aa start -U http://www.baidu.comstart ability successfully.$ hdc shell aa start -U kwai://home

获取页面布局信息(控件树)

$ hdc shell uitest dumpLayout -p {saveDumpPath} # 运行命令前需要手动打开app,进入对应页面DumpLayout saved to:/data/local/tmp/layout_407568854.json
  • -p表示控件树保存的目录,如果不指定,则默认保存在手机的/data/local/tmp目录
    /data/local/tmp/layout_407568854.json文件内容如下:
{ \"attributes\": { \"accessibilityId\": \"\", \"bounds\": \"[0,0][1260,2720]\", \"checkable\": \"\", \"checked\": \"\", \"clickable\": \"\", \"description\": \"\", \"enabled\": \"\", \"focused\": \"\", \"hostWindowId\": \"\", \"id\": \"\", \"key\": \"\", \"longClickable\": \"\", \"origBounds\": \"\", \"scrollable\": \"\", \"selected\": \"\", \"text\": \"\", \"type\": \"\" }, \"children\": [ ... ]

录制用户操作

将当前界面操作记录到/data/local/tmp/layout/record.csv,结束录制操作使用Ctrl+C结束录制

$ hdc shell uitest uiRecord recordwindowBounds : (0,0,1260,2720)Current ForAbility :com.kuaishou.hmapp, EntryAbilityThe result will be written in csv file at location: /data/local/tmp/layout/record.csvStarted Recording Successfully...click , fingerNumber:1 ,finger1:click: at Point(x:557, y:1542) ; from Point(x:557, y:1542) to Point(x:557, y:1542) ;click , fingerNumber:1 ,finger1:click: at Point(x:550, y:1638) ; from Point(x:550, y:1638) to Point(x:550, y:1638) ;fling , fingerNumber:1 ,finger1:from Point(x:409, y:1916) to Point(x:370, y:1528) ; Off-hand speed:1415.42, Step length:34;fling , fingerNumber:1 ,finger1:from Point(x:400, y:1886) to Point(x:389, y:1586) ; Off-hand speed:1995.97, Step length:31;home , fingerNumber:1 ,finger1:from Widget(id: , type: Text, text: state: didAppear, feedId: 5218827670987295481, feedType: 3, ServerExpTag: feed_photo|5218827670987295481|1499501607|1_u/2003373606202106162_bs54357, ) ; to Point(x:615, y:2338) ;

支持两种方式查看数据:

  • uiRecord record, 将事件的位置坐标写入文件
  • uiRecord read, 将文件内容打印到控制台

录制完成后,再将csv文件拷贝到电脑上

$ hdc file recv /data/local/tmp/layout/record.csv ~/record.csv

record数据字段含义请参考如下示例数据

{ \"ABILITY\": \"com.ohos.launcher.MainAbility\", // 前台应用界面 \"BUNDLE\": \"com.ohos.launcher\", // 操作应用 \"CENTER_X\": \"\", // 模拟捏合中心X, pinch事件 \"CENTER_Y\": \"\", // 模拟捏合中心Y, pinch事件 \"EVENT_TYPE\": \"pointer\", // \"LENGTH\": \"0\", // 总体步长 \"OP_TYPE\": \"click\", //事件类型,当前支持点击、双击、长按、拖拽、捏合、滑动、抛滑动作录制 \"VELO\": \"0.000000\", // 离手速度 \"direction.X\": \"0.000000\",// 总体移动X方向 \"direction.Y\": \"0.000000\", // 总体移动Y方向 \"duration\": 33885000.0, // 手势操作持续时间 \"fingerList\": [{ \"LENGTH\": \"0\", // 总体步长 \"MAX_VEL\": \"40000\", // 最大速度 \"VELO\": \"0.000000\", // 离手速度 \"W1_BOUNDS\": \"{\"bottom\":361,\"left\":37,\"right\":118,\"top\":280}\", // 起点控件bounds \"W1_HIER\": \"ROOT,3,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0\", // 起点控件hierarchy \"W1_ID\": \"\", // 起点控件id \"W1_Text\": \"\", // 起点控件text \"W1_Type\": \"Image\", // 起点控件类型 \"W2_BOUNDS\": \"{\"bottom\":361,\"left\":37,\"right\":118,\"top\":280}\", // 终点控件bounds \"W2_HIER\": \"ROOT,3,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0\", // 终点控件hierarchy \"W2_ID\": \"\", // 终点控件id \"W2_Text\": \"\", // 终点控件text \"W2_Type\": \"Image\", // 终点控件类型 \"X2_POSI\": \"47\", // 终点X \"X_POSI\": \"47\", // 起点X \"Y2_POSI\": \"301\", // 终点Y \"Y_POSI\": \"301\", // 起点Y \"direction.X\": \"0.000000\", // x方向移动量 \"direction.Y\": \"0.000000\" // Y方向移动量 }], \"fingerNumber\": \"1\" //手指数量}

系统日志(log)

$ hdc hilog -hUsage:-h --help Show all help information. Show single help information with option: query/clear/buffer/stats/persist/private/kmsg/flowcontrol/baselevel/domain/comboQuerying logs options: No option performs a blocking read and keeps printing. -x --exit Performs a non-blocking read and exits when all logs in buffer are printed. -a , --head= Show n lines logs on head of buffer. -z , --tail= Show n lines logs on tail of buffer. -t , --type= Show specific type/types logs with format: type1,type2,type3 Don\'t show specific type/types logs with format: ^type1,type2,type3 Type coule be: app/core/init/kmsg, kmsg can\'t combine with others. Default types are: app,core,init. -L , --level= Show specific level/levels logs with format: level1,level2,level3 Don\'t show specific level/levels logs with format: ^level1,level2,level3 Long and short level string are both accepted Long level string coule be: DEBUG/INFO/WARN/ERROR/FATAL. Short level string coule be: D/I/W/E/F. Default levels are all levels. -D , --domain= Show specific domain/domains logs with format: domain1,domain2,doman3 Don\'t show specific domain/domains logs with format: ^domain1,domain2,doman3 Max domain count is 5. See domain description at the end of this message. -T , --tag= Show specific tag/tags logs with format: tag1,tag2,tag3 Don\'t show specific tag/tags logs with format: ^tag1,tag2,tag3 Max tag count is 10. -P , --pid= Show specific pid/pids logs with format: pid1,pid2,pid3 Don\'t show specific domain/domains logs with format: ^pid1,pid2,pid3 Max pid count is 5. -e , --regex= Show the logs which match the regular expression . -v , --format= Show logs in different formats, options are: color or colour display colorful logs by log level.i.e. DEBUG INFO WARN ERROR FATAL time format options are(single accepted): time display local time, this is default. epoch display the time from 1970/1/1. monotonic display the cpu time from bootup. time accuracy format options are(single accepted): msec display time by millisecond, this is default. usec display time by microsecond. nsec display time by nanosecond. year display the year when -v time is specified. zone display the time zone when -v time is specified. Different types of formats can be combined, such as: -v color -v time -v msec -v year -v zone.-r Remove all logs in hilogd buffer, advanced option: -t , --type= Remove specific type/types logs in buffer with format: type1,type2,type3 Type coule be: app/core/init/kmsg. Default types are: app,core-g Query hilogd buffer size, advanced option: -t , --type= Query specific type/types buffer size with format: type1,type2,type3 Type coule be: app/core/init/kmsg. Default types are: app,core-G , --buffer-size= Set hilogd buffer size,  could be number or number with unit. Unit could be: B/K/M/G which represents Byte/Kilobyte/Megabyte/Gigabyte.  range: [64.0K,64.0K]. Advanced option: -t , --type= Set specific type/types log buffer size with format: type1,type2,type3 Type coule be: app/core/init/kmsg. Default types are: app,core **It\'s a persistant configuration**-s, --statistics Query log statistic information. Set param persist.sys.hilog.stats true to enable statistic. Set param persist.sys.hilog.stats.tag true to enable statistic of log tag.-S Clear hilogd statistic information.-w ,--write= Log persistance task control, options are: query query tasks informations stop stop all tasks start start one task clear clear /data/log/hilog/hilog*.gz Persistance task is used for saving logs in files. The files are saved in directory: /data/log/hilog/ Advanced options: -f , --filename= Set log file name, name should be valid of Linux FS. -l , --length= Set single log file size.  could be number or number with unit. Unit could be: B/K/M/G which represents Byte/Kilobyte/Megabyte/Gigabyte.  range: [64.0K, 512.0M]. -n , --number Set max log file numbers, log file rotate when files count over this number.  range: [2, 1000]. -m ,--stream= Set log file compressed algorithm, options are: none write file with non-compressed logs. zlib write file with zlib compressed logs. -j , --jobid Start/stop specific task of .  range: [10, 0xffffffff). User can start task with options (t/L/D/T/P/e/v) as if using them when \"Query logs\" too. **It\'s a persistant configuration**-p , --privacy  Set HILOG api privacy formatter feature on or off. **It\'s a temporary configuration, will be lost after reboot**-k , --kmsg  Set hilogd storing kmsg log feature on or off **It\'s a persistant configuration**-Q  Set log flow-control feature on or off, options are: pidon process flow control on pidoff process flow control off domainon domain flow control on domainoff domain flow control off **It\'s a temporary configuration, will be lost after reboot**-b , --baselevel= Set global loggable level to  Long and short level string are both accepted. Long level string coule be: DEBUG/INFO/WARN/ERROR/FATAL/X. Short level string coule be: D/I/W/E/F/X. X means that loggable level is higher than the max level, no log could be printed. Advanced options: -D , --domain= Set specific domain loggable level. See domain description at the end of this message. -T , --tag= Set specific tag loggable level. The priority is: tag level > domain level > global level. **It\'s a temporary configuration, will be lost after reboot**The first layer options can\'t be used in combination, ILLEGAL expamples: hilog -S -s; hilog -w start -r; hilog -p on -k on -b DDomain description: Log type \"core\" & \"init\" are used for OS subsystems, the range is [0xd000000, 0xd0fffff] Log type \"app\" is used for applications, the range is [0x0, 0xffff] To reduce redundant info when printing logs, only last five hex numbers of domain are printed So if user wants to use -D option to filter OS logs, user should add 0xD0 as prefix to the printed domain: Exapmle: hilog -D 0xD0xxxxx The xxxxx is the domain string printed in logs.Dictionary description:-d , --dictionary= Set elf file path, name should be valid of Linux FS. Rescan the elf file in the system to generate a full data dictionary file

导出日志

$ hdc file recv data/log/hilog/ ./

导出crash日志

hdc file recv data/log/faultlog/faultlogger/ ./

hidumper工具

$ hdc shell hidumper -husage: -h |help text for the tool -lc |a list of system information clusters -ls |a list of system abilities -c |all system information clusters -c [base system] |system information clusters labeled \"base\" and \"system\" -s |all system abilities -s [SA0 SA1] |system abilities labeled \"SA0\" and \"SA1\" -s [SA] -a [\'-h\']  |system ability labeled \"SA\" with arguments \"-h\" specified -e |faultlogs of crash history --net [pid]  |dump network information; if pid is specified, dump traffic usage of specified pid --storage [pid] |dump storage information; if pid is specified, dump /proc/pid/io -p |processes information, include list and information of processes and threads -p [pid]  |dump threads under pid, includes smap, block channel, execute time, mountinfo --cpuusage [pid] |dump cpu usage by processes and category; if PID is specified, dump category usage of specified pid --cpufreq  |dump real CPU frequency of each core --mem [pid]  |dump memory usage of total; dump memory usage of specified pid if pid was specified --zip |compress output to /data/log/hidumper --mem-smaps pid [-v] |display statistic in /proc/pid/smaps, use -v specify more details --mem-jsheap pid [-T tid] [--gc] |triggerGC and dumpHeapSnapshot under pid and tid

list system abilities

$ hdc shell hidumper -lsSystem ability list:SystemAbilityManager RenderService  AbilityManagerService DataObserverMgr  UriPermissionMgr  AccountMgr BundleMgr FormMgr ApplicationManagerService AccessibilityManagerService UserIdmService  UserAuthService  AuthExecutorMgrService  PinAuthService  FaceAuthService  FingerprintAuthService  WifiDevice WifiHotspotWifiP2p WifiScan 1125 1126 NetConnManager  NetPolicyManager  NetStatsManager  NetTetheringManager  VPNManager EthernetManager  NetsysNative  NetsysExtService  DistributedNet  1181 HiviewService  HiviewFaultLogger HiviewSysEventService 1204 XperfTraceService HiDumperService  XpowerManager  HiDumperCpuService  DistributedKvData ContinuationManagerService ResourceSched  BackgroundTaskManager WorkSchedule  ComponentSchedServer SocPerfService  DeviceUsageStatistics MemoryManagerService SuspendManager  AbnormalEfficiencyManager ConcurrentTaskService ResourceQuotaControl DeviceStandbyService TaskHeartbeatMgrService 2901 DeviceStatusService  2903 2904 2908 AudioDistributed  PlayerDistributedService CameraService  AudioPolicyService  AVSessionService  AVCodecService  MediaKeySystemService MultimodalInput  DistributedNotificationService CommonEventService  PowerManagerService  BatteryService  ThermalService  BatteryStatisticsService DisplayPowerManagerService AccessTokenManagerService PrivacyManagerService KeystoreService  DeviceThreatDetectionService RiskAnalysisManagerService DataCollectManagerService DlpCreService  SensorService  MiscDeviceService PasteboardService TimeServiceInputMethodService  ScreenlockService WallpaperManagerService ParamWatcher  TelephonyCallManager TelephonyCellularCall TelephonyCellularData TelephonySmsMms  TelephonyStateRegistry  TelephonyCoreService 4011 TelephonyIms  ModuleUpdateService  UsbService WindowManagerService DisplayManagerService DSoftbus DeviceAuthService DeviceManagerService StorageDaemon  StorageManager  HdfDeviceServiceManager CloudFileDaemonService  EcologicalRuleManager UiService UiAppearanceService  CaDaemon AssetService  9527 65537 65570 65728 65777 65830 65850 65888 65904 65926 65958 65962 66070 66090 70633 

获取到abilities后,就可以指定service获取相关的信息。 比如通过RenderService获取一些信息

$ hdc shell hidumper -s RenderService -------------------------------[ability]-----------------------------------------------------------------RenderService---------------------------------------Graphic2D--RenderSerice ------Usage: h |help text for the toolscreen |dump all screen infomation in the systemsurface |dump all surface informationcomposer fps  |dump the fps info of composer[surface name] fps |dump the fps info of surfacecomposer fpsClear  |clear the fps info of composer[windowname] fps  |dump the fps info of window[windowname] hitchs |dump the hitchs info of window[surface name] fpsClear |clear the fps info of surfacenodeNotOnTree  |dump nodeNotOnTree infoallSurfacesMem  |dump surface mem infoRSTree |dump RSTree infoEventParamList  |dump EventParamList infoallInfo |dump all infodumpMem |dump CachetrimMem cpu/gpu/shader |release Cachesurfacenode [id]  |dump node infofpsCount |dump the refresh rate counts infoclearFpsCount  |clear the refresh rate counts info

RenderService

获取分辩率

$ hdc shell hidumper -s RenderService -a screen -------------------------------[ability]-----------------------------------------------------------------RenderService----------------------------------- ScreenInfoscreen[0]: id=0, powerstatus=POWER_STATUS_OFF, backlight=21, screenType=EXTERNAL_TYPE, render size: 1260x2720, physical screen resolution: 1260x2720, isvirtual=false, skipFrameInterval_:1 supportedMode[0]: 1260x2720, refreshrate=120 supportedMode[1]: 1260x2720, refreshrate=90 supportedMode[2]: 1260x2720, refreshrate=60 supportedMode[3]: 1260x2720, refreshrate=30 activeMode: 1260x2720, refreshrate=60 capability: name=, phywidth=72, phyheight=156,supportlayers=12, virtualDispCount=0, propCount=0, type=DISP_INTF_HDMI, supportWriteBack=false

获取帧率
首先执行如下命令进入到shell环境

$ hdc shell

然后执行hidumper [surface name] fps , 例如composer fps

$ hidumper -s RenderService -a \"composer fps\"-------------------------------[ability]-----------------------------------------------------------------RenderService----------------------------------- The recently fps records info of screens:The fps of screen [Id:0] is:107537646652857107537663200253107537679747128107537696352336107537712846086107537729390357107537745974211107537762468482107537779015357107537795561190107537812110148107537828651815107537845349732...

DisplayManagerService

$ hdc shell hidumper -s DisplayManagerService-------------------------------[ability]-----------------------------------------------------------------DisplayManagerService----------------------------------Usage: -h |help text for the tool -a |dump all screen information in the system -z |switch to fold half status -y |switch to expand status -p |switch to fold status -f |get to fold status

PowerManagerService

$ hdc shell hidumper -s PowerManagerService-------------------------------[ability]-----------------------------------------------------------------PowerManagerService----------------------------------Power manager dump options: [-h] [-runninglock] description of the cmd option: -a: show dump info of all power modules. -h: show this help. -r: show the information of runninglock. -s: show the information of power state machine. -d: show power off dialog.

例如上文提到的获取屏幕状态

$ hdc shell hidumper -s PowerManagerService -a -s

BatteryService

$ hdc shell hidumper -s BatteryService-------------------------------[ability]-----------------------------------------------------------------BatteryService----------------------------------Usage: -h: dump help -i: dump battery info -u: unplug battery charging state -r: reset battery state --capacity : set battery capacity, the capacity range [0, 100] --uevent : set battery uevent

例如上文提到的获取电量温度

$ hdc shell hidumper -s BatteryService -a -i 

NetConnManager

$ hdc shell hidumper -s NetConnManager-------------------------------[ability]-----------------------------------------------------------------NetConnManager----------------------------------Net connect Info:defaultNetSupplier_ is nullptrSupplierId:NetId: 0ConnStat: 0IsAvailable:IsRoaming: 0Strength: 0Frequency: 0LinkUpBandwidthKbps: 0LinkDownBandwidthKbps: 0Uid: 0Dns result Info:netId: 0totalReports: 2failReports: 2

StorageManager

aa工具

Ability assistant(Ability助手,简称为aa),是实现应用及测试用例启动功能的工具,为开发者提供基本的应用调试和测试能力,例如启动应用组件、强制停止进程、打印应用组件相关信息等。

$ hdc shell aa helpusage: aa  These are common aa commands list: help list available commands start start ability with options stop-service stop service with options dump dump the ability info force-stop  force stop the process with bundle name attachattach application to enter debug mdoe detachdetach application to exit debug mode test start the test framework with options appdebug  set / cancel / get waiting debug status

start

stop-service

force-stop

test

attach

detach

appdebug

详细介绍请参考文档:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/tools/aa-tool.md

bm工具

Bundle Manager(包管理工具,简称bm)是实现应用安装、卸载、更新、查询等功能的工具,bm为开发者提供基本的应用安装包的调试能力,例如:安装应用,卸载应用,查询安装包信息等。

$ hdc shell bm helpusage: bm  These are common bm commands list: help list available commands install install a bundle with options uninstall uninstall a bundle with options dump dump the bundle info get obtain device udid quickfix quick fix, including query and install compile Compile the software package dump-overlay dump overlay info of the specific overlay bundle dump-target-overlay dump overlay info of the specific target bundle dump-dependencies dump dependencies by given bundle name and module name dump-shared dump inter-application shared library information by bundle name clean clean the bundle data

install

uninstall

dump

clean

enable

disable

get

详细介绍请参考文档:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/tools/bm-tool.md

param工具

param是为开发人员提供用于操作系统参数的工具,该工具只支持标准系统。

$ hdc shell param Command list: param ls [-r] [name] --display system parameter param get [name] --get system parameter param set name value --set system parameter param wait name [value] [timeout]  --wait system parameter param dump [verbose] --dump system parameter param shell [-p] [name] [-u] [username] [-g] [groupname] --shell system parameter param save 

详细介绍请参考文档:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/tools/param-tool.md

Instrument Test

主要用来做APP 的UI自动化测试,将应用测试包安装到测试设备上,在cmd窗口中执行aa命令,完成对用例测试。

aa test命令执行配置参数

执行参数全写 执行参数缩写 执行参数含义 执行参数示例 –bundleName -b 应用 Bundle 名称 -b com.test.example –packageName -p 应用模块名,适用于 FA 模型应用 -p com.test.example.entry –moduleName -m 应用模块名,适用于 STAGE 模型应用 -m entry NA -s 特定参数,以 键值对方式传入 -s unittest /ets/testrunner/OpenHarmonyTestRunner
$ hdc shell aa test -husage: aa test options list: -h, --help list available commands -b  -s unittest  start the test framework with options  [-p ]  the name of package with test-runner, required for the FA model  [-m ]  the name of module with test-runner, required for the STAGE model  [-s class ]  [-s level ]  [-s size ]  [-s testType ]  [-s timeout ]  [-s  ]  [-w ]  [-D]

举例

$ hdc shell aa test -b com.example.myapplication -m entry_test -s unittest /ets/testrunner/OpenHarmonyTestRunner -s class UiTestDemo timeout 15000

查看测试结果
cmd模式执行过程,会打印如下相关日志信息。

OHOS_REPORT_STATUS: class=testStopOHOS_REPORT_STATUS: current=1OHOS_REPORT_STATUS: id=JSOHOS_REPORT_STATUS: numtests=447OHOS_REPORT_STATUS: stream=OHOS_REPORT_STATUS: test=stop_0OHOS_REPORT_STATUS_CODE: 1OHOS_REPORT_STATUS: class=testStopOHOS_REPORT_STATUS: current=1OHOS_REPORT_STATUS: id=JSOHOS_REPORT_STATUS: numtests=447OHOS_REPORT_STATUS: stream=OHOS_REPORT_STATUS: test=stop_0OHOS_REPORT_STATUS_CODE: 0OHOS_REPORT_STATUS: consuming=4OHOS_REPORT_RESULT: stream=Tests run: 447, Failure: 0, Error: 1, Pass: 201, Ignore: 245OHOS_REPORT_CODE: 0OHOS_REPORT_RESULT: breakOnError model, Stopping whole test suite if one specific test case failed or errorOHOS_REPORT_STATUS: taskconsuming=16029

性能工具

SmartPerf是一款基于系统开发的性能功耗测试工具,操作简单易用。工具可以检测性能、功耗相关指标,包括FPSCPUGPURAMTemp等,通过量化的指标项了解应用性能状况。在开发过程中,使用的可能是有屏或无屏设备,对此SmartPerf提供了两种方式:分别是SmartPerf-DeviceSmartPerf-DaemonSmartPerf-Device适用于有屏设备,支持可视化操作。测试时是通过悬浮窗的开始和暂停来实时展示性能指标数据,保存后可生成数据报告,在报告中可分析各指标数据详情。SmartPerf-Daemon支持shell命令行方式,同时适用于有屏和无屏设备。

  • CPU:每秒读取一次设备节点下CPU大中小核的频点和各核使用率,衡量应用占用CPU资源的情况,占用过多的CPU资源会导致芯片发烫。
  • GPU:每秒读取一次设备节点下GPU的频点和负载信息,衡量应用占用GPU资源的情况,当GPU占用过多时,会导致性能下降,应用程序的运行速度变慢。
  • FPS:应用界面每秒刷新次数,衡量应用画面的流畅度,FPS越高表示图像流畅度越好,用户体验也越好。
  • POWER:每秒读取一次设备节点下的电流及电压信息。
  • TEMP:每秒读取一次设备节点下电池温度、系统芯片温度等信息。
  • RAM:每秒读取一次应用进程的实际物理内存,衡量应用的内存占比情况。
  • snapshot:每秒截取一张应用界面截图。
$ hdc shell# SP_daemon// 查看daemon进程是否存在# ps -ef | grep SP_daemonroot 1584 1 0 21:50:05 ? 00:00:00 SP_daemonroot 1595 1574 3 21:51:02 pts/0 00:00:00 grep SP_daemon

执行查看帮助命令

# SP_daemon --helpOpenHarmony performance testing tool SmartPerf command-line versionUsage: SP_daemon [options] [arguments]options: -N set the collection times(default value is 0) range[1,2147483647], for example: -N 10 -PKG  set package name, must add, for example: -PKG ohos.samples.ecg -c get device CPU frequency and CPU usage, process CPU usage and CPU load .. -g get device GPU frequency and GPU load -f get app refresh fps(frames per second) and fps jitters and refreshrate -profilerfps get refresh fps and timestamp -sections set collection time period(using with profilerfps) -t get remaining battery power and temperature.. -p get battery power consumption and voltage -r get process memory and total memory -snapshot get screen capture -net  get uplink and downlink traffic -start collection start command -stop collection stop command -VIEW set layler, for example: -VIEW DisplayNode -screen get screen resolution -OUT  set csv output path. -d get device DDR information -m get other memoryexample:SP_daemon -N 20 -c -g -t -p -r -m -net -snapshot -dSP_daemon -N 20 -PKG ohos.samples.ecg -c -g -t -p -f -r -m -net -snapshot -dSP_daemon -start -cSP_daemon -stopSP_daemon -screen

基于hdc命令行的SmartPerf性能工具使用详细文档参考这个:https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-test/smartperf-guidelines.md

参考链接

  • https://gitee.com/openharmony/developtools_hdc
  • https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/ide-command-line-hdc-0000001237908229-V2#section116322265308
  • https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/application-test/smartperf-guidelines.md
  • https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/tools/aa-tool.md
  • https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/tools/bm-tool.md
  • https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/tools/param-tool.md
  • https://github.com/mzlogin/awesome-adb

陇西