一、目的
测试app的稳定性、健壮性
二、说明
【例子】
adb shell monkey -p com.android.calendar 1000 向日历app发动1000次随机事件
【-v】日志级别
adb shell monkey -v -p com.android.calendar 1000
adb shell monkey -v-v -p com.android.calendar 1000
adb shell monkey -v-v-v -p com.android.calendar 1000
【-s】随机种子
adb shell monkey -p com.android.calendar -s 2017 1000
【-p】指定包名(可以多个)
查看已安装的包:
1)adb shell
su root
cd data
cd data
2)adb shell pm list packages
【--throttle】事件间隔
adb shell monkey -p com.android.calendar --throttle 3000 1000
【--pct-touch
【--pct-motion
【--pct-trackball
【--pct-nav
【--pct-majornav
【--pct-syskeys
【--pct-appswitch
【--pct-anyevent
【--hprof】在发送事件序列的前后生成性能分析报告 /data/misc目录下
【--ignore-crashes】忽略应用崩溃
【--ignore-timeout】忽略超时错误
【--ignore-security-exceptions】忽略证书或网络许可错误
【--kill-process-after-error】停止发生错误的进程
【--monitor-native-crashes】本地代码崩溃时系统停止运行
【--wait-dbg】启动后等待调试器与其连接
三、使用
1、向一个坐标点发送手势信息
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)
action 0按下 1抬起 2移动
输入手势密码“1-2-3”
DispatchPointer(0,0,0,155,476,0,0,0,0,0,0,0)
DispatchPointer(0,0,2,155,476,0,0,0,0,0,0,0)
DispatchPointer(0,0,0,357,474,0,0,0,0,0,0,0)
DispatchPointer(0,0,2,357,474,0,0,0,0,0,0,0)
DispatchPointer(0,0,0,547,485,0,0,0,0,0,0,0)
DispatchPointer(0,0,2,547,485,0,0,0,0,0,0,0)
DispatchPointer(0,0,1,547,485,0,0,0,0,0,0,0)
2、向一个坐标点发送跟踪求信息
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)
3、发送按键消息
DispatchKey(long downTime,long eventTime,int action,int code,int repeat,int metaState,int device,int scancode)
输入("Abc")
DispatchKey(0,0,0,29,0,0,0,0)
DispatchKey(0,0,1,29,0,0,0,0)
DispatchKey(0,0,0,30,0,0,0,0)
DispatchKey(0,0,1,30,0,0,0,0)
DispatchKey(0,0,0,31,0,0,0,0)
DispatchKey(0,0,1,31,0,0,0,0)
4、打开或关闭软键盘
DispatchFlip(boolean keyboardOpen)
为true时打开软键盘,为false时关闭软键盘
5、启动任意活动界面
LaunchActivity(String pkg_name,String cl_name)
LaunchActivity(com.yuy.test,com.yuy.test.MainActivity)
6、运行测试用例
LaunchInstrumentation(test_name,runner_name)
test_name测试用例名
runner_name测试用例类名
7、让脚本中断一段时间
UserWait(long sleeptime)
毫秒
8、在设备运行shell命令
RunCmd(cmd)
9、模拟手指单击事件
Tap(x,y,tarDuration)
Tap(100,100,3000)
10、等待5秒
ProfileWait()
11、唤醒设备并解锁
DeviceWakeUp()
12、向Shell输入一个字符串
DispatchString(input)
DispatchString(hello)