首先电脑已经搭配好SDK的环境
录制
1. 把以下的脚本复制粘贴后保存为recorder.py的文件,并且把recorder.py文件放在SDK---Tools目录下。
#!/usr/bin/envmonkeyrunner
#Copyright 2010, The Android Open Source Project
#
#Licensed under the Apache License, Version 2.0 (the "License");
# you maynot use this file except in compliance with the License.
# You mayobtain a copy of the License at
#
#http://www.apache.org/licenses/LICENSE-2.0
#
# Unlessrequired by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUTWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See theLicense for the specific language governing permissions and
#limitations under the License.
fromcom.android.monkeyrunner import MonkeyRunner as mr
fromcom.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device =mr.waitForConnection()
recorder.start(device)
2.保存后,在cmd窗口tools目录下,输入recorder.py
3.点击预览界面的控件,在右边会出现点击的坐标。录制过程中,每个控件之间应该点击Wait,等待下时间。
4.录制完成后,点击ExportActions按钮,保存文件,把文件保存到tools目录下,并且保存为XX.py文件。
回放
把以下代表复制粘贴并且保存文件为recorder_playback.py
#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open SourceProject
#
# Licensed under the Apache License,Version 2.0 (the "License");
# you may not use this file except incompliance 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 oragreed to in writing, software
# distributed under the License isdistributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.
# See the License for the specific languagegoverning permissions and
# limitations under the License.
import sys
from com.android.monkeyrunner importMonkeyRunner
# The format of the file we are parsing isvery carfeully constructed.
# Each line corresponds to a singlecommand. 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 intoexistence) that specifies the
# arguments for the command. In most cases, this directly maps to the
# keyword argument dictionary that could bepassed to the underlying
# command.
# Lookup table to map command strings tofunctions 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 specifieddevice.
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()
2.在cmd窗口tools目录下输入monkeyrunnerrecorder_playback.py xx.py