Android monkey 命令详解

本文详细解读了Androidmonkey命令的各种选项,包括事件类型的比例控制、操作约束、事件间隔、调试模式等,帮助开发者理解和优化应用的自动化测试过程。
摘要由CSDN通过智能技术生成

monkey 命令

| Category | Option | Description |

| — | — | — |

| General | --help | Prints a simple usage guide. |

| -v | Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing. |

| Events | -s <seed> | Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events. |

| --throttle <milliseconds> | Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible. |

| --pct-touch <percent> | Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.) |

| --pct-motion <percent> | Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.) |

| --pct-trackball <percent> | Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.) |

| --pct-nav <percent> | Adjust percentage of “basic” navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.) |

| --pct-majornav <percent> | Adjust percentage of “major” navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.) |

| --pct-syskeys <percent> | Adjust percentage of “system” key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.) |

| --pct-appswitch <percent> | Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package. |

| --pct-anyevent <percent> | Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth. |

| Constraints | -p <allowed-package-name> | If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you’ll need to specify those packages as well. If you don’t specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package. |

| -c <main-category> | If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don’t specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category. |

| Debugging | --dbg-no-events | When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application. |

| --hprof | If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files. |

| --ignore-crashes | Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |

| --ignore-timeouts | Normally, the Monkey will stop when the application experiences any type of timeout error such as a “Application Not Responding” dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |

| --ignore-security-exceptions | Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed. |

| --kill-process-after-error | Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event. |

| --monitor-native-crashes | Watches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop. |

| --wait-dbg | Stops the Monkey from executing until a debugger is attached to it. |

这个表是google 官网中介绍monkey命令给出的,google 对monkey命令分了四类:

1. 基本配置选项,如设置事件的数量、查看帮助信息等。

2. 操作的约束,如通过包名限制哪些应用可以被测试。

3. 事件的类型和频率,如点击事件占比多少、触摸事件又占多少以及事件之间的间隔时间等。

4. 调试选项,如是否忽略crashes、ANR等。

下面以测试小例子的形式对上面的命令进行详细讲解:

adb shell monkey 或者 adb shell monkey -help

查看help得到的monkey选项命令比google官网中的要多一些,主要是一些更高级的命令,如通过脚本文件进行monkey测试等。

adb shell monkey -p your.package.name 500

作用:-p 为约束命令,作用是约束只对某个应用进行测试,your.package.name是你要进行测试的应用包名,如果要对多个应用进行测试可以使用多个-p

例:adb shell monkey -p com.android.settings 500 对系统设置应用进行monkey测试发送500个随机事件

adb shell monkey -p com.android.settings -p com.android.calculator2 500 对系统设置应用和计算器应用进行monkey测试共发送500个随机事件

注:如果不使用-p约束,如 adb shell monkey 500 那么将会对手机中的所有应用进行随机测试共发送500个随机事件

-v

adb shell monkey -v 500

作用:命令行中的每个-v都将增加测试信息的详细级别

Level 0 (默认)提供了很少的信息除了启动通知,测试完成,和最终结果。-v

Level 1 提供了更详细的测试运行,如个别事件被发送到您的Activity。 -v -v

Level 2 提供了更详细的设置等信息如Activity选中或未选中的测试信息。 -v -v -v

根据您对测试结果要求的详细程度来确定您用几个 -v,一般会用-v -v -v 最详细的输出到指定文件中,方便查找bug的原因。

-s

adb shell monkey -s 8888 -v -v -v 500

作用:伪随机数生成器的种子值,如果用相同的种子值再次运行monkey,将生成相同的事件序列。(该种子值对于Bug复现至关重要)

注:如果不指定种子值,系统会随机生成一个种子值,在出现Bug时该种子值会和Bug信息一起被输出,这也是为了便于复现该Bug

–throttle

adb shell monkey --throttle 300 -v 500

作用:在事件之间插入特定的延时时间(单位毫秒),这样做可以延缓monkey执行事件的速度,默认没有延时,monkey会以最快速度将指定的事件个数执行完。

注:建议使用该参数,经验值300,这是模拟人操作的速度

–pct-touch

adb shell monkey --pct-touch 50 -v -v 500

作用:指定touch(触摸)事件的百分比,touch事件是由一个DOWN和一个UP组成,按下并抬起即是一个touch事件

注:若不指定任何事件的百分比,系统将随机分配各种事件的百分比

–pct-motion

adb shell monkey --pct-motion 100 -v -v 500

作用:指定motion(手势)事件百分比,motion事件是由屏幕上某处一个down事件、一系列伪随机的移动事件和一个up事件组成

注:移动事件是直线移动

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

由于文章篇幅原因,我只把面试题列了出来,详细的答案,我整理成了一份PDF文档,这份文档还包括了还有 高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 ,帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

最后

由于文章篇幅原因,我只把面试题列了出来,详细的答案,我整理成了一份PDF文档,这份文档还包括了还有 高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 ,帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习。

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
  • 25
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值