一、Monkey脚本主要包含两部分,一部分是头文件信息,一部分是具体的monkey命令。

  文件格式如下:

   type = raw events

   count = 5

   speed = 1.0

   start data >>

   //下面为monkey命令 

二、monkey API 

  LaunchActivity(pkg_name, cl_name):启动应用的Activity。参数:包名和启动的Activity。

  Tap(x, y, tapDuration): 模拟一次手指单击事件。参数:x,y为控件坐标,tapDuration为点击的持续时间,此参数可省略。

  DispatchPress(keyName): 按键。参数: keycode。 详细的Android Keycode列表: http://blog.csdn.net/feizhixuan46789/article/details/16801429

  RotateScreen(rotationDegree, persist): 旋转屏幕。 参数:rotationDegree为旋转角度, e.g. 1代表90度;persist表示旋转之后是否固定,0表示旋转后恢复,非0则表示固定不变。

  DispatchFlip(true/false): 打开或者关闭软键盘。

  LongPress(): 长按2秒。

  PressAndHold(x, y, pressDuration): 模拟长按事件。

  DispatchString(input): 输入字符串。

  Drag(xStart, yStart, xEnd, yEnd, stepCount): 用于模拟一个拖拽操作。

  PinchZoom(x1Start, y1Start, x1End, y1End, x2Start, y2Start, x2End, y2End, stepCount): 模拟缩放手势。

  UserWait(sleepTime): 休眠一段时间

  DeviceWakeUp(): 唤醒屏幕。

  PowerLog(power_log_type, test_case_status): 模拟电池电量信息。

  WriteLog(): 将电池信息写入sd卡。

  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): 结束获取帧率。

三、脚本编写

  1. 编写脚本,将此脚本保存为monkey.txt,如下:

    

#头文件信息
type=raw events
count=10
speed=1.0
start data >>
 
#具体的脚本内容
LaunchActivity(com.example.package,com.example.ui.SplashActivity)
Tap(972,1818)
erWait(2000)
Tap(540,607)
DispatchFlip(false)
DispatchString(1234567)
Tap(636,859)
UserWait(2000)
Tap(388,895)
UserWait(1000)
DispatchString(123456)
DispatchFlip(false) UserWait(2000)
Tap(540,1104)

2.执行脚本

adb push monkey.txt /sdcard/
adb shell monkey -f /sdcard/monkey.txt -v 1

四、补充知识

  1.怎么获取某个应用的包名和activity名

    usb调试模式下,打开该应用,cmd中执行adb shell dumpsys activity | find "mFocusedActivity"

    

 

posted on 2018-07-31 15:44  我是学习的  阅读(504)  评论(0编辑  收藏  举报