monkeyrunner测试工具的录制和回放----MonkeyRecorder录制回放脚本
前言
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 monkey_recorder.py
出现下图:
③该窗口的功能如下:
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)
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()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!