MonkeyRunner Mac環境 錄製腳本和回放 批量回放

 

一、MonkeyRunner是AndroidSDK自帶的一個東西,在SDK目錄中的tools\bin文件夾中python

 

二、配置環境變量android

編輯環境變量:打開終端輸入:open ~/.bash_profileexpress

將sdk/tools/bin所在路徑添加到環境變量apache

使配置生效終端輸入:~/.zshrcbash

 

三、運行 MonkeyRunner腳本錄製工具app

新建一個python文件:monkeyrunner.pyless

內容以下:工具

#coding=utf-8
import sys
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorderui

device = mr.waitForConnection()
recorder.start(device) this

保存以後打開終端輸入命令:monkeyrunner monkeyrunner.py

彈出MonkeyRecorder界面:

 

四、使用MonkeyRecorder錄製腳本

功能簡介:

wait: 用來插入下一次操做的時間間隔,點擊後便可設置時間,單位是秒

Press a Button:用來肯定須要點擊的按鈕,包括menu、home、search,以及對按鈕的press、down、up屬性

Type Something:用來輸入內容到輸入框

Fling:用來進行拖動操做,能夠向上、下、左、右,以及操做的範圍

Export Actions:用來導出腳本,不須要後綴名,也能夠添加後綴名.mr

Refresh Display:用來刷新手機界面,估計只有在斷開手機後,從新鏈接時纔會用到

使用方法:點擊左側手機界面,在右側會生成相應的事件,該事件也會真實反應在手機上,若是兩次操做之間須要時間間隔,須要手動點擊【wait】來添加等待事件

錄製完成後點擊【Export Actions】保存錄制的腳本,這裏我保存的腳本名是:automonkeyrunner

 

五、回放錄製的腳本:

回放錄製的腳本須要一個python腳本,保存的文件名: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()

 

打開終端定位到錄製的腳本目錄,輸入命令:monkeyrunner monkey_playback.py automonkeyrunner 便可回放剛纔路錄製的腳本

 

六、若是想要批量重複性的執行剛纔錄製的腳本,能夠再新建一個python文件,文件名爲:autoreplay.py

#coding=utf-8
import os
import math
for i in range(1,1000):
  print("第"+str(i)+"次執行")
  os.system("monkeyrunner monkey_playback.py automonkeyrunner")

就是用python重複執行剛纔的命令:monkeyrunner monkey_playback.py automonkeyrunner

而後打開終端執行python命令:python autoreplay.py 便可

 

注意事項:

一、文件路徑不要弄錯了,儘可能都放在一個目錄下

相關文章
相關標籤/搜索