zoukankan      html  css  js  c++  java
  • 小白Monkey学习笔记

    Monkey是google提供的一款对Android app进行压力测试工具,基于随机坐标位置,进行点击、滑动、输入等操作.

    Monkey的环境配置

    1. pc电脑需要配置adb环境
    2. Monkey程序由Android系统自带,使用Java语言写,在Android文件系统中的存放路径是:/system/framework/monkey.jar
    3. Monkey.jar程序是由一个名为"monkey"的Shell脚本来启动执行,shell脚本在Android文件系统中的存放路径:/system/bin/monkey

    常用命令介绍

    官方文档

    https://developer.android.com/studio/test/monkey

    help

    adb shell monkey -help 打印参数帮助信息
    
    out:
    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

    p

    查看当前app包名命令:
    adb shell "dumpsys window |grep mCurrent" adb shell dumpsys activity activities | sed -En -e '/Running activities/,/Run #0/p'
    adb shell monkey
    -p com.test.android(app包名) 启动包名是com.test.android的app进行monkey测试.

    COUNT

    db shell monkey -p com.test.android 300(COUNT)
    
    COUNT是需要Monkey执行的总共事件数.

    v

    adb shell monkey -p com.test.android -v 100
    
    adb shell monkey -p com.test.android -v -v -v 100
    
    v是设置日志级别,默认是一个
    
    如需更详细的日志可以加多个-v,最多3个

    s

    adb shell monkey -p com.test.android(app包名) -s 50(随机种子)
    
    s表示一个随机种子,必须是数字类型.可以用于两次monkey测试,第二次monkey回归第一次monkey测试出来的bug,指定的随机种子是一样的.
    那么两次操作的事件操作基本一致.需要说明的是,随机种子仅仅的是操作是一样的,有些app启动以后会有弹框或者其他引导提醒,那么这样不能保证两次操作的路径是完全一样,需要手工去除
    伪随机数生成器的 seed 值,通俗的说就是个标记,后面跟数字,例如:执行adb shell monkey -s 1 -p 包名 事件总数,这个我标记了-s 1,命令操作完之后,我发现有日志报错,我想重新执行这个monkey操作,那你就可以继续执行这个命令,排错时常用

    throttle

    adb shell monkey -p com.test.android --throttle 500 300
    
    throttle表示两次操作之间的延迟,单位是毫秒.因为实际模拟器用户操作并不一定要操作很快,所以500ms延迟到1000ms接近用户的真实使用.
    减速的意思,后面接时间,单位为ms,,表示事件之间的固定延迟,如果不接该项,monkey将不会延迟

    randomize-throttle

    adb shell monkey -p com.test.android --throttle 500 --randomize-throttle 300
    
    randomize-throttle表示每次事件的间隔在0到500毫秒之间不固定

    pct

    --pct-touch {+百分比}:0
    触摸,触摸事件泛指发生在某一位置的一个down-up事件,点击
    --pct-motion {+百分比}:1
    动作,动作事件泛指从某一位置接下(即down事件)后经过一系列伪随机事件后弹出(即up事件)
    --pct-pinchzoom {+百分比}:2
    二指缩放,智能机上的放大缩小手势操作事件
    --pct-trackball {+百分比}: 3
    轨迹,轨迹事件包括一系列的随机移动,以及偶尔跟随在移动后面的点击事件
    --pct-rotation {+百分比}:4
    屏幕旋转,横屏竖屏事件
    --pct-nav {+百分比}:5
    基本导航,基本导航事件主要来自方向输入设备的上、下、左、右事件
    --pct-majornav {+百分比}:6
    主要导航,主要导航事件通常指引发图形界面的一些动作,如键盘中间按键、返回按键、菜单按键等
    --pct-syskeys {+百分比}:7
    系统按键,系统按键事件通常指仅供系统使用的保留按键,如HOME键、BACK键、拨号键、挂断键、音量键等
    --pct-appswitch {+百分比}:8
    应用启动,应用启动事件(activity launches)即打开应用,通过调用startActivity()方法最大限度地开启该package下的所有应用
    --pct-flip {+百分比}:9
    翻转,键盘轻弹百分比,如点击输入框,键盘弹起,点击输入框以外区域,键盘收回
    --pct-anyevent {+百分比}:10
    翻译其他类型,其他类型事件指上文中未涉及的所有其他事件,如keypress、不常用的button等
    
    adb shell monkey -p com.test.android --pct-touch 20 --pct-motion 30 300

    ignore-crashes

    adb shell monkey -p com.test.android --ignore-crashes 500
    
    ignore-crashes表示运行monkey中,遇到crash会忽略,继续执行后续的操作.

    ignore-timeouts

    adb shell monkey -p com.test.android --ignore-timeouts 500
    
    ignore-timeouts表示运行monkey中,遇到ANR会忽略,继续执行后续的操作.

    ignore-native-crashes

    adb shell monkey -p com.test.android --ignore-native-crashes 500 
    
    ignore-native-crashes表示忽略monkey本身的异常,直到事件执行完毕

    monkey测试中,如何终止运行:

    方法一:

    • 新建一个窗口
    • 执行: adb shell ,
    • 再执行ps | grep monkey,返回来的第一个数字,即是monkey的进程号
    • 然后kill pid 进程号

    方法二:

    • 对手机进行重启
    • 新建一个窗口
    • 执行adb reboot

    log日志导出

    保存到电脑上

    • 例如:保存到电脑上并命名为monkey.txt
    adb shell monkey -p 包名 -v 1000 > /Users/X/Desktop/monkey.txt
    • 例如:保存到电脑上标准流与错误流分开保存,分别命名为info.txt,error.txt
    • nfo.txt:此文件在手机上的SDCARD中,主要记录了MONKEY测试时发送的各种事件,如触摸事件的位置等等
    • error.txt:此文件在手机上的SDCARD中,主要记录了MONKEY测试时产生的一些ANR、强制关闭等异常。 执行
    adb shell monkey -p 包名 -v 1000 1>/Users/X/Desktop/info.txt 2>/Users/weixiangyang/Desktop/error.txt

    参考:

    https://testerhome.com/topics/15966

    https://testerhome.com/topics/12013

    http://shark.farbox.com/post/xing-neng-ce-shi/monkeyyong-fa-zong-jie

  • 相关阅读:
    [ZJOI2011]营救皮卡丘
    TJOI2018Party
    HEOI2013SAO
    [BJOI2017]树的难题
    [HNOI2016]序列
    [SHOI2007]善意的投票
    CF802C Heidi and Library (hard)
    SPOJ DIVCNT2
    LOJ子序列
    BZOJ2882工艺
  • 原文地址:https://www.cnblogs.com/wx2017/p/10283178.html
Copyright © 2011-2022 走看看