Android 之Monkey、MonkeyScript、MonkeyRunner


Monkey是发送伪随机用户事件的工具。 

MonkeyScript实现自动测试的脚本,是一组可以被Monkey识别的命令集合,可以完成重复固定的操作。不支持截屏。 

MonkeyRunner提供一系列api操作。 

Monkey与MonkeyRunner的区别:

  • Monkey:在adb shell中,生产用户或系统的伪随机事件
  • MonkeyRunner:通过API定义特定命令和事件控制设备

MonkeyRunner APIs

  • MonkeyRunner:用来连接设备或模拟器的

  • MonkeyDevice:提供安装、卸载应用,发送模拟事件

  • MonkeyImage:完成图像保存,及对比的操作

MonkeyRunner的测试类型 :多设备控制, 功能测试, 回归测试

压力测试结果。
1、crash,即崩溃,应用程序在使用过程汇总,非正常退出
2、ANR,application not responding 响应时间过长

实践:(需配置android sdk和python环境)

第一步:usb数据线连接手机与电脑 
第二步:cmd窗口输入adb devices,若显示类似下图则连接成功 
第三步:安装测试app 
第四步:发送压力指令,adb shell monkey 1000(随机完成1000个指令) 
第五步:获取测试应用包名 
adb logcat | grep START
(获取app执行的log信息作为下一个抓取日志中包括start标签的输出的输入,grep 是linux下的命令,windows系统可以使用 findstr 替代 grep。或者执行adb shell进入到手机,在手机系统下,执行logcat | grep START。android系统是linux的,所以支持grep) 
cmp 后面的就是包名
虚拟机测试图,输出会跟着操作而更新 
这里写图片描述 
第六步:给指定包打压力 
adb shell monkey -p package 1000

monkey高级参数的应用(参数均在包名后,可组合使用)

1、throttle 参数
 指定事件之间的间隔 间隔1s
adb shell monkey --throttle <毫秒>

adb shell monkey -p package --throttle 1000 100 ,100个事件,


2、seed参数,
adb shell monkey -s <seed> <event-count>
adb shell monkey -p com.android.calculator2 -s 100 50 ,执行两边测试,两次随机的序列是一样的,最终的结果是一样的,

seed 的值是一样的, 这样计算器输出的结果是一样的


3、触摸事件
adb shell monkey --pct-touch <percent>,  如果percent设置成100,就是点击事件占比100%

abd shell monkey -v -p com.android.calculator2 --pct-touch 100 100 ,-v 是把完成的事件列出来


4、动作事件,
设定动作事件百分比
adb shell monkey --pct-motion<percent>,
adb shell monkey -v -p com.android.calculator2 --pct-touch 50 --pct-motion 30  100

百分比,touch 百分之50,motion 百分之30.


5、轨迹球事件
设定轨迹球事件百分比

adb shell monkey --pct-trackball <percent>


6、基本导航事件,
设定基本导航事件百分比,输入设备的上下左右

adb shell monkey --pct-nav<percent>


7、主要导航事件
设定主要导航事件百分比,兼容中间键、返回键、菜单按键

adb shell monkey --pct-majornav<percent> 


8、系统导航事件
设定系统导航事件百分比,HOME、BACK 拨号及音量键

adb shell monkey --pct-syskeys<percent>


9、启动Activity事件
设定启动Acttivity的事件百分比,切换不同的activity 

adb shell monkey --pct-appswitch <percent>


10、不常用事件
设定不常用事件的百分比

adb shell monkey --pct-anyevent<percent>


11、崩溃事件


CRASH结果析取 日志,

adb shell monkey -p -v com.example --ingnore-crashes 10000

ANR 结果析取

以anr开头,或者adb shell,cd /data/anr/ ,ls可以看到traces.txt的文件,more traces.txt可以查看详细信息


MonkeyScript常用命令介绍

执行monkey脚本的命令 
adb shell monkey -f< scriptfile >< event-count> 
脚本命令:

  1. DispatchTrackball 轨迹球事件,配对使用 
    DispatchTrackball(long downtime,long eventide,int action,float x, float y,float pressure,float size,int metastate,float xprecision,float yprecision,int divice,int edgeflags) 
    action 0代表按下,1代表弹起,x,y代表坐标点
  2. DispatchPointer 点击事件 ,配对使用 
    DispatchTrackball(long downtime,long eventide,int action,float x, float y,float pressure,float size,int metastate,float xprecision,float yprecision,int divice,int edgeflags) 
    action 0代表按下,1代表弹起,x,y代表坐标点

  3. DispatchString命令 输入字符串事件 
    DispatchString(String text)

  4. LaunchActivity命令 启动应用 
    LaunchActivity(package,Activity)

  5. LaunchActitity(com.example.minibrowser2,com.example.minibrowser2.myapplication.MainActivity),activity前面要加包名,要不启动不了app

  6. UserWait 等待事件 
    UserWait (millseconds)

  7. DispatchPress 按下键值 
    DispatchPress(int keycode) 如keycode 66=回车键

实例:

1.使用android sdk/tools/uiautomatorviewer.bat工具对要测试的应用界面进行测量,得到控件在界面的左上点坐标,右下点坐标 
 
2.编写脚本(保存为script文件) 
图中LaunchActivity中第二个参数要把activity的action名写全,即com.xxx.xxx.xxx.MainActivity 
这里写图片描述 
3.由于脚本在电脑里,monkey在手机里,所以要先把脚本复制到手机里 
adb push mooc.script /data/local/tmp/ 
可以进入目录下,ls查看目录,ll查看文件创建时间 
3.测试(此时在手机中,不用加adb shell) 

monkey -f mooc.script 2 

在app代码里,要把android:exported="true"
exported 设置为true 才可以调起app

ps:要想能够启动对应应用,其activity在AndroidManifest文件中的exported属性要设置为true,才能在外部被打开!!!

执行脚本命令

adb shell monkey -f /data/local/tmp/mook.script 2


缺点,没法实现截屏操作,这个脚本,没有报错


MonkeyRunner常用API(能截屏)

1.MonkeyRunner API -alert 警告框 
void alert (String message,String title,String okTitle)

#!/user/bin/python
#-*- UTF-8 -*-
from com.android.monkeyrunner import MonkeyRunnerMonkeyRunner.alert("Hello","title","OK")

cmd进入Android sdk/tools目录下,输入monkeyrunner demo.py,(此时已经把demo.py放在了该目录下,否则需加上路径,如monkeyrunner D:\xx\demo.py)得到这里写图片描述 

2.MonkeyRunner API -waitForConnection 

等待设备连接,有多个device id,需指明具体哪个设备

waitForConnection(float timeout,string deviceid)

3. MonkeyDevice API -drag 

拖动(tuple类似point) 

drag(tuple start,tuple end,float duration,integer steps)

start 起点位置,

end终点位置,

duration 手势持续的时间

steps 插值点的步数,默认10。


4.MonkeyDevices API -press 按键 

press(String keycode,dictionary type)

keycode 名,Down、UP 、DOWN_AND_UP

5.MonkeyDevice API-startActivity 

启动应用 startActivity (package+'/'+activity)


6.MonkeyDevice API -touch 点击 

touch(integer x,integer y,integer type)

x坐标值,y坐标值

type:DOWN,UP,DOWN_AND_UP

7.MonkeyDevice API-type 输入 

type(String message)


8.MonkeyDevice API - takeSnapShot 截屏

MonkeyImage takeSnapshot()

MonkeyImage API 具体包含哪些api

1.sameAs 图像对比 

boolean sameAs(MonkeyImage other,float percent) 相似度百分比

2.writetoFile 图像保存 
void writeToFile (String path,String format)

实践

#!/usr/bin/python
#-*- UTF-8 -*-

from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
#连接设备,设备名adb devices可以获取
device=MonkeyRunner.waitForConnection(3,"192.168.56.101:5555")
#启动App,sleep里单位是秒
device.startActivity("com.jikexueyuan.animationartdemo/com.jikexueyuan.animationartdemo.MainActivity")
MonkeyRunner.sleep(2)
#点击搜索框
device.touch(100,100,"DOWN_AND_UP")
MonkeyRunner.sleep(1)#输入查询词device.type('test')MonkeyRunner.sleep(1)#点击搜索按钮device.touch(400,100,"DOWN_AND_UP")MonkeyRuner.sleep(6)#截图image  = device.takeSnapshot()image.writeToFile('./test.png','png')#点击清除按钮device.touch(300,100,"DOWN_AND_UP")MonkeyRunner.sleep(3)

执行脚本命令monkeyrunner mook.py
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值