繼上一篇monkeyrunner環境搭建:http://www.cnblogs.com/zh-ya-jing/p/4351245.html 以後,咱們能夠進一步學習monkeyrunner了。html
我也是剛接觸monkeyrunner不久,對monkeyrunner的腳本錄製功能很感興趣,因此學習一下。沒想到中間遇到不少問題,以前是錄製腳本不經過,再以後是手機鏈接不上,monkeyrunner運行不起來,歸根結底仍是錄製腳本的問題,後向大神請教,可算是能成功錄製腳本了。python
不知道出於什麼目的,google把monkeyrunner的腳本錄製功能雪藏了,須要從Android源碼中才能將其發掘出來。monkey_recorder.py是用來錄製在設備上的操做病生成腳本的,monkey_playback.py則用來回放腳本。android
新建monkey_recorder.py文件,代碼以下:express
#!/usr/bin/env monkeyrunner # Copyright 2010, The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from com.android.monkeyrunner import MonkeyRunner as mr from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder device = mr.waitForConnection() recorder.start(device)
新建monkey_playback.py文件,代碼以下:apache
#!/usr/bin/env monkeyrunner # Copyright 2010, The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import sys from com.android.monkeyrunner import MonkeyRunner # The format of the file we are parsing is very carfeully constructed. # Each line corresponds to a single command. The line is split into 2 # parts with a | character. Text to the left of the pipe denotes # which command to run. The text to the right of the pipe is a python # dictionary (it can be evaled into existence) that specifies the # arguments for the command. In most cases, this directly maps to the # keyword argument dictionary that could be passed to the underlying # command. # Lookup table to map command strings to functions that implement that # command. CMD_MAP = { 'TOUCH': lambda dev, arg: dev.touch(**arg), 'DRAG': lambda dev, arg: dev.drag(**arg), 'PRESS': lambda dev, arg: dev.press(**arg), 'TYPE': lambda dev, arg: dev.type(**arg), 'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg) } # Process a single file for the specified device. def process_file(fp, device): for line in fp: (cmd, rest) = line.split('|') try: # Parse the pydict rest = eval(rest) except: print 'unable to parse options' continue if cmd not in CMD_MAP: print 'unknown command: ' + cmd continue CMD_MAP[cmd](device, rest) def main(): file = sys.argv[1] fp = open(file, 'r') device = MonkeyRunner.waitForConnection() process_file(fp, device) fp.close(); if __name__ == '__main__': main()
鏈接真機或模擬器,執行如下命令:windows
$monkeyrunner monkey_recorder.pyapp
執行完畢以後,monkeyrunner會打開一個窗口,以下所示。less
它會不停地從設備上抓取最新界面,你能夠直接在左邊的屏幕截圖上單擊圖標來模擬觸控操做方式,不要直接操做真機或模擬器,不然沒法錄製腳本。操做的同時右邊會實時顯示錄製的腳本,單擊「Type something」按鈕輸入字符串,單擊「Fling」按鈕模擬滑動手勢。當操做錄製完畢後,單擊「Export Actions」按鈕,將腳本保存到指定目錄,好比test.mr,關閉monkeyrunner運行窗口。學習
將錄製好的腳本傳給monkey_playback.py文件就能夠回放了,執行以下命令:ui
$monkeyrunner monkey_playback.py test.mr
假如回放過程出錯,有多是真機或者模擬器反應比較慢,兩次操做之間間隔時間過短,因此建議兩次操做之間加些wait,即每次操做以後點擊「wait」按鈕,增長等待時間。
必須把monkey_recorder.py,monkey_playback.py和錄製的腳本test.mr放入"*\adt-bundle-windows-x86-20130917\sdk\tools"目錄下,並且運行.py文件都使用絕對路徑。
能夠參考以下博客:http://blog.csdn.net/zm2714/article/details/7980634