Monkey测试

Posted zhengna

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Monkey测试相关的知识,希望对你有一定的参考价值。

一 monkey简介

1.参考资料

http://blog.csdn.net/jlminghui/article/details/38238443

2.Monkey介绍

顾名思义,Monkey就是猴子,Monkey测试,就像一只猴子,在电脑面前,乱敲键盘在测试。猴子什么都不懂,只知道乱敲。Monkey会发送伪随机的事件流,通过Monkey程序模拟用户触摸屏幕、滑动Trackball、按键等操作来对设备上的程序进行压力测试,检测程序多久的时间会发生异常。

3.Monkey用来做什么

Monkey 主要用于android 的压力测试自动的一个压力测试小工具, 主要目的就是为了测试app是否会Crash(崩溃)。

4.Monkey程序介绍

(1)Monkey程序由Android系统自带,使用Java语言写成,在Android文件系统中的存放路径是: /system/framework/monkey.jar;
(2)Monkey.jar程序是由一个名为“monkey”的Shell脚本来启动执行,shell脚本在Android文件系统中的存放路径是:/system/bin/monkey;
(3)Monkey 命令启动方式:
a)可以通过PC机CMD窗口中执行: adb shell monkey {+命令参数}来进行Monkey测试
b)在PC上adb shell 进入Android系统,通过执行 monkey {+命令参数} 来进行Monkey测试
c)在Android机或者模拟器上直接执行monkey 命令,可以在Android机上安装Android终端模拟器

二 Monkey命令基本参数介绍

1.使用monkey测试手机所有程序
adb shell monkey 100
执行monkey测试100次
这种操作就是在手机上随便点,点击100次。对测试应用没什么用处,如果你进入到一些大公司比如华为测试手机ROM可能会用到。

技术分享图片

使用adb指令,安装Login.apk

技术分享图片

2.查看手机上应用包名
adb shell pm list packages

技术分享图片

3.-p <允许的包名列表>
用此参数指定一个或多个包。指定包之后,monkey将只允许系统启动指定的app。如果不指定包, monkey将允许系统启动设备中的所有app。
指定一个包:

技术分享图片

4.显示日志详细程度
用于指定反馈信息级别(信息级别就是日志的详细程度),总共分3个级别,分别对应的参数如下所示:
Level 0 : adb shell monkey -p <软件包名> -v 100 //缺省值,仅提供启动提示、测试完成和最终结果等少量信息
Level 1 : adb shell monkey -p <软件包名> -v -v 100 //提供较为详细的日志,包括每个发送到Activity的事件信息
Level 2 : adb shell monkey -p <软件包名> -v -v -v 100 //最详细的日志,包括了测试中选中/未选中的Activity信息

技术分享图片
C:UsersAdministrator>adb shell monkey -p com.example.login -v 100
:Monkey: seed=1541108337471 count=100
:AllowPackage: com.example.login
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Event percentages:
//   0: 15.0%
//   1: 10.0%
//   2: 2.0%
//   3: 15.0%
//   4: -0.0%
//   5: 25.0%
//   6: 15.0%
//   7: 2.0%
//   8: 2.0%
//   9: 1.0%
//   10: 13.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.example.login/.MainActivity;en
d
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.example.login/.MainActivity } in package com.exa
mple.login
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.example.login/.MainActivity;en
d
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.example.login/.MainActivity } in package com.exa
mple.login
:Sending Touch (ACTION_DOWN): 0:(137.0,218.0)
:Sending Touch (ACTION_UP): 0:(173.40945,226.30446)
:Sending Touch (ACTION_DOWN): 0:(180.0,259.0)
:Sending Touch (ACTION_UP): 0:(189.61348,257.59262)
:Sending Trackball (ACTION_MOVE): 0:(3.0,-2.0)
:Sending Trackball (ACTION_MOVE): 0:(1.0,-5.0)
:Sending Touch (ACTION_DOWN): 0:(88.0,281.0)
:Sending Touch (ACTION_UP): 0:(112.34274,320.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,1.0)
:Sending Trackball (ACTION_UP): 0:(0.0,0.0)
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.example.login/.MainActivity;en
d
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.example.login/.MainActivity } in package com.exa
mple.login
:Sending Trackball (ACTION_MOVE): 0:(3.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,-3.0)
:Sending Touch (ACTION_DOWN): 0:(39.0,248.0)
:Sending Touch (ACTION_UP): 0:(51.867878,247.03575)
:Sending Touch (ACTION_DOWN): 0:(118.0,288.0)
Events injected: 100
:Sending rotation degree=0, persist=false
:Dropped: keys=20 pointers=77 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=510ms (510ms mobile, 0ms wifi, 0ms not connected)

// Monkey finished

C:UsersAdministrator>adb shell monkey -p com.example.login -v -v 100
:Monkey: seed=1541108102892 count=100
:AllowPackage: com.example.login
:IncludeCategory: android.intent.category.LAUNCHER
:IncludeCategory: android.intent.category.MONKEY
// Selecting main activities from category android.intent.category.LAUNCHER
//   + Using main activity com.example.login.MainActivity (from package com.exam
ple.login)
// Selecting main activities from category android.intent.category.MONKEY
// Seeded: 1541108102892
// Event percentages:
//   0: 15.0%
//   1: 10.0%
//   2: 2.0%
//   3: 15.0%
//   4: -0.0%
//   5: 25.0%
//   6: 15.0%
//   7: 2.0%
//   8: 2.0%
//   9: 1.0%
//   10: 13.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.example.login/.MainActivity;en
d
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.example.login/.MainActivity } in package com.exa
mple.login
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 199    // KEYCODE_BUTTON_12
:Sending Key (ACTION_UP): 199    // KEYCODE_BUTTON_12
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 23    // KEYCODE_DPAD_CENTER
:Sending Key (ACTION_UP): 23    // KEYCODE_DPAD_CENTER
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 90    // KEYCODE_MEDIA_FAST_FORWARD
:Sending Key (ACTION_UP): 90    // KEYCODE_MEDIA_FAST_FORWARD
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 139    // KEYCODE_F9
:Sending Key (ACTION_UP): 139    // KEYCODE_F9
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(228.0,109.0)
:Sending Touch (ACTION_UP): 0:(239.7635,108.96301)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 93    // KEYCODE_PAGE_DOWN
:Sending Key (ACTION_UP): 93    // KEYCODE_PAGE_DOWN
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 20    // KEYCODE_DPAD_DOWN
:Sending Key (ACTION_UP): 20    // KEYCODE_DPAD_DOWN
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 33    // KEYCODE_E
:Sending Key (ACTION_UP): 33    // KEYCODE_E
Sleeping for 0 milliseconds
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.example.login/.MainActivity;en
d
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.example.login/.MainActivity } in package com.exa
mple.login
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 20    // KEYCODE_DPAD_DOWN
:Sending Key (ACTION_UP): 20    // KEYCODE_DPAD_DOWN
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 143    // KEYCODE_NUM_LOCK
:Sending Key (ACTION_UP): 143    // KEYCODE_NUM_LOCK
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 166    // KEYCODE_CHANNEL_UP
:Sending Key (ACTION_UP): 166    // KEYCODE_CHANNEL_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU
:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 25    // KEYCODE_VOLUME_DOWN
:Sending Key (ACTION_UP): 25    // KEYCODE_VOLUME_DOWN
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 190    // KEYCODE_BUTTON_3
:Sending Key (ACTION_UP): 190    // KEYCODE_BUTTON_3
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(-2.0,-1.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(3.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(1.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,3.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,0.0)
:Sending Trackball (ACTION_MOVE): 0:(-2.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,1.0)
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 187    // KEYCODE_APP_SWITCH
:Sending Key (ACTION_UP): 187    // KEYCODE_APP_SWITCH
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU
:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 155    // KEYCODE_NUMPAD_MULTIPLY
:Sending Key (ACTION_UP): 155    // KEYCODE_NUMPAD_MULTIPLY
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 82    // KEYCODE_MENU
:Sending Key (ACTION_UP): 82    // KEYCODE_MENU
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(210.0,1.0)
:Sending Touch (ACTION_UP): 0:(206.1193,5.3746395)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(202.0,72.0)
:Sending Touch (ACTION_UP): 0:(203.49069,55.111717)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(213.0,169.0)
:Sending Touch (ACTION_POINTER_DOWN 1): 0:(213.03842,169.64987) 1:(90.0,273.0)
:Sending Touch (ACTION_MOVE): 0:(224.27519,172.04413) 1:(85.0046,253.13188)
:Sending Touch (ACTION_MOVE): 0:(232.99971,179.71663) 1:(79.35529,246.14488)
:Sending Touch (ACTION_MOVE): 0:(240.0,185.72884) 1:(73.46896,229.39772)
:Sending Touch (ACTION_MOVE): 0:(240.0,199.92346) 1:(70.65656,218.28358)
:Sending Touch (ACTION_MOVE): 0:(240.0,203.7676) 1:(70.50711,214.87372)
:Sending Touch (ACTION_MOVE): 0:(240.0,204.10182) 1:(65.11935,199.15561)
:Sending Touch (ACTION_MOVE): 0:(240.0,218.79683) 1:(60.31317,189.60109)
:Sending Touch (ACTION_MOVE): 0:(240.0,220.99147) 1:(56.169495,187.63142)
:Sending Touch (ACTION_MOVE): 0:(240.0,235.9418) 1:(50.057125,180.71246)
:Sending Touch (ACTION_POINTER_UP 1): 0:(240.0,236.40236) 1:(44.15694,173.95424)

:Sending Touch (ACTION_UP): 0:(240.0,255.14737)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(115.0,305.0)
:Sending Touch (ACTION_UP): 0:(113.861176,304.03662)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 21    // KEYCODE_DPAD_LEFT
:Sending Key (ACTION_UP): 21    // KEYCODE_DPAD_LEFT
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 171    // KEYCODE_WINDOW
:Sending Key (ACTION_UP): 171    // KEYCODE_WINDOW
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(1.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,-4.0)
Events injected: 100
:Sending rotation degree=0, persist=false
:Dropped: keys=60 pointers=38 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=792ms (792ms mobile, 0ms wifi, 0ms not connected)

// Monkey finished

C:UsersAdministrator>adb shell monkey -p com.example.login -v -v -v 100
:Monkey: seed=1541107230859 count=100
:AllowPackage: com.example.login
: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.calculator2.Calculator (from package
com.android.calculator2)
//   - NOT USING main activity com.android.mms.ui.ConversationList (from package
 com.android.mms)
//   - NOT USING main activity com.android.browser.BrowserActivity (from package
 com.android.browser)
//   - NOT USING main activity com.android.deskclock.DeskClock (from package com
.android.deskclock)
//   - NOT USING main activity com.android.camera.GalleryPicker (from package co
m.android.gallery)
//   - NOT USING main activity com.android.music.MusicBrowserActivity (from pack
age com.android.music)
//   - NOT USING main activity com.android.calendar.AllInOneActivity (from packa
ge com.android.calendar)
//   - NOT USING main activity com.android.settings.Settings (from package com.a
ndroid.settings)
//   - NOT USING main activity com.android.camera.Camera (from package com.andro
id.camera)
//   - NOT USING main activity com.android.contacts.activities.DialtactsActivity
 (from package com.android.contacts)
//   - NOT USING main activity com.android.contacts.activities.PeopleActivity (f
rom package com.android.contacts)
//   - NOT USING main activity com.android.widgetpreview.WidgetPreviewActivity (
from package com.android.widgetpreview)
//   - NOT USING main activity com.android.gesture.builder.GestureBuilderActivit
y (from package com.android.gesture.builder)
//   - NOT USING main activity com.example.android.apis.ApiDemos (from package c
om.example.android.apis)
//   - NOT USING main activity com.android.customlocale2.CustomLocaleActivity (f
rom package com.android.customlocale2)
//   - NOT USING main activity com.android.development.Development (from package
 com.android.development)
//   - NOT USING main activity com.android.quicksearchbox.SearchActivity (from p
ackage com.android.quicksearchbox)
//   - NOT USING main activity com.android.speechrecorder.SpeechRecorderActivity
 (from package com.android.speechrecorder)
//   - NOT USING main activity com.android.providers.downloads.ui.DownloadList (
from package com.android.providers.downloads.ui)
//   - NOT USING main activity android.app.AliasActivity (from package com.andro
id.development_settings)
//   + Using main activity com.example.login.MainActivity (from package com.exam
ple.login)
//   - NOT USING main activity com.example.test.MainActivity (from package com.e
xample.test)
// Selecting main activities from category android.intent.category.MONKEY
//   - NOT USING main activity com.android.launcher2.Launcher (from package com.
android.launcher)
//   - NOT USING main activity com.android.settings.Settings$RunningServicesActi
vity (from package com.android.settings)
//   - NOT USING main activity com.android.settings.Settings$StorageUseActivity
(from package com.android.settings)
// Seeded: 1541107230859
// Event percentages:
//   0: 15.0%
//   1: 10.0%
//   2: 2.0%
//   3: 15.0%
//   4: -0.0%
//   5: 25.0%
//   6: 15.0%
//   7: 2.0%
//   8: 2.0%
//   9: 1.0%
//   10: 13.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.categ
ory.LAUNCHER;launchFlags=0x10200000;component=com.example.login/.MainActivity;en
d
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.in
tent.category.LAUNCHER] cmp=com.example.login/.MainActivity } in package com.exa
mple.login
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 24    // KEYCODE_VOLUME_UP
:Sending Key (ACTION_UP): 24    // KEYCODE_VOLUME_UP
Sleeping for 0 milliseconds
:Sending Flip keyboardOpen=false
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 23    // KEYCODE_DPAD_CENTER
:Sending Key (ACTION_UP): 23    // KEYCODE_DPAD_CENTER
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(159.0,217.0)
:Sending Touch (ACTION_UP): 0:(165.70789,214.53561)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(19.0,106.0)
:Sending Touch (ACTION_POINTER_DOWN 1): 0:(0.0,108.30382) 1:(11.0,171.0)
:Sending Touch (ACTION_MOVE): 0:(0.0,116.11915) 1:(6.463764,177.53587)
:Sending Touch (ACTION_POINTER_UP 1): 0:(0.0,122.11656) 1:(0.0,184.39256)
:Sending Touch (ACTION_UP): 0:(0.0,134.64552)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 21    // KEYCODE_DPAD_LEFT
:Sending Key (ACTION_UP): 21    // KEYCODE_DPAD_LEFT
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(30.0,283.0)
:Sending Touch (ACTION_UP): 0:(29.540138,270.772)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(35.0,315.0)
:Sending Touch (ACTION_MOVE): 0:(41.940323,301.62585)
:Sending Touch (ACTION_MOVE): 0:(46.01829,277.74582)
:Sending Touch (ACTION_UP): 0:(48.474968,266.00723)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 21    // KEYCODE_DPAD_LEFT
:Sending Key (ACTION_UP): 21    // KEYCODE_DPAD_LEFT
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 145    // KEYCODE_NUMPAD_1
:Sending Key (ACTION_UP): 145    // KEYCODE_NUMPAD_1
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(1.0,-2.0)
:Sending Trackball (ACTION_MOVE): 0:(-2.0,-2.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,4.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,0.0)
:Sending Trackball (ACTION_MOVE): 0:(1.0,3.0)
:Sending Trackball (ACTION_MOVE): 0:(3.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(-2.0,-1.0)
:Sending Key (ACTION_DOWN): 20    // KEYCODE_DPAD_DOWN
:Sending Key (ACTION_UP): 20    // KEYCODE_DPAD_DOWN
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(183.0,203.0)
:Sending Touch (ACTION_UP): 0:(194.52084,187.27563)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 20    // KEYCODE_DPAD_DOWN
:Sending Key (ACTION_UP): 20    // KEYCODE_DPAD_DOWN
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(100.0,171.0)
:Sending Touch (ACTION_UP): 0:(96.87977,170.16449)
Sleeping for 0 milliseconds
:Sending Flip keyboardOpen=true
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 19    // KEYCODE_DPAD_UP
:Sending Key (ACTION_UP): 19    // KEYCODE_DPAD_UP
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 95    // KEYCODE_SWITCH_CHARSET
:Sending Key (ACTION_UP): 95    // KEYCODE_SWITCH_CHARSET
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 20    // KEYCODE_DPAD_DOWN
:Sending Key (ACTION_UP): 20    // KEYCODE_DPAD_DOWN
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(4.0,0.0)
:Sending Trackball (ACTION_MOVE): 0:(3.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(0.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(0.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,-1.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,0.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,-5.0)
:Sending Trackball (ACTION_MOVE): 0:(-4.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,3.0)
:Sending Key (ACTION_DOWN): 21    // KEYCODE_DPAD_LEFT
:Sending Key (ACTION_UP): 21    // KEYCODE_DPAD_LEFT
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 206    // KEYCODE_3D_MODE
:Sending Key (ACTION_UP): 206    // KEYCODE_3D_MODE
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 4    // KEYCODE_BACK
:Sending Key (ACTION_UP): 4    // KEYCODE_BACK
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(168.0,253.0)
:Sending Touch (ACTION_POINTER_DOWN 1): 0:(166.42249,231.88263) 1:(212.0,58.0)
:Sending Touch (ACTION_MOVE): 0:(166.0931,228.01944) 1:(195.48389,51.1498)
:Sending Touch (ACTION_MOVE): 0:(155.35042,223.16498) 1:(175.32312,31.635899)
:Sending Touch (ACTION_MOVE): 0:(154.35658,211.59282) 1:(161.17451,21.018143)
:Sending Touch (ACTION_MOVE): 0:(142.34442,188.13126) 1:(154.24657,14.730347)
:Sending Touch (ACTION_POINTER_UP 1): 0:(138.6136,164.46696) 1:(153.50166,3.2237
806)
:Sending Touch (ACTION_UP): 0:(129.04036,156.62956)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(52.0,215.0)
:Sending Touch (ACTION_UP): 0:(50.890404,218.14482)
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(1.0,0.0)
:Sending Trackball (ACTION_MOVE): 0:(0.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,-1.0)
:Sending Trackball (ACTION_MOVE): 0:(-1.0,-2.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-3.0)
:Sending Trackball (ACTION_MOVE): 0:(4.0,0.0)
:Sending Trackball (ACTION_MOVE): 0:(-2.0,0.0)
:Sending Trackball (ACTION_MOVE): 0:(-2.0,-4.0)
:Sending Touch (ACTION_DOWN): 0:(136.0,308.0)
:Sending Touch (ACTION_MOVE): 0:(140.34961,310.26785)
:Sending Touch (ACTION_UP): 0:(161.46814,310.94815)
Sleeping for 0 milliseconds
:Sending Key (ACTION_DOWN): 22    // KEYCODE_DPAD_RIGHT
:Sending Key (ACTION_UP): 22    // KEYCODE_DPAD_RIGHT
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(202.0,241.0)
:Sending Touch (ACTION_MOVE): 0:(200.27258,244.47406)
:Sending Touch (ACTION_MOVE): 0:(198.87253,246.80893)
:Sending Touch (ACTION_UP): 0:(198.36757,252.80966)
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(-4.0,-1.0)
:Sending Trackball (ACTION_MOVE): 0:(3.0,4.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,-5.0)
Events injected: 100
:Sending rotation degree=0, persist=false
:Dropped: keys=30 pointers=67 trackballs=0 flips=0 rotations=0
## Network stats: elapsed time=909ms (909ms mobile, 0ms wifi, 0ms not connected)

// Monkey finished
Level0-3输出信息举例

5.Monkey日志分析
adb shell monkey 100 >E:/log/a.txt
将log信息写到文档中
正常情况,如果Monkey测试顺利执行完成,在log的最后,会打印出当前执行事件的次数和所花费的时间

技术分享图片

技术分享图片

随机数种子
用于指定伪随机数生成器的seed值,如果seed相同,则两次Monkey测试所产生的事件序列也相同的。示例:
monkey测试1:adb shell monkey -p <软件包名> –s 10 100
monkey测试2:adb shell monkey -p <软件包名> –s 10 100

技术分享图片

技术分享图片

6.--throttle <毫秒>
用亍指定用户操作(即事件)间的时延,单位是毫秒;如果不指定这个参数,monkey会尽可能快的生成和发送消息。
示例:adb shell monkey -p com.example.login --throttle 3000 100 #3秒之后开始执行

技术分享图片

7.--pct-touch <percent>
调整触摸事件的百分比。(触摸事件是指在屏幕中的一个down-up事件,即在屏幕某处按下并抬起的操作)
示例:adb shell monkey --pct-touch 100 -v 10

技术分享图片

8.--pct-motion <percent>
调整motion事件百分比。(motion事件是由屏幕上某处一个down事件、一系列伪随机的移动事件和一个up事件组成)
示例:adb shell monkey --pct-motion 100 -v 10

技术分享图片

9.--ignore-crashes
作用:通常应用发生崩溃或异常时Monkey会停止运行。如果设置此项,Monkey会在日志中记录相关信息并将继续发送事件给系统,直到事件计数完成。

正常操作,不忽略异常

技术分享图片

忽略异常

技术分享图片

10.--ignore-timeouts
作用:通常应用程序发生任何超时错误(如“Application Not responding”对话框)Monkey将停止运行,设置此项,Monkey将继续发送事件给系统,直到事件计数完成。

11.Monkey实例
实例1:adb shell monkey -p com.example.login --ignore-crashes --ignore-timeouts --throttle 100 --pct-touch 50 --pct-motion 50 -v -v 100 >E:loglogin.txt
用monkey 命令对登录apk进行性能测试

技术分享图片

技术分享图片

实例2:adb shell monkey -p com.example.login --pct-touch 30 --pct-motion 30 --pct-trackball 0 --pct-nav 0 --pct-majornav 20  --pct-appswitch 10 --pct-anyevent 10 -s 12867 --ignore-crashes --ignore-timeouts --throttle 100  -v -v 100 >E:loglogin.txt

使用--pct-xxx参数进行操作的限制。比如操作不仅仅需要点击,还需要滑动、长按,在智能手机硬件按钮、应用之间的切换等。

12.Monkey异常分析
Monkey测试出现错误后,一般的分析步骤:
看Monkey的日志 (注意第一个swith以及异常信息等)
1. 程序无响应的问题: 在日志中搜索 “ANR”
2. 崩溃问题:在日志中搜索“Exception”或者“crash”(如果出现空指针,NullPointerException)肯定是有bug
Monkey 执行中断, 在log最后也能看到当前执行次数

三 Monkey进阶

1.第一个简单的Monkey测试命令

技术分享图片

上图日志中间部分显示了操作事件的默认百分比,这些百分比数字对应的操作可参考Monkey测试的源码.../Monkey/src/com/android/commands/monkey/MonkeySourceRandom.java文件中的操作定义。

技术分享图片

2.Monkey测试注意点

2.1.测试前准备好一切测试的前置条件

2.2.慎重使用adb服务
比如当你正在执行Monkey测试时,由于某种原因使用adb命令重启了adb服务,那么Monkey测试的日志记录就会被终止,但正在机器上执行的测试行为不会停止。换句话说,你会看到测试在执行,但却没有任何记录了。

2.3.同时记录Android系统日志
adb logcat还是非常有必要的。

2.4.一定需要记录Seed值
Seed值是唯一能够重现(repro)Monkey测试中出现的崩溃问题的方法。当问题被修复后同样需要它来进行重现并验证。

2.5.不要使用单一的命令
多写几条有不同测试偏重点的Monkey测试命令,在一个项目中同时使用,有助于找到更多的缺陷。

2.6.必须重视Crash
Monkey测试出现的NullPointException,一般都是可以在用户使用时出现的,何时出现只是时间问题。所以本质上来说,Monkey所有的CRASH都需要在Release前fix掉。

3.Monkey工具再探索

从development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java源码中可以很容易找到Monkey测试提供的每个参数,以及是如何实现的,有什么弊端。如果觉得Monkey测试的伪随机流还不能满足测试的需求,希望能够按照特定步骤来执行测试的话,可用以下两种方法:
(1)更改Monkey自身的源码。在Linux环境下,下载要测试版本对应的全部源代码,在终端中定位到源代码的根目录,输入make monkey即可。通过编译之后可在/out/.../monkey/中获取Monkey.jar这个包。将编译好的包通过adb push到要测试的Android系统中的/System/framework下。随后,或通过Monkey提供的参数启动,或通过自己写的参数调用即可。
2.可编写脚本进行指定的操作。写一个脚本,将需要Monkey执行的操作放在一个队列中,从而根据自定义的步骤执行。具体步骤大家可自行谷歌相关信息,或者查看源码中的核心类:android-x.x.x/development/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java。

以上是关于Monkey测试的主要内容,如果未能解决你的问题,请参考以下文章

Android自动化测试——Monkey

adb命令之monkey压力测试篇

monkey测试===Monkey测试结果分析(系列三)转

android测试monkey啥意思

Android命令Monkey压力测试,详解

monkey测试===Monkey测试策略(系列二)转