官方未公开的Monkey功能-Monkey Server

前面的内容请参见Android应用自动化测试-提纲
前篇介绍了Monkey的进阶用法,Monkey的脚本功能。这篇继续介绍另一个在官方介绍文档中没有说明的功能,Monkey的Server功能。Monkey的Server功能提供了远程访问设备并控制设备执行的能力。在monkey的参数中,我们可以看到有一个--port的参数,它提供的就是Monkey连接server,并控制设备执行的功能。

还是来到Monkey的源码,在Monkey的源码目录下有一个Readme.network.txt的说明文档,就是说明这个server功能的使用的。

SIMPLE PROTOCOL FOR AUTOMATED NETWORK CONTROL

The Simple Protocol for Automated Network Control was designed to be a
low-level way to programmability inject KeyEvents and MotionEvents
into the input system.  The idea is that a process will run on a host
computer that will support higher-level operations (like conditionals,
etc.) and will talk (via TCP over ADB) to the device in Simple
Protocol for Automated Network Control.  For security reasons, the
Monkey only binds to localhost, so you will need to use adb to setup
port forwarding to actually talk to the device.

INITIAL SETUP

Setup port forwarding from a local port on your machine to a port on
the device:

$ adb forward tcp:1080 tcp:1080

Start the monkey server

$ adb shell monkey --port 1080

Now you're ready to run commands

COMMAND LIST

Individual commands are separated by newlines.  The Monkey will
respond to every command with a line starting with OK for commands
that executed without a problem, or a line starting with ERROR for
commands that had problems being run.  For commands that return a
value, that value is returned on the same line as the OK or ERROR
response.  The value is everything after (but not include) the colon
on that line.  For ERROR values, this could be a message indicating
what happened.  A possible example:

key down menu
OK
touch monkey
ERROR: monkey not a number
getvar sdk
OK: donut
getvar foo
ERROR: no such var

The complete list of commands follows:

key [down|up] keycode

This command injects KeyEvent's into the input system.  The keycode
parameter refers to the KEYCODE list in the KeyEvent class
(http://developer.android.com/reference/android/view/KeyEvent.html).
The format of that parameter is quite flexible.  Using the menu key as
an example, it can be 82 (the integer value of the keycode),
KEYCODE_MENU (the name of the keycode), or just menu (and the Monkey
will add the KEYCODE part).  Do note that this last part doesn't work
for things like KEYCODE_1 for obvious reasons.

Note that sending a full button press requires sending both the down
and the up event for that key

touch [down|up|move] x y

This command injects a MotionEvent into the input system that
simulates a user touching the touchscreen (or a pointer event).  x and
y specify coordinates on the display (0 0 being the upper left) for
the touch event to happen.  Just like key events, touch events at a
single location require both a down and an up.  To simulate dragging,
send a "touch down", then a series of "touch move" events (to simulate
the drag), followed by a "touch up" at the final location.

trackball dx dy

This command injects a MotionEvent into the input system that
simulates a user using the trackball. dx and dy indicates the amount
of change in the trackball location (as opposed to exact coordinates
that the touch events use)

flip [open|close]

This simulates the opening or closing the keyboard (like on dream).

wake

This command will wake the device up from sleep and allow user input.

tap x y
The tap command is a shortcut for the touch command.  It will
automatically send both the up and the down event.

press keycode

The press command is a shortcut for the key command.  The keycode
paramter works just like the key command and will automatically send
both the up and the down event.

type string

This command will simulate a user typing the given string on the
keyboard by generating the proper KeyEvents.

listvar

This command lists all the vars that the monkey knows about.  They are
returned as a whitespace separated list.

getvar varname

This command returns the value of the given var.  listvar can be used
to find out what vars are supported.

quit

Fully quit the monkey and accept no new sessions.

done

Close the current session and allow a new session to connect

OTHER NOTES

There are some convenience features added to allow running without
needing a host process.

Lines starting with a # character are considered comments.  The Monkey
eats them and returns no indication that it did anything (no ERROR and
no OK).

You can put the Monkey to sleep by using the "sleep" command with a
single argument, how many ms to sleep.

我们提取其中主要的用法:
首先需要利用adb的forward命令,被本地端口和设备端口进行映射

adb forward tcp:1080 tcp:1080

然后利用Monkey的--port将Monkey Server开放在1080端口上

adb shell monkey --port 1080

这时我们通过telnet连接本机的1080端口,即可访问到设备上的Monkey server,并且执行Server中的相关指令。

telnet 127.0.0.1 1080

  • key [down|up] keycode -- 指定Keycode的按键事件(分按下、弹起)
  • touch [down|up|move] x y -- 指定坐标的触屏操作(分按下、弹起、移动)
  • trackball dx dy -- 轨迹球操作
  • tap x y -- 指定坐标的触屏操作
  • flip [open|close] -- 调用软键盘
  • wake -- 唤醒设备
  • press keycode -- 指定Keycode的按键事件
  • listvar -- 列出所有的系统变量
  • getvar varname -- 获取给定系统变量值
  • quit -- 退出当前连接,且不接受新的连接
  • done -- 退出当前连接,但可以接受新的连接

命令执行成功,会返回OK的响应。如果执行错误则返回error command.
注意操作退出telnet前,需要执行done指令,否则再次连接,会报端口已占用的错误。只能重启设备以释放端口。
操作示例:


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容

  • 单身就像是一壶酒, 醉了,醒了。 孤独就像是一颗星, 闪了,灭了。 寂寞就像是一轮月, 升了,落了。 世上的路万千...
    花笑远阅读 255评论 1 4
  • TCP/IP协议场景 说道TCP/IP协议想起了计算机网络书中举了一个红蓝军的通讯的问题。其场景如下: 红蓝军需要...
    shawnxjf阅读 1,160评论 0 0
  • 昨天晚上,小六的结营仪式还是在晚上9点如约而至,虽然十分不舍,但还是得接受事实。当时,并没有什么心情去看谁获了奖,...
    顾小宝阅读 540评论 0 3
  • 机缘巧合的,周末在金宝汇又体验了把高大上的美甲生活。对,没错,又是金宝汇,就是金宝汇。我想说我爱金宝汇。吃喝玩乐一...
    竞走的蜗牛阅读 352评论 0 0