Monkey脚本API简介
LaunchActivity(pkg_name, cl_name):启动应用的Activity。参数:包名和启动的Activity。
Tap(x, y, tapDuration): 模拟一次手指单击事件。参数:x,y为控件坐标,tapDuration为点击的持续时间,此参数可省略。
UserWait(sleepTime): 休眠一段时间
DispatchPress(keyName): 按键。参数: keycode。
RotateScreen(rotationDegree, persist): 旋转屏幕。 参数:rotationDegree为旋转角度, e.g. 1代表90度;persist表示旋转之后是否固定,0表示旋转后恢复,非0则表示固定不变。
DispatchString(input): 输入字符串。
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): 结束获取帧率。
日志级别
adb shell monkey -v <event-count>
-v:打印出日志信息,每个-v将增加反馈信息的级别。-v越多日志信息越详细,不过目前最多支持3个-v,即:
1.执行指定脚本
adb shell monkey -f <scriptfile> <event-count>
eg:
adb shell monkey -f /mnt/sdcard/test1
2.伪随机数生成种子值
使用 -s命令可以重复执行之前的伪随机操作。本身每次执行伪随机事件操作也会默认生成一个seed值
adb shell monkey -s <seed> <event-count>
eg:
adb shell monkey -s 666 100
3.设置间隔 如果你希望在每一个指令之间加上固定的间隔时间,可以用--throttle(注意,前面是--)命令。
adb shell monkey --throttle <milliseconds>
eg:
adb shell monkey --throttle 3000 5
--throttle:后面接时间,单位为ms(<milliseconds>),表示事件之间的固定延迟(即执行每一个指令间隔的时间),若不接该选项,monkey将不会延迟。
4.调整触摸事件百分比
如果你希望调整触摸事件的百分比,记住使用--pct-touch。
adb shell monkey --pct-touch
eg:
adb shell monkey -v -v --pct-touch 100 200
<percent>--pct-touch:后面接触摸事件百分比
注意:触摸事件不单单是按键,它泛指发生在某一位置的一个down-up事件。
5.调整手势事件百分比
adb shell monkey --pct-motion
eg:
adb shell monkey -v -v --pct-motion 100 200
6.调整应用启动事件的百分比
如果你希望调整应用启动事件的百分比,记住使用--pct-app-switch。
adb shell monkey --pct-appswtich <percent>
--pct-appswitch:后面接应用启动事件百分比。
应用启动事件(即activity launches)俗称打开应用,通过调用startActivity()方法最大限度地开启该package下的所有应用。
7.调整屏幕旋转事件百分比
adb shell monkey --pct-rotation <percent>
--pct-rotation 后面接屏幕旋转事件的比例值。
1.包约束
-p:后面接一个或多个包名(<allowed-package-name>),如果应用需要访问其他包里的Activity,那相关的包也需要在此同时指定。如果不指定任何包,monkey将允许系统启动全部包里的Activity。
adb shell monkey -p <allowed-package-name> <event-count>
eg:
adb shell monkey -p com.tal.kaoyan 500
adb shell monkey -p com.tal.kaoyan -p com.tencent.mm 500
2.activity类约束
如果你希望将monkey限制在一个或几个类别中,使用如下命令:
adb shell monkey -c <main-category> <event-count>
以下命令表示运行Intent.CATEGORY_LAUNCHER类别的Activity并发送1000个随机事件。
adb shell monkey -c Intent.CATEGORY_LAUNCHER 1000
----------------------------------------------------------------------------
type = raw events
count = 1 #执行一次
speed = 1.0
start data >>
LaunchActivity(com.tal.kaoyan,com.tal.kaoyan.ui.activity.SplashActivity) #包名,LaunchActivity
UserWait(2000) #等待2秒
Tap(xxx,xxx,1000) #三个参数分别为x坐标 y坐标 按压时间 安卓手机可以在开发者选项中打开坐标显示
UserWait(2000) #等待2秒
Tap(xxx,xxx,1000)
DispatchString(xxx) #输入账号
UserWait(2000) #等待2秒
Tap(xxx,xxx,1000)
DispatchString(xxx) #输入账号
UserWait(2000) #等待2秒
Tap(xxx,xxx,1000) #点击登录
UserWait(2000) #等待2秒
不知道如何查看包名与LaunchActivity参考https://blog.csdn.net/u012002125/article/details/81322001
#------------------------------------------------------------------------------------------------------------------------
#以下为如何在手机上执行操作
1、将文件发送到手机上:adb push d:\xxx\xxx.txt /sdcard
2、执行脚本:adb shell monkey -f /sdcard/xxx.txt -v -v 1 #-v -v日志级别 1为执行1次