Android Monkey 的环境配置及实践练习

一、java环境

二、Android SDK 下载及安装

1. SDK Tools  下载

2. 解压并双击SDK.Manager

 

3.下面的截图 显示installed的 是我本地安装的Android 10(安装需要一些时间,建议在网络状况良好的情况下进行)

 

如果安装过程中出现失败或者安装不上,单独去下载所需插件,放入指定位置即可,参考链接:https://www.androiddevtools.cn/

 4.安装完成后,将android-sdk 的路径添加到环境变量里边,并保存,如下:

 5.检查android SDK是否安装成功

方法1:adb

方法2:android

 

三、monkey的使用

1. Monkey 官网地址:

英文版   https://developer.android.google.cn/studio/test/monkey?hl=en

中文版  https://developer.android.google.cn/studio/test/monkey?hl=zh_cn

2. 什么是monkey?

Monkey 是Android SDK提供的一个命令行工具,可以在任何模拟器实例或设备上运行。它会将伪随机用户事件流发送到系统中,从而对您正在开发的应用软件进行压力测试。

Monkey 包含许多选项,主要分为以下四个类别:

  • 基本配置选项,例如设置要尝试的事件数。
  • 操作限制条件,例如将测试对象限制为单个软件包。
  • 事件类型和频率。
  • 调试选项。
3. Monkey的基本用法:

以下演示使用mumu模拟器,如下,列出已连接的设备( https://mumu.163.com/help/func/20190129/30131_797867.html)

进入adb shell 环境, 习惯使用方式2

4.monkey  命令选项参考信息 --来源于官网
C:\Users\86183>adb shell monkey -help
usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]
              [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]
              [--ignore-crashes] [--ignore-timeouts]
              [--ignore-security-exceptions]
              [--monitor-native-crashes] [--ignore-native-crashes]
              [--kill-process-after-error] [--hprof]
              [--pct-touch PERCENT] [--pct-motion PERCENT]
              [--pct-trackball PERCENT] [--pct-syskeys PERCENT]
              [--pct-nav PERCENT] [--pct-majornav PERCENT]
              [--pct-appswitch PERCENT] [--pct-flip PERCENT]
              [--pct-anyevent PERCENT] [--pct-pinchzoom PERCENT]
              [--pct-permission PERCENT]
              [--pkg-blacklist-file PACKAGE_BLACKLIST_FILE]
              [--pkg-whitelist-file PACKAGE_WHITELIST_FILE]
              [--wait-dbg] [--dbg-no-events]
              [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]
              [--port port]
              [-s SEED] [-v [-v] ...]
              [--throttle MILLISEC] [--randomize-throttle]
              [--profile-wait MILLISEC]
              [--device-sleep-time MILLISEC]
              [--randomize-script]
              [--script-log]
              [--bugreport]
              [--periodic-bugreport]
              [--permission-target-system]
              COUNT


下面以头条(com.ss.android.article.news)为例,进行常用参数练习

-p 指定被测应用的包名    count  必填

例:adb shell monkey -p com.ss.android.article.news 2000   
(-p 指定被测应用的包名  如果想要指定多个包名,每一个包名要跟一个-p 如:adb shell monkey -p packageName1 -p packageName2 100 )
以上命令的作用:它会启动您的应用-头条并向其发送 2000 个伪随机事件,此处count=2000,count必填,2000是事件计数
C:\Users\86183>adb shell monkey -p com.ss.android.article.news 2000
    // activityResuming(com.ss.android.article.news)
    // activityResuming(com.ss.android.article.news)
    // activityResuming(com.ss.android.article.news)
    // Injection Failed
    // activityResuming(com.ss.android.article.news)
    // activityResuming(com.ss.android.article.news)
    // activityResuming(com.ss.android.article.news)
// CRASH: com.ss.android.article.news (pid 12804)
// Short Msg: Native crash
// Long Msg: Native crash: Segmentation fault
// Build Label: OnePlus/OnePlus2/OnePlus2:6.0.1/MMB29M/1447841200:user/release-keys
// Build Changelist: eng.root.20191227.103137
// Build Time: 1577442784000
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Build fingerprint: 'OnePlus/OnePlus2/OnePlus2:6.0.1/MMB29M/1447841200:user/release-keys'
// Revision: '0'
// ABI: 'x86'
// pid: 12804, tid: 13859, name: Thread-591  >>> com.ss.android.article.news <<<
// signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xdead0000
//     eax 00000000  ebx b2e0cff4  ecx b7792600  edx b7792600
//     esi 00000008  edi 00000001
//     xcs 00000073  xds 0000007b  xes 0000007b  xfs 0000003b  xss 0000007b
//     eip b2c4860e  ebp 103c2040  esp 81d99be4  flags 00010282
//
// backtrace:
//     #00 pc 0024360e  /system/lib/arm/libhoudini.so
//     #01 pc 0001453c  <unknown>
//
** Monkey aborted due to error.
Events injected: 1081
## Network stats: elapsed time=23908ms (0ms mobile, 0ms wifi, 23908ms not connected)
** System appears to have crashed at event 1081 of 2000 using seed 1639345785450

C:\Users\86183>adb shell monkey -p com.ss.android.article.news 2000
    // activityResuming(com.ss.android.article.news)
    // activityResuming(com.ss.android.article.news)
    // activityResuming(com.ss.android.article.news)
    // Injection Failed
    // Injection Failed
    // activityResuming(com.ss.android.article.news)
    // activityResuming(com.ss.android.article.news)
Events injected: 2000
## Network stats: elapsed time=21868ms (0ms mobile, 0ms wifi, 21868ms not connected)

C:\Users\86183>adb shell monkey -p com.ss.android.article.news 2000
    // activityResuming(com.ss.android.article.news)
// CRASH: com.ss.android.article.news (pid 14526)
// Short Msg: Native crash
// Long Msg: Native crash: Segmentation fault
// Build Label: OnePlus/OnePlus2/OnePlus2:6.0.1/MMB29M/1447841200:user/release-keys
// Build Changelist: eng.root.20191227.103137
// Build Time: 1577442784000
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
// Build fingerprint: 'OnePlus/OnePlus2/OnePlus2:6.0.1/MMB29M/1447841200:user/release-keys'
// Revision: '0'
// ABI: 'x86'
// pid: 14526, tid: 15011, name: Thread-598  >>> com.ss.android.article.news <<<
// signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xdead0000
//     eax 00000000  ebx b2e0cff4  ecx b7792600  edx b7792600
//     esi 00000008  edi 00000001
//     xcs 00000073  xds 0000007b  xes 0000007b  xfs 0000003b  xss 0000007b
//     eip b2c4860e  ebp 103c2040  esp 840c3be4  flags 00010282
//
// backtrace:
//     #00 pc 0024360e  /system/lib/arm/libhoudini.so
//     #01 pc 0001453c  <unknown>
//
** Monkey aborted due to error.
Events injected: 746
## Network stats: elapsed time=20936ms (0ms mobile, 0ms wifi, 20936ms not connected)
** System appears to have crashed at event 746 of 2000 using seed 1639345851148

 -v  日志级别,默认一个v, 最多3个v

  日志级别 Level 0,缺省值,仅提供启动提示、测试完成和最终结果等少量信息

  日志级别 Level 1,提供较为详细的日志,包括每个发送到Activity的事件信息

  日志级别 Level 2,最详细的日志,包括了测试中选中/未选中的Activity信息

例:adb shell monkey -p com.ss.android.article.news -v -v -v 10 

C:\Users\86183>adb shell monkey -p com.ss.android.article.news -v -v -v 10
:Monkey: seed=1639347263800 count=10
:AllowPackage: com.ss.android.article.news
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Selecting main activities from category android.intent.category.LAUNCHER
//   - NOT USING main activity com.android.settings.Settings (from package com.android.settings)
//   - NOT USING main activity com.android.browser.BrowserActivity (from package com.android.browser)
//   - NOT USING main activity com.cyanogenmod.filemanager.activities.NavigationActivity (from package com.cyanogenmod.filemanager)
//   - NOT USING main activity com.android.camera.CameraLauncher (from package com.android.camera2)
//   - NOT USING main activity com.android.gallery3d.app.GalleryActivity (from package com.android.gallery3d)
//   - NOT USING main activity com.android.providers.downloads.ui.DownloadList (from package com.android.providers.downloads.ui)
//   - NOT USING main activity com.android.quicksearchbox.SearchActivity (from package com.android.quicksearchbox)
//   - NOT USING main activity io.kkzs.activity.MainActivity (from package io.kkzs)
//   - NOT USING main activity com.netease.mumu.cloner.ClonerActivity (from package com.netease.mumu.cloner)
//   - NOT USING main activity com.mumu.store.MainActivity (from package com.mumu.store)
//   + Using main activity com.ss.android.article.news.activity.SplashBadgeActivity (from package com.ss.android.article.news)
//   - NOT USING main activity com.baidu.searchbox.SplashActivity (from package com.baidu.searchbox)
//   - NOT USING main activity com.netease.open.pocoservice.TestActivity (from package com.netease.open.pocoservice)
//   - NOT USING main activity com.netease.nie.yosemite.MainActivity (from package com.netease.nie.yosemite)
//   - NOT USING main activity com.ss.android.ugc.aweme.splash.SplashActivity (from package com.ss.android.ugc.aweme)
//   - NOT USING main activity com.netease.buff.entry.SplashActivity (from package com.netease.buff)
//   - NOT USING main activity com.tencent.mobileqq.activity.SplashActivity (from package com.tencent.mobileqq)
//   - NOT USING main activity com.tencent.mm.ui.LauncherUI (from package com.tencent.mm)
// Selecting main activities from category android.intent.category.MONKEY
//   - NOT USING main activity com.android.settings.Settings$RunningServicesActivity (from package com.android.settings)
//   - NOT USING main activity com.android.settings.Settings$StorageUseActivity (from package com.android.settings)
//   - NOT USING main activity com.mumu.launcher.Launcher (from package com.mumu.launcher)
// Seeded: 1639347263800
// Event percentages:
//   0: 15.0%
//   1: 10.0%
//   2: 2.0%
//   3: 15.0%
//   4: -0.0%
//   5: -0.0%
//   6: 25.0%
//   7: 15.0%
//   8: 2.0%
//   9: 2.0%
//   10: 1.0%
//   11: 13.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.ss.android.article.news/.activity.SplashBadgeActivity;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.ss.android.article.news/.activity.SplashBadgeActivity } in package com.ss.android.article.news
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 23    // KEYCODE_DPAD_CENTER
    // Allowing start of Intent { cmp=com.ss.android.article.news/.activity.MainActivity } in package com.ss.android.article.news
    // activityResuming(com.ss.android.article.news)
:Sending Key (ACTION_UP): 23    // KEYCODE_DPAD_CENTER
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(432.0,955.0)
:Sending Touch (ACTION_MOVE): 0:(446.12125,956.8809)
:Sending Touch (ACTION_MOVE): 0:(466.95187,957.866)
:Sending Touch (ACTION_MOVE): 0:(489.2918,960.4463)
:Sending Touch (ACTION_UP): 0:(493.4249,965.02905)
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(-4.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-1.0,-5.0)
Events injected: 10
:Sending rotation degree=0, persist=false
:Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=950ms (0ms mobile, 0ms wifi, 950ms not connected)
// Monkey finished

-s 设置种子数

相当于设置一个路径,因为monkey是随机事件,所以为了使回归路径一致就需要设置相同的seed值
例:adb shell monkey -p com.ss.android.article.news -v -v -s 2  10  如果想重现上面例子的路径下次执行的-s也必须为2

adb shell monkey -p com.ss.android.article.news -v -v  -s 2 10

--throttle 设置每次随机事件的时间间隔(单位:毫秒)

adb shell monkey  --throttle 500  200

--throttle time --randomize-throttle 设置随机时间的时间间隔区间

例:adb shell monkey -p com.ss.android.article.news  --throttle 500 --randomize-throttle 200

说明:执行一百次monkey随机事件,每次事件的间隔在0到500毫秒之间不固定

--ignore-crashes 运行中忽略crash,遇到crash依然把后面的事件跑完

例:adb shell monkey -p com.ss.android.article.news  --ignore-crashes -v 200

--ignore-timeouts  运行中忽略ANR,遇到ANR依然把后面的事件跑完

ANR  Application Not Responding 

   ANR是指在Android上,应用程序响应不够灵敏时,系统会向用户显示的一个对话框。

  ANR 对话框会为用户提供强行退出应用的选项。

例:adb shell monkey -p com.ss.android.article.news  --ignore-timeouts -v 200

将控制台的日志输出到文件里边

例:adb shell monkey -p com.ss.android.article.news  --ignore-timeouts -v 200  >d:\monkey.txt

设置事件百分比,所有的百分比加起来不能超过100%

 Event percentages://各种事件的百分比,不同厂家设备可能有所不同

0: 15.0% //可通过--pct-touch 参数设置的事件的百分比 常用
1: 10.0% //可通过--pct-motion 参数设置的事件的百分比 常用
2: 2.0% //可通过--pct-pinchzoom 参数设置的事件的百分比
3: 15.0% //可通过--pct-trackball 参数设置的事件的百分比
4: -0.0%
5: -0.0%
6: 25.0% //可通过--pct-nav 参数设置的事件的百分比
7: 15.0% //可通过--pct-majornav 参数设置的事件的百分比
8: 2.0% //可通过--pct-syskeys 参数设置的事件的百分比 常用
9: 2.0% //可通过--pct-appswitch 参数设置的事件的百分比 常用
10: 1.0% //可通过--pct-flip 参数设置的事件的百分比
11: 13.0% //可通过--pct-anyevent 参数设置的事件的百分比

--ignore-native-crashes   忽略monkey本身的异常,直到事件执行完毕

例:adb shell monkey -p com.ss.android.article.news  --ignore-timeouts --ignore-native-crashes -v 200


以上就是本次分享的内容,稍后继续针对mokey 日志进行分析,找出异常或者crashes ,具体定位问题并提交bug.

原文地址:https://www.cnblogs.com/eosclover/p/15672722.html