Monkey详解

一、默认运行方式:

  adb shell monkey -p package.name -v 30

其中:  可以继续添加一个或者两个 -v 参数, -v参数越多,输出的日志越详细

         最后的数字表示,触发的事件次数

为了更好的查看日志,可以将输出的日志信息重定向到文件中

如:adb shell monkey -p package.name -v 30 log.txt

二、脚本方式

Android 的monkey test 工具提供了 -f scriptfile 参数,可以指定 test 脚本。在 monkey 的源码 MonkeySourceScript.java 中有一小段注释,里面给了一个不到 10 行例子:

  

monkey中提供的函数如下:

15 DispatchPointer(long downTime,  long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int device, int edgeFlags)  

16 DispatchTrackball(long downTime,  long eventTime, int action,  float x, float y, float pressure, float size, int metaState,  float xPrecision, float yPrecision, int device, int edgeFlags)    

17 DispatchKey(long downTime, long eventTime, int action, int code, int repeat, int metaState, int device, int scancode)     

18 DispatchFlip(boolean keyboardOpen)    

19 DispatchPress(int keyCode)    

20 LaunchActivity(String pkg_name, String cl_name)  

21 UserWait(long sleeptime)    

22 LongPress(int keyCode) 

首先本地编写需要的测试的事件 命名为 monkey.script (文件格式无要求),将文件push到手机或模拟器的sdcard中

如: adb push  lujing/monkey.script  /sdcard/

然后执行脚本:

       adb shell monkey -v -f /sdcard/monkey.script

 

附 Example:

type= user
count= 49
speed= 1.0
start data >>
LaunchActivity(com.example.android.notepad, com.example.android.notepad.NotesList)
DispatchPress(KEYCODE_DPAD_DOWN)
LongPress(KEYCODE_DOWN)
DispatchPress(KEYCODE_BACK)

其中 type值可以任意,源码中没有对该值做任何处理。

       count值,在此无效,还是需要在命令行输入需要执行的次数。因为命令行的count值是必填项

 

Monkey脚本支持的命令

脚本命令必须为小写字母

脚本命令以回车符作为一条命令的结束

键盘按下/弹起

key [down|up] keycode

这个命令模拟一次键盘输入。

keycode参数值详见KeyEvent类的KEYCODE列表。这个参数的格式很灵活,例如模拟菜单按钮可以使用82(菜单按钮的键值),也可以使用 KEYCODE_MENU(键值的名称,必须严格都是大写字母),还可以使用menu(Monkey程序会自动添加KEYCODE部分,此时并不区分大小 )

注意一次完整的敲击(press)操作是一个按下(key down)和弹起(key up)的组合

触摸按下/弹起/移动

touch [down|up|move] y

This command injects MotionEvent into the input system that simulates user touching the touchscreen (or pointer event). and specify coordinates on the display (0 being the upper left) for the touch event to happen. Just like key events, touch events at single location require both down and an up. To simulate dragging, send touch downthen series of touch move” events (to simulate the drag), followed by touch up” at the final location.

滚动轨迹球

trackball dx dy

This command injects MotionEvent into the input system that simulates user using the trackball. dx and dy indicates the amount of change in the trackball location (as opposed to exact coordinates that the touch events use)

打开/关闭实体键盘

flip [open|close]

模拟一次实体键盘的打开/关闭。

在没有实体键盘的手机上此命令无效。

唤醒设备(点亮屏幕)

wake

这个命令将唤醒设备,并允许用户输入。

如果设备键盘已锁,这个命令并不能解锁键盘

屏幕点击

tap y

这个命令模拟一次屏幕点击。

这个命令就是touch downtouch up命令的一次组合。

敲击键盘

press keycode

这个命令模拟一次键盘敲击。

这个命令就是key downkey up命令的一次组合。

键入字符串

type string

这个命令模拟键入一个字符串

该命令会完全模拟每一个字符的键盘事件

列出环境变量

listvar

该命令将列出Monkey的所有环境变量。

返回值为空格分隔的字符串。

参考:Nexus SMonkey的环境变量

am.current.action

am.current.categories

am.current.comp.class

am.current.comp.package

am.current.data

am.current.package

build.board

build.brand

build.cpu_abi

build.device

build.display

build.fingerprint

build.host

build.id

build.manufacturer

build.model

build.product

build.tags

build.type

build.user

build.version.codename

build.version.incremental

build.version.release

build.version.sdk

clock.millis

clock.realtime

clock.uptime

display.density 设备屏幕密度

display.height 设备纵向像素

display.width 设备横向像素

获取环境变量值

getvar varname

该命令用于获取指定的环境变量的值。

通过listvar命令获取支持的环境变量列表。

退出Monkey

quit

完全退出Monkey

Monkey不再接受新的连接。

结束当前会话

done

结束当前会话。

Monkey还可以接受新的连接。

休眠

sleep ms

使Monkey进入休眠一段时间,参数为整数,单位毫秒

注释

#

#开头的语句会被Monkey当做注释。发送这样的命令Monkey不会返回错误也不会返回OK。 

Monkey脚本

Monkey脚本支持的命令

脚本命令必须为小写字母

脚本命令以回车符作为一条命令的结束

键盘按下/弹起

key [down|up] keycode

这个命令模拟一次键盘输入。

keycode参数值详见KeyEvent类的KEYCODE列表。这个参数的格式很灵活,例如模拟菜单按钮可以使用82(菜单按钮的键值),也可以使用 KEYCODE_MENU(键值的名称,必须严格都是大写字母),还可以使用menu(Monkey程序会自动添加KEYCODE部分,此时并不区分大小 )

注意一次完整的敲击(press)操作是一个按下(key down)和弹起(key up)的组合

触摸按下/弹起/移动

touch [down|up|move] y

This command injects MotionEvent into the input system that simulates user touching the touchscreen (or pointer event). and specify coordinates on the display (0 being the upper left) for the touch event to happen. Just like key events, touch events at single location require both down and an up. To simulate dragging, send touch downthen series of touch move” events (to simulate the drag), followed by touch up” at the final location.

滚动轨迹球

trackball dx dy

This command injects MotionEvent into the input system that simulates user using the trackball. dx and dy indicates the amount of change in the trackball location (as opposed to exact coordinates that the touch events use)

打开/关闭实体键盘

flip [open|close]

模拟一次实体键盘的打开/关闭。

在没有实体键盘的手机上此命令无效。

唤醒设备(点亮屏幕)

wake

这个命令将唤醒设备,并允许用户输入。

如果设备键盘已锁,这个命令并不能解锁键盘

屏幕点击

tap y

这个命令模拟一次屏幕点击。

这个命令就是touch downtouch up命令的一次组合。

敲击键盘

press keycode

这个命令模拟一次键盘敲击。

这个命令就是key downkey up命令的一次组合。

键入字符串

type string

这个命令模拟键入一个字符串

该命令会完全模拟每一个字符的键盘事件

列出环境变量

listvar

该命令将列出Monkey的所有环境变量。

返回值为空格分隔的字符串。

参考:Nexus SMonkey的环境变量

am.current.action

am.current.categories

am.current.comp.class

am.current.comp.package

am.current.data

am.current.package

build.board

build.brand

build.cpu_abi

build.device

build.display

build.fingerprint

build.host

build.id

build.manufacturer

build.model

build.product

build.tags

build.type

build.user

build.version.codename

build.version.incremental

build.version.release

build.version.sdk

clock.millis

clock.realtime

clock.uptime

display.density 设备屏幕密度

display.height 设备纵向像素

display.width 设备横向像素

获取环境变量值

getvar varname

该命令用于获取指定的环境变量的值。

通过listvar命令获取支持的环境变量列表。

退出Monkey

quit

完全退出Monkey

Monkey不再接受新的连接。

结束当前会话

done

结束当前会话。

Monkey还可以接受新的连接。

休眠

sleep ms

使Monkey进入休眠一段时间,参数为整数,单位毫秒

注释

#

#开头的语句会被Monkey当做注释。发送这样的命令Monkey不会返回错误也不会返回OK

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值