monkeyrunner的录制与回放
在monkeyrunner中我們可以錄制對手機的操作事件,新建一文件monkey_recorder.py,復(fù)制一下代碼:
#!/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 recorderdevice = mr.waitForConnection() recorder.start(device)在命令行中運行:monkeyrunner ??c:\Users\Administrator\Desktop\monkey_recorder.py
注意,一定要寫上絕對路徑,否則運行會出錯
(參見:http://blog.csdn.net/lyhdream/article/details/17011153)
運行后會彈出一個操作的界面:
在使用過程中發(fā)現(xiàn)錄制腳本的工具并不是很強大,有些操作無法錄制,比如長按HOME鍵的操作,返回鍵的操作等等。錄制好后將其保存為一文件,這里彬美有要求文件的格式。
回放腳本的操作,新建一文件monkey_playback.py,復(fù)制以下代碼:
#!/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 pydictrest = eval(rest)except:print 'unable to parse options'continueif cmd not in CMD_MAP:print 'unknown command: ' + cmdcontinueCMD_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()在命令行中運行(我錄制的腳本為aaa):monkeyrunner c:\Users\Administrator\Desktop\monkey_playback.py c:\Users\Administrator\Desktop\aaa
注意前后回放腳本和錄制的腳本的路徑都要使用絕對路徑,否則會出錯。
補充:由于錄制腳本的工具功能有限,但能幫我們完成大部分的腳本的錄制,而使用android提供的monkeyrunner ?api更為靈活,因此我們可以將錄制后的腳本轉(zhuǎn)化為monkeyrunner ?中api提供的操作,將其轉(zhuǎn)換為.py格式的文件,通過適當?shù)男薷奈募?#xff0c;即可以制作一個自動化測試的腳本。
monkeyrunner控制手機:http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980086.html
官方api:http://www.android-doc.com/tools/help/MonkeyDevice.html
總結(jié)
以上是生活随笔為你收集整理的monkeyrunner的录制与回放的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android获取手机内部存储和外部存储
- 下一篇: linux sdkMannger的打开