Monkey工具脚本功能详解

●Monkey脚本

adb shell monkey -f <script> 1

参考源码:https://android.googlesource.com/platform/development/+/mastercmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java

●Monkey脚本主要命令

•DispatchPointer  相当于把手按在某一个点上面

•DispatchPress [keycode] 向系统发送固定的按键(keycode)事件

 Android KEYCODE键值对应大全:  http://blog.csdn.net/xl_tang/article/details/8720953

•LaunchActivity   启动应用

•UserWait   让脚本的执行暂停一段时间,做一个等待操作

•RotateScreen  翻转屏幕

•Tap   单击事件

●Monkey脚本编写实例(计算器)

type=raw events 

count = 10 

speed = 1.0 //执行速度

start data >>

//cmd里面启动uiautomatorviewer,在虚拟机打开计算器,点击如图按钮,获取包名

LaunchActivity(com.android.calculator2, com.android.calculator2.Calculator)

#touch 9

DispatchPointer(0,0,0,600,700,0,0,0,0,0,0,0)  //按下的操作

//解释一下横坐标600纵坐标700是怎么来的?

X在543和810之间; Y在602和896间

DispatchPointer(0,0,1,600,700,0,0,0,0,0,0,0)  //弹起的操作

UserWait(1500) //等待时间1500ms

#touch 6

DispatchPress(KEYCODE_6)

UserWait(1500)

#touch +

DispatchPress(KEYCODE_PLUS)

UserWait(1500)

#touch 7

Tap(200,700,50) //200是X,700是Y,50ms是时间

UserWait(1500)

#touch =

DispatchPress(KEYCODE_EQUALS)

UserWait(1500)

RotateScreen(2,1)  //翻转屏幕2个参数:角度(0是0度,1是90度,2是180度,3是270度)、是否保留(0和1)

UserWait(500)

•把此脚本放到电脑本地用户目录下面命名monkey.script,接下来上载到设备上(adb push monkey.script /data/temp/monkey.script),然后可以执行Monkey脚本(adb shell monkey -f /data/temp/monkey.script 1)

原文地址:https://www.cnblogs.com/ljf-hero/p/6625304.html