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

前面的內(nèi)容請參見Android應(yīng)用自動化測試-提綱火鼻。
前篇介紹了Monkey的進階用法,Monkey的腳本功能。這篇繼續(xù)介紹另一個在官方介紹文檔中沒有說明的功能民晒,Monkey的Server功能芙贫。Monkey的Server功能提供了遠程訪問設(shè)備并控制設(shè)備執(zhí)行的能力搂鲫。在monkey的參數(shù)中,我們可以看到有一個--port的參數(shù)磺平,它提供的就是Monkey連接server魂仍,并控制設(shè)備執(zhí)行的功能。

還是來到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命令菠劝,被本地端口和設(shè)備端口進行映射

adb forward tcp:1080 tcp:1080

然后利用Monkey的--port將Monkey Server開放在1080端口上

adb shell monkey --port 1080

這時我們通過telnet連接本機的1080端口赊舶,即可訪問到設(shè)備上的Monkey server,并且執(zhí)行Server中的相關(guān)指令赶诊。

telnet 127.0.0.1 1080

  • key [down|up] keycode -- 指定Keycode的按鍵事件(分按下笼平、彈起)
  • touch [down|up|move] x y -- 指定坐標(biāo)的觸屏操作(分按下、彈起甫何、移動)
  • trackball dx dy -- 軌跡球操作
  • tap x y -- 指定坐標(biāo)的觸屏操作
  • flip [open|close] -- 調(diào)用軟鍵盤
  • wake -- 喚醒設(shè)備
  • press keycode -- 指定Keycode的按鍵事件
  • listvar -- 列出所有的系統(tǒng)變量
  • getvar varname -- 獲取給定系統(tǒng)變量值
  • quit -- 退出當(dāng)前連接出吹,且不接受新的連接
  • done -- 退出當(dāng)前連接,但可以接受新的連接

命令執(zhí)行成功辙喂,會返回OK的響應(yīng)捶牢。如果執(zhí)行錯誤則返回error command.
注意操作退出telnet前,需要執(zhí)行done指令巍耗,否則再次連接秋麸,會報端口已占用的錯誤。只能重啟設(shè)備以釋放端口炬太。
操作示例:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末灸蟆,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子亲族,更是在濱河造成了極大的恐慌炒考,老刑警劉巖可缚,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異斋枢,居然都是意外死亡帘靡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進店門瓤帚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來描姚,“玉大人,你說我怎么就攤上這事戈次⌒保” “怎么了?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵怯邪,是天一觀的道長绊寻。 經(jīng)常有香客問我,道長擎颖,這世上最難降的妖魔是什么榛斯? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮搂捧,結(jié)果婚禮上驮俗,老公的妹妹穿的比我還像新娘。我一直安慰自己允跑,他們只是感情好王凑,可當(dāng)我...
    茶點故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著聋丝,像睡著了一般索烹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上弱睦,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天百姓,我揣著相機與錄音,去河邊找鬼况木。 笑死垒拢,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的火惊。 我是一名探鬼主播求类,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼屹耐!你這毒婦竟也來了尸疆?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎寿弱,沒想到半個月后犯眠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡症革,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年阔逼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片地沮。...
    茶點故事閱讀 38,100評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖羡亩,靈堂內(nèi)的尸體忽然破棺而出摩疑,到底是詐尸還是另有隱情,我是刑警寧澤畏铆,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布雷袋,位于F島的核電站,受9級特大地震影響辞居,放射性物質(zhì)發(fā)生泄漏楷怒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一瓦灶、第九天 我趴在偏房一處隱蔽的房頂上張望鸠删。 院中可真熱鬧,春花似錦少欺、人聲如沸滤灯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽烘贴。三九已至,卻和暖如春撮胧,著一層夾襖步出監(jiān)牢的瞬間桨踪,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工芹啥, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留锻离,地道東北人。 一個月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓叁征,卻偏偏與公主長得像纳账,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子捺疼,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,834評論 2 345

推薦閱讀更多精彩內(nèi)容

  • 單身就像是一壺酒疏虫, 醉了,醒了。 孤獨就像是一顆星卧秘, 閃了呢袱,滅了。 寂寞就像是一輪月翅敌, 升了羞福,落了。 世上的路萬千...
    花笑遠閱讀 288評論 1 4
  • TCP/IP協(xié)議場景 說道TCP/IP協(xié)議想起了計算機網(wǎng)絡(luò)書中舉了一個紅藍軍的通訊的問題蚯涮。其場景如下: 紅藍軍需要...
    shawnxjf閱讀 1,229評論 0 0
  • 昨天晚上,小六的結(jié)營儀式還是在晚上9點如約而至棒旗,雖然十分不舍喘批,但還是得接受事實。當(dāng)時铣揉,并沒有什么心情去看誰獲了獎饶深,...
    顧小寶閱讀 553評論 0 3
  • 機緣巧合的敌厘,周末在金寶匯又體驗了把高大上的美甲生活。對朽合,沒錯额湘,又是金寶匯,就是金寶匯旁舰。我想說我愛金寶匯锋华。吃喝玩樂一...
    競走的蝸牛閱讀 368評論 0 0