欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

3-Appium之monkey测试

程序员文章站 2022-07-01 16:11:47
插播一篇介绍monkey的博客...

上节我们学习了一些adb的常用命令。如果你是做移动端测试的话,应该对一个叫monkey的工具比较了解。
在继续讲解Appium的知识之前,本节我们插播一篇博客。

monkey是Android SDK提供的一个命令行工具,可以发送伪随机的用户事件流(你可以简单理解为模拟用户的各种操作)。有人介绍说是对Android APP做压力或稳定性测试。我不是特别喜欢这种说法,我潜意识里认为压力和稳定性都是指的在并发情况下被测系统的性能,但明显monkey是顺序依次的操作,单线程,没并发。我更愿意将其视为“随机性”或“健壮性”测试。

为啥叫monkey,就是说该命令会模拟猴子一样乱点、乱画,随机的在屏幕上执行各种各样的操作,没有明确的目标。

记得最早用monkey的时候,应该是2010年在华为做Android OS测试的时候,当时部门领导要求测试机每天下班的时候要跑上monkey,第二天来了后,将问题手机交给研发人员定位问题。

1、参数概览

支持的参数如下:

C:\Users\duzil>adb shell monkey
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


2、adb shell monkey 组织形式

一般adb shell monkey命令后面跟几部分内容:
3-Appium之monkey测试

(1)基本参数说明

基本参数 说明
-p <allowed -package-name 指定一个或多个包
-s < seed> 指定一个随机数生成器的seed值
–throttle < milliseconds> 指定事件之间的固定延迟(ms)
-v 指定反馈信息级别(信息级别就是日志的详细程度)
-c < main-category> 指定一个或多个类别名
-f < scriptfile> 运行指定的monkey脚本

(2)事件参数说明

事件参数 说明
–pct-touch < percent> 指定触摸事件百分比
–pct-motion < percent> 指定动作事件百分比
–pct-trackball < percent> 指定轨迹事件百分比
–pct-syskeys < percent> 指定系统按键事件百分比
–pct-nav < percent> 指定基本导航事件百分比
–pct-majornav < percent> 指定主要导航事件百分比
–pct-appswitch < percent> 指定应用启动事件百分比
–pct-flip < percent> 指定flip(弹)事件的百分比
–pct-anyevent < percent> 指定其他类型事件百分比
–pct-pinchzoom < percent> 指定缩放(捏)事件百分比
–pct-permission < percent> 指定……

(3)调试参数

调试参数 说明
–ignore-crashes 忽略因崩溃或异常引起的停止运行
–ignore-timeouts 忽略应用发生超时错误(ANR)
–ignore-security-exceptions 忽略应用发生任何权限错误
–monitor-native-crashes 监视并报告monkey运行时native code的崩溃事件
–ignore-native-crashes 忽略因native code的崩溃事件
–kill-process-after-error 在应用出错后通知系统杀死发生错误的进程
–hprof 在事件序列前后立即生成profilfing report
–dbg-no-events 监视应用程序所调用的包之间的转换
–wait-dbg 暂停执行中的monkey,直到有调试器与它连接

3、命令演示

(1)对手机任意APP操作1次

C:\Users\duzil>adb shell monkey 1
Events injected: 1
## Network stats: elapsed time=17ms (0ms mobile, 0ms wifi, 17ms not connected)

C:\Users\duzil>

(2)对指定的包,随意操作10次

C:\Users\duzil>adb shell monkey -p com.tencent.android.qqdownloader 10
    // activityResuming(com.tencent.android.qqdownloader)
Events injected: 10
## Network stats: elapsed time=5208ms (0ms mobile, 0ms wifi, 5208ms not connected)

C:\Users\duzil>

(3)打印日志

  • -v:打印日志信息,每个-v将增加输出信息的级别,-v越多,日志信息越详细,最多支持3个-v
  • -v,提供启动提示、测试完成、最终结果等少量信息
  • -v -v,提供较为详细的日志,包括每个发送到Activity的事件信息
  • -v -v -v,最详细的日志,包括测试中选中、未选中的Activity的信息。

以下是对计算器应用的一个操作示例:

C:\Users\duzil>adb shell monkey -p com.ddnapalon.calculator.gp  -v -v -v  10
:Monkey: seed=1600234464317 count=10
:AllowPackage: com.ddnapalon.calculator.gp
: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.tencent.pangu.link.SplashActivity (from package com.tencent.android.qqdownloader)
//   - 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)
//   - NOT USING main activity io.appium.settings.Settings (from package io.appium.settings)
//   + Using main activity com.ddnapalon.calculator.gp.ScienceFragment (from package com.ddnapalon.calculator.gp)
// 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: 1600234464317
// 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.ddnapalon.calculator.gp/.ScienceFragment;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.ddnapalon.calculator.gp/.ScienceFragment } in package com.ddnapalon.calculator.gp
Sleeping for 0 milliseconds
:Sending Trackball (ACTION_MOVE): 0:(-5.0,-1.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-4.0)
:Sending Trackball (ACTION_MOVE): 0:(-2.0,2.0)
:Sending Trackball (ACTION_MOVE): 0:(3.0,3.0)
:Sending Trackball (ACTION_MOVE): 0:(-5.0,1.0)
:Sending Trackball (ACTION_MOVE): 0:(1.0,-2.0)
:Sending Trackball (ACTION_MOVE): 0:(-3.0,4.0)
:Sending Trackball (ACTION_MOVE): 0:(2.0,-2.0)
:Sending Trackball (ACTION_MOVE): 0:(-1.0,3.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=473ms (0ms mobile, 0ms wifi, 473ms not connected)
// Monkey finished

(4)约定事件类型

这里演示约定100%时间类型都是touch类型

C:\Users\duzil>adb shell monkey -p com.ddnapalon.calculator.gp -v -v -v  --pct-touch 100 10
:Monkey: seed=1600235066676 count=10
:AllowPackage: com.ddnapalon.calculator.gp
: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.tencent.pangu.link.SplashActivity (from package com.tencent.android.qqdownloader)
//   - 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)
//   - NOT USING main activity io.appium.settings.Settings (from package io.appium.settings)
//   + Using main activity com.ddnapalon.calculator.gp.ScienceFragment (from package com.ddnapalon.calculator.gp)
// 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: 1600235066676
// Event percentages:
//   0: 100.0%
//   1: 0.0%
//   2: 0.0%
//   3: 0.0%
//   4: -0.0%
//   5: -0.0%
//   6: 0.0%
//   7: 0.0%
//   8: 0.0%
//   9: 0.0%
//   10: 0.0%
//   11: 0.0%
:Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=com.ddnapalon.calculator.gp/.ScienceFragment;end
    // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.ddnapalon.calculator.gp/.ScienceFragment } in package com.ddnapalon.calculator.gp
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(6.0,939.0)
:Sending Touch (ACTION_UP): 0:(13.326804,938.92957)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(751.0,958.0)
:Sending Touch (ACTION_UP): 0:(739.85266,956.5599)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(195.0,235.0)
:Sending Touch (ACTION_UP): 0:(196.64018,235.05173)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(444.0,82.0)
:Sending Touch (ACTION_UP): 0:(429.81186,100.30007)
Sleeping for 0 milliseconds
:Sending Touch (ACTION_DOWN): 0:(325.0,936.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=36ms (0ms mobile, 0ms wifi, 36ms not connected)
// Monkey finished

(5)将日志存放到文件

将日志输出到c盘的AAA目录下的a.log文件中

C:\Users\duzil>adb shell monkey -p com.ddnapalon.calculator.gp -v -v -v  --pct-touch 100 10>c:\AAA\a.log

C:\Users\duzil>

好,对adb monkey的应用简单介绍到这里,谢谢。

本文地址:https://blog.csdn.net/duzilonglove/article/details/108552236