1# wukong稳定性工具使用指导
2
3## 功能介绍
4
5wukong是系统自带的一种命令行工具,支持Ability的随机事件注入、控件注入、异常捕获、报告生成和对Ability数据遍历截图等特性。通过模拟用户行为,对系统或应用进行稳定性压力测试。  
6wukong分为随机测试和专项测试。随机测试是指随机测试界面内容,支持的能力包括:shell启动、拉起整机应用、多种注入方式、设置随机种子、打印运行日志和生成报告。专项测试主要提供对指定应用控件进行测试,支持的能力包括:shell启动、顺序遍历及截图、测试休眠睡醒、录制回放、打印运行日志和生成报告。
7
8## 实现原理
9
10wukong部件架构图以及部件内子模块职责如下所述。  
11![Alternate text](figures/wukongRandomTestFlow.png)
12
13- 命令行解析:支持命令行获取参数并解析命令行参数。
14- 运行环境管理:根据命令行初始化wukong整体运行环境。
15- 系统接口管理:检查并获取指定的mgr,注册controller和dfx的faultlog的回调函数。
16- 随机事件生成:通过random函数生成指定种子数的随机序列,生成事件。
17- 事件注入:根据支持的事件类型向系统注入事件,依赖窗口、多模、安全等子系统。
18- 异常捕获处理/报告生成:通过DFX子系统获取运行中的异常信息并记录log,生成报告。
19
20## 约束与限制
21
221. wukong测试工具在API 9版本开始预置使用。
232. 在低于API 9版本,不能随版本编译,使用时需自行编译后推送至被检测设备,具体步骤如下。
24
25   ```bash
26   // 构建方式
27   ./build.sh --product-name rk3568 --build-target wukong
28
29   // 推送方式
30   hdc shell mount -o rw,remount /
31   hdc file send wukong /
32   hdc shell chmod a+x /wukong
33   hdc shell mv /wukong /bin/
34   ```
35
363. PC被检测设备连接后,才可执行命令行,支持单个和多个设备。
37
38## 功能特性及命令说明
39
40| 命令           | 说明                                           | 备注          |
41| -------------- | ---------------------------------------------- | ------------- |
42| version | 获取wukong版本信息。                             | -v, --version。 |
43| help    | 获取wukong帮助信息。                             |              |
44| appinfo | 查询支持拉起应用bundleName和对应的mainAbility名。 |               |
45| special | wukong专项测试。                                   |               |
46| exec    | wukong随机测试。                                   |               |
47| focus   | wukong专注测试。                                   |               |
48
49## 随机测试
50
51### wukong随机测试命令参数
52
53| 命令            | 功能                                 | 必选 | 备注                                     |
54| --------------- | ------------------------------------ | ---- | ---------------------------------------- |
55| -h,--help       | 获取当前测试的帮助信息。               | 否   |                          |
56| -c,--count      | 设置执行次数,与设置执行时间-T冲突。二者取其一。   | 否   | 单位次数,默认10次。                       |
57| -i,--interval   | 设置执行间隔。                         | 否   | 单位ms,默认1500ms。                       |
58| -s,--seed       | 设置随机种子。                         | 否   | 配置相同随机种子,会生成相同随机事件序列。 |
59| -b,--bundle[bundlename,……,bundlename]     | 设置本次测试的允许应用名单,与-p冲突。 | 否   | 默认测试当前设备所有应用(应用名称用英文逗号隔开)。                 |
60| -p,--prohibit[bundlename,……,bundlename]   | 设置本次测试的禁止应用名单,与-b冲突。 | 否   | 默认不禁止任何应用(应用名称用英文逗号隔开)。                       |
61| -d,--page[page,……,page]                   | 设置本次测试的禁止页面名单。 | 否  | 系统默认禁止pages/system页面(页面名称用逗号隔开)。 |
62| -a,--appswitch  | 设置应用随机拉起测试比例。             | 否   | 默认10%。                                  |
63| -t,--touch      | 设置屏幕随机touch测试比例。            | 否   | 默认10%。                                  |
64| -S,--swap       | 设置屏幕随机swap测试比例。             | 否   | 默认3%。                                   |
65| -m,--mouse      | 设置屏幕随机mouse测试比例。            | 否   | 默认1%。                                   |
66| -k,--keyboard   | 设置屏幕随机keyboard测试比例。         | 否   | 默认2%。                                   |
67| -H,--hardkey    | 设置随机hardkey测试比例。              | 否   | 默认2%。                                   |
68| -r,--rotate     | 设置随机rotate测试比例。               | 否   | 默认2%。                                   |
69| -C, --component | 设置随机控件测试比例。                 | 否   | 默认70%。                                  |
70| -I, --screenshot | 控件测试截图。                 | 否   | - |
71| -T,--time       | 设置测试总时间,与设置执行次数-c冲突。二者取其一。 | 否   | 单位分钟,默认10分钟。         |
72| -e, --allow ability   |  设置压测ability白名单 | 否 | - |
73| -E, --block ability   |  设置压测ability白名单 | 否 | - |
74| -Y, --blockCompId     |  设置不进行注入的CompId | 否 | - |
75| -y, --blockCompType   |  设置不进行注入的CompType | 否 | - |
76| -B, --checkBWScreen   |  设置启用黑白屏检测 | 否 | - |
77
78## 专项测试
79
80### wukong专项测试命令参数
81
82| 命令                | 功能                   | 必选 | 备注                |
83| :------------------ | ---------------------- | ---- | :------------------ |
84| -h, --help          | 获取当前专项测试的帮助信息。 | 否   |      |
85| -k, --spec_insomnia | 休眠唤醒专项测试。       | 否   | -                   |
86| -c, --count         | 设置执行次数。           | 否   | 单位次数,默认10次。          |
87| -i, --interval      | 设置执行间隔。           | 否   | 单位ms,默认1500ms。  |
88| -S, --swap          | 滑动测试。               | 否   | -                   |
89| -s, --start[x,y]    | 设置滑动测试起点坐标。   | 否   | 坐标均为正值。           |
90| -e, --end[x,y]      | 设置滑动测试终点坐标。   | 否   | 坐标均为正值。          |
91| -b, --bilateral     | 设置往返滑动。           | 否   | 默认不往返滑动。      |
92| -t, --touch[x,y]    | 点击测试。               | 否   | -                   |
93| -T, --time          | 设置测试总时间。         | 否   | 单位分钟,默认10分钟。 |
94| -C, --component     | 控件顺序遍历测试。       | 否   | 需要设置测试应用名称。 |
95| -r, --record     | 录制。       | 否   | 需要指定录制文件。 |
96| -R, --replay    |  回放。      | 否   | 需要指定回放文件。 |
97| -p, --screenshot    |  控件测试截图。      | 否   | - |
98
99## 专注测试
100
101### wukong专注测试命令参数
102
103| 命令            | 功能                                 | 必选 | 备注                                     |
104| --------------- | ------------------------------------ | ---- | ---------------------------------------- |
105| -n,--numberfocus       | 设置每个控件注入的次数。               | 否   | 单位次数。                 |
106| -f, --focustypes       | 设置需要专注的控件类型。               | 否   | 以英文逗号隔开。                         |
107其余参数继承自随机测试命令参数。
108
109## 命令行使用示例
110
111> 以下操作需要先进入shell模式
112
113```bash
114#若为单设备,则直接输入如下命令进入shell模式
115C:\Users>hdc shell
116#
117
118#若为多设备,则需先获取sn号,先输入hdc list targets获取sn号,然后进入shell模式
119C:\Users>hdc list targets
12015xxx424axxxx345209d94xxxx8fxx900
121C:\Users>hdc -t 15xxx424axxxx345209d94xxxx8fxx900 shell
122#
123```
124
125### 1.获取应用的bundle name和ability name
126
127```bash
128# wukong appinfo
129BundleName:  com.ohos.adminprovisioning
130AbilityName:  com.ohos.adminprovisioning.MainAbility
131BundleName:  com.ohos.callui
132AbilityName:  com.ohos.callui.MainAbility
133```
134
135### 2.执行随机测试命令
136
137```bash
138# wukong exec -s 10 -i 1000 -a 0.28 -t 0.72 -c 100
139```
140
141命令中各参数含义:
142| 命令           | 参数值      |说明        |
143| -------------- | -------------- | -------------- |
144| wukong exec | -           | 主命令。                |
145| -s     | 10           | 参数设置随机种子,10为种子值。  |
146| -i  | 1000           | 参数设置应用拉起间隔为1000ms。 |
147| -a  | 0.28          | 参数设置应用随机拉起测试比例28%。    |
148| -t  | 0.72           | 参数设置屏幕随机touch测试比例为72%。    |
149| -c  | 100           | 参数设置执行次数为100次。         |
150
151### 3.执行专项测试命令
152
153```bash
154# wukong special -C [bundlename] -p
155```
156
157 命令中各参数含义:
158 | 命令           | 参数值           | 说明                                           |
159 | -------------- |-------------- | ---------------------------------------------- |
160 | wukong special | -  | 主命令。                             |
161 | -C [bundlename]    |[bundlename] | 控件顺序遍历测试参数设置,bundlename为测试应用名称。            |
162 | -p | p  | 表示截图。 |
163
164### 4.执行查看帮助命令
165
166```bash
167C:\Users>hdc shell
168# wukong --help        //wukong帮助菜单
169wukong: '--help' is not a valid wukong command. See 'wukong help'.
170usage: wukong <command> [<arguments>]
171These are common wukong command list:
172   help                       wukong help information
173   -v/--version               wukong version
174   exec                       run random test
175   special                    run special test
176   focus                      run focus test
177   appinfo                    show all app information
178# wukong exec -help   //wukong随机测试帮助菜单
179usage: wukong exec [<arguments>]
180These are wukong exec arguments list:
181   -h, --help                 random test help
182   -a, --appswitch            appswitch event percent
183   -b, --bundle               the bundle name of allowlist
184   -p, --prohibit             the bundle name of blocklist
185   -d, --page                 block page list
186   -t, --touch                touch event percent
187   -c, --count                test count
188   -i, --interval             interval
189   -s, --seed                 random seed
190   -m, --mouse                mouse event percent
191   -k, --keyboard             keyboard event percent
192   -H, --hardkey              hardkey event percent
193   -S, --swap                 swap event percent
194   -T, --time                 test time
195   -C, --component            component event percent
196   -r, --rotate               rotate event percent
197   -e, --allow ability        the ability name of allowlist
198   -E, --block ability        the ability name of blocklist
199   -I, --screenshot           get screenshot(only in random input)
200# wukong special -help    //wukong专项测试帮助菜单
201usage: wukong special [<arguments>]
202These are wukong special arguments list:
203   -h, --help                 special test help
204   -t, --touch[x,y]           touch event
205   -c, --count                total count of test
206   -i, --interval             interval
207   -S, --swap[option]         swap event
208                              option is -s| -e| -b
209                              -s, --start: the start point of swap
210                              -e, --end: the end point of swap
211                              -b, --bilateral: swap go and back
212   -k, --spec_insomnia        power on/off event
213   -T, --time                 total time of test
214   -C, --component            component event
215   -p, --screenshot           get screenshot(only in componment input)
216   -r, --record               record user operation
217   -R, --replay               replay user operation
218   -u, --uitest               uitest dumpLayout
219```
220
221## 查看测试结果
222
223### 测试结果输出路径
224
225执行完测试指令后,会自动生成测试结果,测试结果输出根路径如下:
226
227- 2022/9/22之前的IDE版本,结果存放路径为:/data/local/wukong/report/xxxxxxxx_xxxxxx/
228- 2022/9/22之后的IDE版本,结果存放路径为:/data/local/tmp/wukong/report/xxxxxxxx_xxxxxx/
229
230### 测试报告文件目录
231
232| 类型                                 | 描述               |
233| ------------------------------------ | ------------------ |
234| exception/                           | 存放本次测试产生的异常文件。 |
235| screenshot/                          | 存放测试遍历的截图。  |
236| wukong_report.csv                    | 测试报告统计汇总。       |
237| wukong.log                | 测试操作历程。       |
238
239### 查看操作日志
240
241wukong支持通过hdc命令将日志获取到本地,查看操作历程。
242
243```bash
244// wukong.log文件对应路径如下
245/data/local/tmp/wukong/report/xxxxxxxx_xxxxxx/wukong.log
246
247// 查看wukong测试报告文件目录操作如下
248# cd /data/local/tmp/wukong/report/20170805_170053
249# ls
250data.js  exception  wukong.log  wukong_report.csv
251
252// 开启shell窗口,用hdc file recv获取wukong日志
253C:\Users\xxx>hdc file recv /data/local/tmp/wukong/report/20170805_170053/wukong.log C:\Users\xxx\Desktop\log
254[I][2024-01-03 20:08:02] HdcFile::TransferSummary success
255FileTransfer finish, Size:76492, File count = 1, time:16ms rate:4780.75kB/s
256```
257