monkey

Monkey途径:用户主要是通过adb命令来启动Monkey的,

  • Monkey启动步骤
  1. 连接移动设备:adb connect 127.0.0.1:62001
  2. 连接成功后输入命令adb shell
  3. 进入到指定目录cd /system/bin
  4. 输入 monkey命令看到如下提示则说明启动成功。

usage: monkey [-p ALLOWED_PACKAGE [-p ALLOWED_PACKAGE] ...]

              [-c MAIN_CATEGORY [-c MAIN_CATEGORY] ...]

              [--ignore-crashes] [--ignore-timeouts]

              [--ignore-security-exceptions]

              [--monitor-native-crashes] [--ignore-native-crashes]

              [--kill-process-after-error] [--hprof]

              [--pct-touch PERCENT] [--pct-motion PERCENT]

              [--pct-trackball PERCENT] [--pct-syskeys PERCENT]

              [--pct-nav PERCENT] [--pct-majornav PERCENT]

              [--pct-appswitch PERCENT] [--pct-flip PERCENT]

              [--pct-anyevent PERCENT] [--pct-pinchzoom PERCENT]

              [--pkg-blacklist-file PACKAGE_BLACKLIST_FILE]

              [--pkg-whitelist-file PACKAGE_WHITELIST_FILE]

              [--wait-dbg] [--dbg-no-events]

              [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]

              [--port port]

              [-s SEED] [-v [-v] ...]

              [--throttle MILLISEC] [--randomize-throttle]

              [--profile-wait MILLISEC]

              [--device-sleep-time MILLISEC]

              [--randomize-script]

              [--script-log]

              [--bugreport]

              [--periodic-bugreport]

              COUNT

  • II 强制关闭monkey
  1. adb shell ps 查看全部在运行的进程
  2. 查找出com.android.commands.monkey 进程PID
  3. adb shell kill pid 杀掉monkey进程
III monkey 命令

monkey命令格式如下:

$ adb shell monkey [options] <event-count>

  • [options] 是指monkey可传入的参数,是可选项(如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包)
  • <event-count> 是指随机发送事件数。如:输入100就是执行100个伪随机事件,为必选项。
IV Monkey参数:

1) 参数:-p

参数-p用于约束限制,用此参数指定一个或多个包(Package,即App)。指定

包之后,Monkey将只允许系统启动指定的APP。如果不指定包,Monkey将允许系统启动设备中的所有APP

* 指定一个包 adb shell monkey -p com.example.sellclientapp  100

说明:com.example.sellClientAPP 为包名,100是事件计数(即让Monkey程序模拟100次随机用户事件)。

* 指定多个包adb shell monkey -p com.htc.Weather –p com.htc.pdfreader  -p com.htc.photo.widgets 100

* 不指定包adb shell monkey 100

 说明:Monkey随机启动APP并发送100个随机事件。

* 要查看设备中所有的包,在CMD窗口中执行以下命令

  >adb shell

  #cd data/data

  #ls

2) 参数:-v用于指定反馈信息级别(信息级别就是日志的详细程度),总共分3个级别

日志级别 Level 0 

示例 adb shell monkey -p com.htc.Weather –v 100

说明 缺省值,仅提供启动提示、测试完成和最终结果等少量信息

日志级别 Level 1

示例 adb shell monkey -p com.htc.Weather –v -v 100

说明  提供较为详细的日志,包括每个发送到Activity的事件信息

 

日志级别 Level 2

示例 adb shell monkey -p com.htc.Weather –v -v –v 100

说明  最详细的日志,包括了测试中选中/未选中的Activity信息

 

3) 参数:-s用于指定伪随机数生成器的seed值,如果seed相同,则两次Monkey测试所产生的事件序列也相同的。

* 示例:

Monkey测试1adb shell monkey -p com.htc.Weather –s 10 100

Monkey 测试2adb shell monkey -p com.htc.Weather –s 10 100

   两次测试的效果是相同的,因为模拟的用户操作序列(每次操作按照一定的先后顺序所组 成的一系列操作,即一个序列)是一样的。操作序

列虽   然是随机生成的,但是只要我们指定了相同的Seed值,就可以保证两次测试产生的随机操作序列是完全相同的

 

Monkey自定义脚本实践
VI Monkey脚本API简介
1.1.1.1.1.1 Monkey脚本字典:

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): 输入字符串。

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

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

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

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

LongPress(): 长按2秒。

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.1.1.1.1.1 步骤:

1、 新建txt文件

2、 编写脚本(包名和activity名在aapt中获取)

Type = raw events

Count = 1

Speed = 1.0

Start data >>

LaunchActivity(package名,Activity)

UserWait(2000)

 

Tap(623,892,1000)

UserWait(2000)

 

Tap(811,62,1000)

UserWait(2000)

 

Tap(173,384,1000)

DispathString(zxw1234)

UserWait(1000)

 

Tap(199,3475,1000)

DispathString(zxw123456)

UserWait(1000)

 

Tap(376,621,1000)

UserWait(2000)

 

3、 执行脚本

命令:adb push 脚本文件路径 /sdcard存入手机

      Adb shell monkey –f /sdcard/kyb.txt –v –v 1

VII Monkey日志:
1.1.1.1.1.1 保存在PC

>adb shell monkey [option] <count> >d:\monkey.txt

 

eg

C:\Users\Shuqing>adb shell monkey -v -v 100 >d:\monkeylog.txt

1.1.1.1.1.1 保存在手机上

C:\Users\Shuqing>adb shell

monkey -v 100 >/sdcard/monkeylog.log

注意:不能写成C:\Users\Shuqing>adb shell monkey -f /sdcard/kyb.txt -v 1 > /mnt/sdcard/monkey.log 否则会报错“系统找不到指定的路径”。

1.1.1.1.1.1 标准流和错误流分开保存

·标准流与错误流分开保存,代码如下:

Monkey [option] <count> 1>/sdcard/monkey.txt 2>/sdcard/error.txt

 

C:\Users\Shuqing>adb shell monkey -v 100 1>d:\monkey.log  2>d:\error.log

执行以上命令,Monkey的运行日志和异常日志将被分开保存。此时Monkey的运行日志将被保存在monkey.txt文件中,而异常日志将被保存在D盘下的error.txt中。

posted @ 2019-11-10 17:14  molly,是茉莉呀  阅读(193)  评论(0编辑  收藏  举报