Monkey介绍

Monkey Testing,也有人叫做搞怪测试。就是用一些稀奇古怪的操作方式去测试被测试系统,以测试系统的稳定性。Monkey Test,一般指这样的测试活动,用毫无规律的指令或操作使用被测系统,观察系统的稳定性和容错性能。
Monkey测试是Android自动化测试的一种手段,Monkey测试本身非常简单,就是模拟用户的按键输入,触摸屏输入,手势输入等,看设备多长时间会出异常。
当Monkey程序在模拟器或设备运行的时候,如果用户触发了比如点击,触摸,手势或一些系统级别的事件的时候,它就会产生随机脉冲,所以可以用Monkey用随机重复的方法去负荷测试你开发的软件.
(来源百度)
Monkey可以通过命令随机测试,也可以通过脚本自定义测试。

一、搭建环境

1.先下载jdk 请参考:
https://blog.csdn.net/write6/article/details/79136388

2.再下载sdk 请参考:
https://blog.csdn.net/u011541946/article/details/77142045
二、连接手机/获取包名

打开手机配置开发者调试,USB连接;
cmd进入dos命令行,输入:adb devices,连接成功;
Monkey命令使用及脚本编写
运行应用,dos命令行输入:adb shell dumpsys window | findstr mCurrentFocus,获取包名和Activity;
Monkey命令使用及脚本编写

三、Monkey命令

Monkey命令使用及脚本编写

-p 用于约束限制,用此参数指定一个包,指定包后Monkey将被允许启动指定应用;如果不指定包, Monkey将被允许随机启动设备中的应用(主Activity有android.intent.category.LAUNCHER 或android.intent.category.MONKEY类别 )。比如 adb shell monkey -p xxx.xxx.xxx 1 ; xxx.xxx.xxx 表示应用包名,1 表示monkey模拟用户随机事件参数,最低1,这样就能把应用启动起来
-c 指定Activity的category类别,如果不指定,默认是CATEGORY_LAUNCHER 或者 Intent.CATEGORY_MONKEY;不太常用的一个参数
-v 用于指定反馈信息级别,也就是日志的详细程度,分Level1、Level2、Level3;-v 默认值,仅提供启动提示,操作结果等少量信息 ,也就是Level1,比如adb shell monkey -p xxx.xxx.xxx -v 1 ;-v -v 提供比较详细信息,比如启动的每个activity信息 ,也就是Level2,比如adb shell monkey -p xxx.xxx.xxx -v -v 1 ;-v -v -v 提供最详细的信息 ,比如adb shell monkey -p xxx.xxx.xxx -v -v -v 1
-s 伪随机数生成器的种子值,如果我们两次monkey测试事件使用相同的种子值,会产生相同的事件序列;如果不指定种子值,系统会产生一个随机值。种子值对我们复现bug很重要。使用如下adb shell monkey -p xxx.xxx.xxx -s 11111 10;这也是伪随机事件的原因,因为这些事件可以通过种子值进行复现
–ignore-crashes 忽略异常崩溃,如果不指定,那么在monkey测试的时候,应用发生崩溃时就会停止运行;如果加上了这个参数,monkey就会运行到指定事件数才停止。比如adb shell monkey -p xxx.xxx.xxx -v -v -v --ignore-crashes 10
–ignore-timeouts 忽略ANR,情况与4类似,当发送ANR时候,让monkey继续运行。比如adb shell monkey -p xxx.xxx.xxx -v -v -v --ignore-timeouts 10
–ignore-native-crashes 忽略native层代码的崩溃,情况与4类似,比如adb shell monkey -p xxx.xxx.xxx -v -v -v --ignore-native-crashes 10
–ignore-security-exceptions 忽略一些许可错误,比如证书许可,网络许可,adb shell monkey -p xxx.xxx.xxx -v -v -v --ignore-security-exceptions 10
–monitor-native-crashes 是否监视并报告native层发送的崩溃代码,adb shell monkey -p xxx.xxx.xxx -v -v -v --monitor-native-crashes 10
–kill-procress-after-error 用于在发送错误后杀死进程
–hprof 设置后,在Monkey事件序列之前和之后立即生产分析报告,保存于data/mic目录,不过将会生成大量几兆文件,谨慎使用
–throttle 设置每个事件结束后延迟多少时间再继续下一个事件,降低cpu压力;如果不设置,事件与事件之间将不会延迟,事件将会尽快生成;一般设置300ms,因为人最快300ms左右一个动作,比如 adb shell monkey -p xxx.xxx.xxx -v -v -v --throttle 300 10
–pct-touch 设置触摸事件的百分比,即手指对屏幕进行点击抬起(down-up)的动作;不做设置情况下系统将随机分配各种事件的百分比。比如adb shell monkey -p xxx.xxxx.xxx --pct-touch 50 -v -v 100 ,这就表示100次事件里有50%事件是触摸事件
–pct-motion 设置移动事件百分比,这种事件类型是由屏幕上某处的一个down事件-一系列伪随机的移动事件-一个up事件,即点击屏幕,然后直线运动,最后抬起这种运动。
–pct-trackball 设置轨迹球事件百分比,这种事件类型是一个或者多个随机移动,包含点击事件,这里可以是曲线运动,不过现在手机很多不支持,这个参数不常用
–pct-syskeys 设置系统物理按键事件百分比,比如home键,音量键,返回键,拨打电话键,挂电话键等
–pct-nav 设置基本的导航按键事件百分比,比如输入设备上的上下左右四个方向键
–pct-appswitch 设置monkey使用startActivity进行activity跳转事件的百分比,保证界面的覆盖情况
–ptc-anyevent 设置其它事件百分比
–ptc-majornav 设置主导航事件的百分比
保存dos窗口打印的monkey信息,在monkey命令后面补上输出地址,如adb shell monkey -p xxx.xxxx.xxx -v -v 100 > D:\monkey.txt;这样monkey测试结束后,所有打印的信息都会输出到这个文件里
通过adb bugreport 命令可以获取整个android系统在运行过程中所有app的内存使用情况,cpu使用情况,activity运行信息等,包括出现异常等信息。使用方法 adb bugreport > bugreport.txt ;这样在当前目录就会产生一个txt文件和一个压缩包,具体信息可在压缩包查看,txt文件只会记录压缩包的生成过程信息
-f 加载monkey脚本文件进行测试,比如 adb shell monkey -f sdcard/monkey.txt -v -v 500

例:

adb shell monkey -s 123123 --throttle 300 --pct-touch 40 --pct-motion 25 --pct-appswitch 30 --pct-syskeys 5 --pct-majornav 0 --pct-nav 0 --pct-trackball 0 -p com.newant.bus 1000 > C:\monkeyLog\monkey.txt

四、Monkey脚本

1、Monkey除了可以随机测试,也可以通过自定义脚本测试某个特定的场景,用户需要按照Monkey脚本规范编写好脚本,接着存放在手机上并执行;

2、获取元素坐标点位置

方法一(UiAutomatorViewer):
Monkey命令使用及脚本编写
使用UiAutomatorViewer进行安卓app控件定位时经常会报错,下面是万能用法;
2.1、cmd进入dos命令行,输入:
adb shell uiautomator dump /sdcard/app.uix
adb pull /sdcard/app.uix C:/work/getUi/app.uix
adb shell screencap -p /sdcard/app.png
adb pull /sdcard/app.png C:/work/getUi/app.png

2.2、打开uiautomatorviewer.bat,导入文件;
2.3、定位元素。

方法二:
打开手机中的开发人员选项,打开“显示指针位置”。随后,在屏幕上的每次操作,在导航栏上都会显示坐标信息。

3、脚本API简介

LaunchActivity(pkg_name, cl_name):启动应用的Activity。参数:包名和启动的Activity。

Tap(x, y, tapDuration): 模拟一次手指单击事件。参数:x,y为控件坐标,tapDuration为点击的持续时间,此参数可省略。

DispatchPress(keyName): 按键。参数: keycode。 详细的Android Keycode列表: http://blog.csdn.net/feizhixuan46789/article/details/16801429

RotateScreen(rotationDegree, persist): 旋转屏幕。 参数:rotationDegree为旋转角度, e.g. 1代表90度;persist表示旋转之后是否固定,0表示旋转后恢复,非0则表示固定不变。

DispatchFlip(true/false): 打开或者关闭软键盘。

LongPress(): 长按2秒。

PressAndHold(x, y, pressDuration): 模拟长按事件。

DispatchString(input): 输入字符串。

Drag(xStart, yStart, xEnd, yEnd, stepCount): 用于模拟一个拖拽操作。

PinchZoom(x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End, stepCount): 模拟缩放手势。

UserWait(sleepTime): 休眠一段时间

DeviceWakeUp(): 唤醒屏幕。

PowerLog(power_log_type, test_case_status): 模拟电池电量信息。

WriteLog(): 将电池信息写入sd卡。

RunCmd(cmd): 运行shell命令。

DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFlags): 向指定位置,发送单个手势。

DispatchPointer(downtime,eventTime,action,x,yxpressure,size,metastate,xPrecision,yPrecision,device,edgeFilags): 发送按键消息。

LaunchInstrumentation(test_name,runner_name): 运行一个instrumentation测试用例。

DispatchTrackball: 模拟发送轨迹球事件。

ProfileWait: 等待5秒。

StartCaptureFramerate(): 获取帧率。

EndCaptureFramerate(input): 结束获取帧率。

更多可参考:

4、例如登陆脚本
type= raw events //脚本类型
count= 1 //执行次数
speed= 1.0 //执行速率
start data >>

#启动应用,第一个参数是包名,第二个是启动的activity名
LaunchActivity(com.newant.bus,com.newant.bus.MainActivity);
UserWait(3000);

#跳过广告
Tap(938, 167);
UserWait(3000);

#点击手机号输入框
Tap(370, 621);
UserWait(3000);

#如果有默认账户,要加一步把他清除了
Tap(858,630);
UserWait(3000);

#点击手机号输入框
Tap(370, 621);
UserWait(3000);

#关闭软键盘
DispatchFlip(false)

#输入手机号
DispatchString(13631271064);
UserWait(3000);

#点击密码输入框
Tap(40,700);
UserWait(3000);

#关闭软键盘
DispatchFlip(false)

#输入密码
DispatchString(Test88888888);
UserWait(3000);

#点击登陆
Tap(550,1179);

5、执行脚本

#将脚本推送到手机sd卡上
adb push C:/monkeyLog/code_login.txt sdcard/code_login.txt

#执行脚本文件
adb shell monkey -f sdcard/code_login.txt -v -v 1

本文结束。

参考
https://www.jianshu.com/p/85454be8424f
https://blog.csdn.net/qq_30993595/article/details/80748559
https://blog.csdn.net/just__like/article/details/83757492
https://blog.csdn.net/qq_40356449/article/details/103302820

相关文章:

  • 2021-09-09
  • 2021-07-28
  • 2021-12-10
  • 2022-01-15
  • 2022-12-23
  • 2021-11-28
  • 2021-11-23
猜你喜欢
  • 2021-12-10
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2021-07-12
  • 2021-11-15
  • 2022-03-09
相关资源
相似解决方案