继上一篇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 recorderdevice = 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 pydictrest = eval(rest)except:print 'unable to parse options'continueif cmd not in CMD_MAP:print 'unknown command: ' + cmdcontinueCMD_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

转载于:https://www.cnblogs.com/yajing-zh/p/4385418.html

Monkeyrunner脚本的录制与回放相关推荐

  1. monkeyrunner脚本的录制和回放

    参考:http://www.cnblogs.com/android-host/p/5378996.html 需要条件: 1.android-sdk 2.monkey_recorder.py 3.mon ...

  2. 第五章脚本的录制与回放

    第五章 脚本的录制与回放 5.1UFT的视图 1. UFT支持两种视图:一种是Keyword View(关键字视图),另一种是Expert View(专家视图). (1) Keyword View:关 ...

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

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

  4. monkeyrunner的录制与回放

    在monkeyrunner中我们可以录制对手机的操作事件,新建一文件monkey_recorder.py,复制一下代码: #!/usr/bin/env monkeyrunner # Copyright ...

  5. 控件获取图像可从几方面取得?_基于图像特征与布局刻画的移动测试脚本跨平台录制回放...

    一. 引言 移动应用在全球范围内有着越发举足轻重的地位,因此移动应用的快速迭代和频繁的需求变更的特点引发了对应用质量保障的要求不断提高.在大型设备集群上迁移测试脚本是移动应用质量保障的关键技术之一,因 ...

  6. 关于脚本录制和回放终端会话(script、scriptreplay)使用

    script和scriptreplay命令在绝大多数GNU/linux都有发行版上都有. 使用这两个命令可以用于终端会话的录制和回放,制作视频供大家分享. 1.开始录制 #script -t 2> ...

  7. 脚本录制和回放终端会话(script、scriptreplay)使用

    script和scriptreplay命令在绝大多数GNU/linux都有发行版上都有. 使用这两个命令可以用于终端会话的录制和回放,制作视频供大家分享. 1.开始录制 #script -t 2> ...

  8. python屏幕录制与回放_monkeyrunner之录制与回放(七)

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

  9. 终端的录制、回放与实时共享

    为什么80%的码农都做不了架构师?>>>    在<linux shell 脚本攻略>一书中,看到可以通过 script / scriptreplay 命令进行终端的录制 ...

最新文章

  1. 理解Java中的hashCode 和 equals 方法
  2. 天猫php采集列表,QueryList: QueryList是一个基于phpQuery的通用列表采集类,是一个简单、 灵活、强大的采集工具,采集任何复杂的页面 基本上就一句话就能搞定了。...
  3. 1063. Set Similarity (25)
  4. AI:2020年6月21日北京智源大会演讲分享之14:00-14:25 张铭教授《基于知识图谱的机器学习》
  5. excel中match函数_Excel函数轻松学02:详解Excel函数中的数据类型
  6. python中的数字类型格式与运算,python数字数据类型
  7. 内网穿透工作笔记001---UDP通信_内网通信原理_P2P点对点通信原理
  8. java xml stax_Java StAX解析器示例以读取XML文件
  9. 群晖修改docker端口映射
  10. 人脸识别实践(1) - 基本原理与设计思路
  11. 中望3D 2021 插入基准面 - 三点法
  12. GEE引擎自定义进度条和自定义属性的脚本展示
  13. maven本地仓库的更新
  14. Coursera | Introduction to Data Science in Python(University of Michigan)| Assignment1
  15. 物联网-The Internet of Things
  16. MyBatis引起的SQL语法错误
  17. App制作需要哪些技术?
  18. 热门软件看点:QQ和它的对手们 收藏 转载
  19. appium知识总结
  20. Matlab矩阵行相加、列相加

热门文章

  1. linux登录指令 pgsql_一句一例解读20条Linux常用指令,学会了你就入门了
  2. linux apache 2.2下载,Apache HTTP Server 2.2.26 发布下载
  3. 中国计量大学研究生复试c语言_中国计量大学2020考研招调剂生,25个专业
  4. Bootstrap 下拉菜单事件
  5. Win10的 IntelliJ idea中log4j2控制台中文乱码的几种处理方法
  6. 【PAT乙】1065 单身狗 (25分)
  7. 查看 java heapspace_Java heap space 问题查找
  8. 设置层级为2html,前端知识(Css)汇总2
  9. html调用mysql数据库表里值,如何从MYSQL数据库正确调用行到HTML表中
  10. aftershokz蓝牙搜不到_iphone xr黑屏无法开机怎么办?iphonexr蓝牙连不上怎么办?