继上一篇monkeyrunner环境搭建:http://www.cnblogs.com/zh-ya-jing/p/4351245.html 之后,我们可以进一步学习monkeyrunner了。

  我也是刚接触monkeyrunner不久,对monkeyrunner的脚本录制功能很感兴趣,所以学习一下。没想到中间遇到很多问题,之前是录制脚本不通过,再之后是手机连接不上,monkeyrunner运行不起来,归根结底还是录制脚本的问题,后向大神请教,可算是能成功录制脚本了。

  不知道出于什么目的,google把monkeyrunner的脚本录制功能雪藏了,需要从Android源码中才能将其发掘出来。monkey_recorder.py是用来录制在设备上的操作病生成脚本的,monkey_playback.py则用来回放脚本。

  新建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)

  新建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_recorder.py

执行完毕之后,monkeyrunner会打开一个窗口,如下所示。

它会不停地从设备上抓取最新界面,你可以直接在左边的屏幕截图上单击图标来模拟触控操作方式,不要直接操作真机或模拟器,否则无法录制脚本。操作的同时右边会实时显示录制的脚本,单击“Type something”按钮输入字符串,单击“Fling”按钮模拟滑动手势。当操作录制完毕后,单击“Export Actions”按钮,将脚本保存到指定目录,比如test.mr,关闭monkeyrunner运行窗口。

  将录制好的脚本传给monkey_playback.py文件就可以回放了,执行如下命令:

$monkeyrunner monkey_playback.py test.mr

假如回放过程出错,有可能是真机或者模拟器反应比较慢,两次操作之间间隔时间太短,所以建议两次操作之间加些wait,即每次操作之后点击“wait”按钮,增加等待时间。

必须把monkey_recorder.py,monkey_playback.py和录制的脚本test.mr放入"*\adt-bundle-windows-x86-20130917\sdk\tools"目录下,而且运行.py文件都使用绝对路径。

可以参考如下博客:http://blog.csdn.net/zm2714/article/details/7980634

最新文章

  1. Android基础总结(十)
  2. javascript 原型及原型链的初步理解
  3. MATLAB 秒表函数 tic toc 计算程序运行时间
  4. 关于mapreduce.map.java.opts
  5. 点击弹出固定大小的新窗口(js实现)
  6. observer观察者模式
  7. 记事本源代码 python3
  8. [terry笔记]IMPDP报错ORA-39083 Object type TYPE failed to create ORA-02304
  9. 视频播放(iOS开发)
  10. Ken Norton和软件工程师打交道的10个秘诀
  11. win8 Pro 64位在 UEFI模式下Ghost系统 备份 恢复
  12. 【LeetCode练习题】Minimum Depth of Binary Tree
  13. 使用批处理根据项目工程文件生成Nuget包并发布(支持.NET Core)
  14. Java 9 揭秘(1.Java入门介绍)
  15. SpringMVC Hello(IDEA)
  16. iOS中 UIMPMediaPickerController播放系统音乐
  17. Zara精讲C#.Cache、它和Redis区别是什么???
  18. Lock wait timeout exceeded
  19. ASP.NET Core微服务+Tabler前端框架搭建个人博客1--开始前想说的话
  20. Unity Shader 效果(1) :图片流光效果

热门文章

  1. MVC View与Controller分离
  2. NodeJS学习视频
  3. SQL函数TIMEDIFF在Java程序中使用报错的问题分析
  4. AJPFX关于Java中的集合
  5. hystrix 应用问题
  6. JAVA---spring-boot入门(图文教程)
  7. u-boot剖析(一)----Makefile分析
  8. 【虚拟机-网络IP】如何开放 Azure 虚拟机 Ping 功能
  9. 卓越管理的实践技巧(3)推动团队管理的要点 Facilitation Essentials for Managers
  10. a survey for RL