继上一篇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

Monkeyrunner脚本的录制与回放的更多相关文章

  1. shell脚本,录制和回放终端的小工具script。

    action.log和time.log这两个配置文件被当做script命令的参数.这两个文件可以随便命名.这里用time.log和action.log.其中time.log用于存储时序信息,描述每一个 ...

  2. MonkeyRunner之MonkeyRecorder录制回放脚本

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

  3. MonkeyRunner之MonkeyRecorder录制回放脚本(亲测可正常运行)

    MonkeyRunner可以录制和回放脚本 前置条件: 电脑连接手机,输入adb devices 看看返回是否手机设备列表(我是真机,模拟器也可以) 配置好安卓sdk和Python环境 step: 1 ...

  4. Android自动化测试之MonkeyRunner录制和回放脚本

    Android自动化测试之MonkeyRunner录制和回放脚本(十一) 分类: 自动化测试 Android自动化 2013-02-22 10:57 7346人阅读 评论(2) 收藏 举报 andro ...

  5. MonkeyRunner Mac环境 录制脚本和回放 批量回放

    1.MonkeyRunner是AndroidSDK自带的一个东西,在SDK目录中的tools\bin文件夹中 2.配置环境变量 编辑环境变量:打开终端输入:open ~/.bash_profile 将 ...

  6. monkeyrunner之录制与回放(七)

    monkeyrunner为我们提供了录制 回放的功能. 录制与回放使用原因:实际项目,需求变更频繁,且测试任务多,我们没有足够时间去写测试脚本,这是就可以进行录制脚本,然后通过回放,跑完需要的流程. ...

  7. Android自动化学习笔记之MonkeyRunner:MonkeyRunner的录制和回放

    ---------------------------------------------------------------------------------------------------- ...

  8. monkey_recorder录制monkeyrunner脚本

    转载:monkey_recorder录制monkeyrunner脚本   1. 你必须有android sdk, sdk的tools文件家里有一个monkeyrunner.bat.2. 将如下内容拷贝 ...

  9. 一、loadrunner脚本录制及回放

    录制及回放的注意点: 1.测试系统教复杂时,正确的划分action,对监控的每一个业务模型和操作,起到重要作用 2.录制完成后,先进行编译(改动脚本之后检查下有没有语法错误):工具栏Vuser下有一个 ...

随机推荐

  1. 使用Zeppelin时出现sh interpreter not found错误的解决办法(图文详解)

    不多说,直接上干货! 问题详解 http://192.168.80.145:8099/#/notebook/2CSV2VT5S 相关博客是 Zeppelin的入门使用系列之使用Zeppelin运行sh ...

  2. Ubuntu常用指令集

    Ubuntu Linux 操作系统常用命令详细介绍 ( 1)Udo apt-get install 软件名 安装软件命令 sudo nautilus 打开文件(有 root 权限)su root 切换 ...

  3. 基于spring-boot和docker-java实现对docker容器的动态管理和监控[附完整源码下载]

    ​ (我是个封面) docker简介 Docker 是一个开源的应用容器引擎,和传统的虚拟机技术相比,Docker 容器性能开销极低,因此也广受开发者喜爱.随着基于docker的开发者越来越多,doc ...

  4. C# 指定WebBrowser 的 User Agent 版本

    今天用WebBrowser 打开网页,本机ie是ie9 可是WebBrowser 显示的效果明显不是ie9 ,百度查资料才知道,其实是因为直接用IE跟使用WebBrowser 运行的是不同的User ...

  5. nodejs 实践:express 最佳实践(五) connect解析

    nodejs 实践:express 最佳实践(五) connect解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需 ...

  6. HDP和包围曝光

    摄影笔记:http://mp.weixin.qq.com/s/6xgTtAcLAPkWY9FjqrfvtA 我们通过观察直方图曝光,尽量要直方图两边的纯黑和纯白区域不要有信息,就是亮的地方不死白,暗的 ...

  7. PHP针对二维数组无限遍历变形研究

    一.需要变形的二维数组 $arr = Array( Array ( 'material_id' => 1, 'material_name' => '铜板纸', 'parent_id' =& ...

  8. 前端面试题总结(三)JavaScript篇

    前端面试题总结(三)JavaScript篇 一.谈谈对this的理解? this是一个关键字. this总是指向函数的直接调用者(而非间接调用者). 如果有new关键字,this指向new出来的那个对 ...

  9. ArrayList 源码分析(JDK1.8)

    ArrayList简介  ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess ...

  10. Processing一些常用技巧

    一些常用技巧总结: Tweak模式 快速查找函数用法 显示与输入中文注释 代码快速对齐 批量添加注释符 Tweak模式 Tweak模式是非常有用的功能,自3.0版本后,它就正式整合到Processin ...