转自http://blog.csdn.net/swordgirl2011/article/details/50918665
1. Monkey脚本格式
1.1 Monkey脚本主要包含两部分,一部分是头文件信息,一部分是具体的monkey命令。
1.2 文件格式如下,e .g.
//头文件,控制monkey发送消息的参数,e.g.
- type = raw events
- count = 5
- speed = 1.0
- //下面为monkey命令
- start data >>
- 具体的monkey脚本内容
2. Monkey脚本API简介
LaunchActivity(pkg_name, cl_name): 启动应用的Activity。参数:包名和启动的Activity。
Tap(x, y, tapDuration): 模拟一次手指单击事件。参数:x,y为控件坐标,tapDuration为点击的持续时间,此参数可省略。
DispatchPress(keyName): 按键。参数: keycode。 详细的Android Keycode列表:Android Keycode列表。
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): 结束获取帧率。
3. 编写monkey脚本,简单的点击应用,并且登陆
3.1 编写脚本
- #头文件信息
- type=raw events
- count=10
- speed=1.0
- start data >>
- #具体的脚本内容
- LaunchActivity(com.example.package,com.example.ui.SplashActivity)
- Tap(972,1818)
- UserWait(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)
3.2 执行脚本
- adb push monkey.txt /sdcard/
- 103 KB/s (490 bytes in 0.004s)
- adb shell
- shell@pisces:/ $ monkey -f /sdcard/monkey.txt -v 1
3.3 执行日志
- :Monkey: seed=1459495139508 count=1
- :IncludeCategory: android.intent.category.LAUNCHER
- :IncludeCategory: android.intent.category.MONKEY
- Replaying 0 events with speed 0.0
- :Switch: #Intent;action=android.intent.action.MAIN;category=android.intent.category.LAUNCHER;launchFlags=0x10200000;component=package/.ui.SplashActivity;end
- // Allowing start of Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=packagename/.ui.SplashActivity } in package xxx
- :Sending Touch (ACTION_DOWN): 0:(972.0,1818.0)
- :Sending Touch (ACTION_UP): 0:(972.0,1818.0)
- // Allowing start of Intent { cmp=packagename/.ui.mine.MineActivity } in package xxx
- :Sending Touch (ACTION_DOWN): 0:(540.0,607.0)
- :Sending Touch (ACTION_UP): 0:(540.0,607.0)
- :Sending Flip keyboardOpen=false
- // Allowing start of Intent { cmp=pacakgename/.ui.usercenter.UserLoginActivity } in package xxx
- // Shell command input text 1234567 status was 0
- :Sending Touch (ACTION_DOWN): 0:(636.0,859.0)
- :Sending Touch (ACTION_UP): 0:(636.0,859.0)
- :Sending Touch (ACTION_DOWN): 0:(388.0,895.0)
- :Sending Touch (ACTION_UP): 0:(388.0,895.0)
- // Shell command input text 123456 status was 0
- :Sending Flip keyboardOpen=false
- :Sending Touch (ACTION_DOWN): 0:(540.0,1104.0)
- :Sending Touch (ACTION_UP): 0:(540.0,1104.0)
- Events injected: 19
- :Sending rotation degree=0, persist=false
- :Dropped: keys=0 pointers=0 trackballs=0 flips=0 rotations=0
- ## Network stats: elapsed time=9245ms (0ms mobile, 0ms wifi, 9245ms not connected)
- // Monkey finished
4. Monkey服务器命令
4.1 常用命令
命令 | 命令格式 | 说明 |
flip | flip open、filp closed | 打开或关闭键盘 |
touch | touch [down|up|move] [x] [y] | 模拟手指按下边界的操作 |
trackball | trackball [dx] [dy] | 发送一个轨迹球操作事件 |
key | key [down|up] [keycode] | 发送一个按键事件 |
sleep | sleep [暂停时间] | 服务器暂停一点时间 |
type | type [String] | 向当前应用发送一个字符串 |
wake | wake | 唤醒设备 |
tap | tap [x] [y] | 发送一个单击坐标的命令 |
press | press [keycode] | 发送一个按键 |
deferreturn | deferrenturn [event] [timepue] [command] | 执行一个命令在指定延时之内等待一个event事件 |
4.2 查询命令:
命令 | 命令格式 | 说明 |
listvar | listvar | 列出可查看的系统变量 |
getvar | getvar [variable name] | 获取一个系统变量的值 |
listviews | listviews | 列出待测应用里的所有视图的ID |
getrootview | getrootview | 获取待测试应用的最上层控件的ID |
getviewswithtext | getviewswithtext [text] | 返回所有包含指定文本的控件ID |
queryview | queryview viewed [id] [command] queryview accessibilityids [id1] [id2] [command] | 根据指定ID来查询控件 |
5. Monkey服务器命令使用示例
5.1 打开手机端口:
![](https://img-blog.csdn.net/20160320001342997?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
5.2 转发端口,并建立连接:
![](https://img-blog.csdn.net/20160320001520809?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
5.3 同时按control + ], 回车即可开始输入命令 e.g:
- //唤醒屏幕
- wake
- OK
-
- //点击指定坐标
- tap 500 600
- OK
-
- //输入字符串
- type 1000
- OK
-
- //点击按钮
- press KEYCODE_VOLUME_UP
- OK
-
- //列出系统变量
- listvar
- OK:am.current.action am.current.categories am.current.comp.class am.current.comp.package am.current.data am.current.package build.board build.brand build.cpu_abi build.device build.display build.fingerprint build.host build.id build.manufacturer build.model build.product build.tags build.type build.user build.version.codename build.version.incremental build.version.release build.version.sdk clock.millis clock.realtime clock.uptime display.density display.height display.width monkey.version
-
- //获取指定系统变量值,e.g.分辨率
- getvar display.height
- OK:1920
- getvar display.width
- OK:1080
-
- //列出待测应用里的所有视图id
- listviews
- OK:action0 action_bar action_bar_activity_content action_bar_container action_bar_root action_bar_spinner action_bar_subtitle action_bar_title action_context_bar action_divider action_menu_divider action_menu_presenter action_mode_bar action_mode_bar_stub action_mode_close_button action_settings activity_chooser_view_content adjust_height adjust_width alertTitle always avatar beginning buttonPanel cancel_action checkbox checkoutWebView chronometer collapseActionView container contentPanel currentUrl custom customPanel decor_content_parent default_activity_button desktopText desktopView desktop_graphic disableHome drawerLayout drawerPane edit_query editbox_shape end end_padder expand_activities_button expanded_menu home homeAsUp icon ifRoom image info install_update line1 line3 listMode list_item mainView media_actions middle multiply navAvatar navDesktop navList never none normal not_now parentPanel powerLantern profileBox progress_circular progress_horizontal radio receive_lantern screen scrollView search_badge search_bar search_button search_close_btn search_edit_frame search_go_btn search_mag_icon search_plate search_src_text search_voice_btn select_dialog_listview sendBtn sendEmail separator settings_icon shortcut showCustom showHome showTitle split_action_bar src_atop src_in src_over status_bar_latest_event_content status_image status_layout_root submit_area tabMode text text2 textSpacerNoButtons time title title_template toast_off toast_on topPanel up update_available update_icon updater updater_view useLogo userName versionNum withText wrap_content
备注:
Monkey源代码地址: https://github.com/android/platform_development/tree/master/cmds/monkey