MonkeyRunner強大的功能之一即是容許用戶自由錄製須要的腳本,錄製和回放須要兩個腳本文件 monkey_recorder.py和monkey_playback.pypython
首先來看 monkey_record.py android
1 #!/usr/bin/env monkeyrunner 2 # Copyright 2010, The Android Open Source Project# 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at# 6 # http://www.apache.org/licenses/LICENSE-2.0# 7 # Unless required by applicable law or agreed to in writing, software 8 # distributed under the License is distributed on an "AS IS" BASIS, 9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 # See the License for the specific language governing permissions and 11 # limitations under the License. 12 from com.android.monkeyrunner import MonkeyRunner as mr 13 from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder 14 device = mr.waitForConnection() 15 recorder.start(device)
鏈接手機,在 cmd 命令行運行 monkeyrunner monkey_record.py,會彈出一個MonkeyRecord窗口界面該窗口的功能:express
一、能夠自動顯示手機當前的界面apache
二、自動刷新手機的最新狀態app
三、點擊手機界面便可對手機進行操做,同時會反應到真機,並且會在右側插入操做腳本less
4:、wait: 用來插入下一次操做的時間間隔,點擊後便可設置時間,單位是秒測試
Press a Button:用來肯定須要點擊的按鈕,包括menu、home、search,以及對按鈕的press、down、up屬性ui
Type Something:用來輸入內容到輸入框this
Fling:用來進行拖動操做,能夠向上、下、左、右,以及操做的範圍spa
Export Actions:用來導出腳本,不須要後綴名,也能夠添加後綴名.mr
Refresh Display:用來刷新手機界面,估計只有在斷開手機後,從新鏈接時纔會用到
而後是須要monkey_playback.py文件
#!/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()
在cmd 窗口運行 monkeyrunner monkey_playback.py test1
若是遇到不能回放的問題,須要關閉當前錄製時的cmd窗口,從新打開cmd窗口執行回放操做
備註:以上路徑都是絕對路徑,錄製後的腳本能夠進行二次更改,並且每一步操做須要有時間間隔,這樣才能保證測試的正確性