前言

MonkeyRunner强大的功能之一便是允许用户自由录制需要的脚本,录制和回放需要两个脚本文件 monkey_recorder.py 和 monkey_playback.py 

1、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 recorder

# 连接设备
device = mr.waitForConnection()
# 录制开始
recorder.start(device)

2、调出MonkeyRecorder录制工具

①将monkey_recorder.py脚本放入monkeyrunner目录下android-sdk\tools。

monkeyrunner测试工具的录制和回放----MonkeyRecorder录制回放脚本_后缀名

②输入命令,执行脚本。

monkeyrunner monkey_recorder.py

出现下图:

monkeyrunner测试工具的录制和回放----MonkeyRecorder录制回放脚本_后缀名_02

③该窗口的功能如下:

1)可以自动显示手机当前的界面;

2)自动刷新手机的最新状态;

3)点击手机界面即可对手机进行操作并反应到真机,在右侧插入脚本;

4)

①wait: 用来插入下一次操作的时间间隔,点击后即可设置时间,单位是秒;

②Press a Button:用来确定需要点击的按钮,包括menu、home、search,以及对按钮的press、down、up属性;

③Type Something:用来输入内容到输入框;

④Fling:用来进行拖动操作,可以向上、下、左、右,以及操作的范围;

⑤Export Actions:用来导出脚本,不需要后缀名,也可以添加后缀名.mr;

⑥Refresh Display:用来刷新手机界面,估计只有在断开手机后,重新连接时才会用到;(刷新下,同步手机端屏幕)

3、录制脚本

下例为导出的脚本完成打开设置,上下滑动,点home回到桌面。(导出的脚本保存为test.mr)

monkeyrunner测试工具的录制和回放----MonkeyRecorder录制回放脚本_后缀名_03

4、如下monkey_playback.py代码:(test.mr这种录制类型的脚本需要monkey_playback.py脚本来解释执行)

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:
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()

5、回放脚本

执行如下命令即可回放:

monkeyrunner monkey_playback.py test.mr

6、注意

①以上路径都是绝对路径,录制后的脚本可以进行二次更改,而且每一步操 作 需要有时间间隔,这样才能保证测试的正确性。

②如果遇到不能回放的问题,需要关闭当前录制时的cmd窗口,重新打开 cmd窗口执行回放操作。

③如果需要不断的执行脚本,死循环可替换如下脚本:

#!/usr/bin/python
#!/usr/bin/env monkeyrunner


import sys
from com.android.monkeyrunner import MonkeyRunner


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:
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]
device = MonkeyRunner.waitForConnection()
fp = open(file, "r")
process_file(fp, device)
fp.close();
while True:
fp = open(file, "r")
process_file(fp, device)
fp.close();

if __name__ == "__main__":
main()

 

去期待陌生,去拥抱惊喜。