日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

monkeyrunner的录制与回放

發(fā)布時間:2025/3/21 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 monkeyrunner的录制与回放 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在monkeyrunner中我們可以錄制對手機的操作事件,新建一文件monkey_recorder.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.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,復制以下代碼:

#!/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

總結

以上是生活随笔為你收集整理的monkeyrunner的录制与回放的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。