monkeyrunner自动化测试工具--录制回放

来源:互联网 发布:mac打开csgo黑屏 编辑:程序博客网 时间:2024/05/17 15:05

一、录制回放前检查

1.sdk是否已经下载并解压,sdk解压后的tools目录中含有monkeyrunner.bat

2.网上下载录制脚本,起名为recorder.py,最好和monkeyrunner.bat在一个目录下

#!/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 mrfrom com.android.monkeyrunner.recorder import MonkeyRecorder as recorderdevice = mr.waitForConnection()recorder.start(device)


二、录制回放

1.手机和PC连接后,在PC命令行输入monkeyrunner recorder.py,等待一会,就会出现monkeyrecorder界面



2.使用monkeyrecorder来鼠标操作app界面,在右边将会录制下你的所有动作

3.要录制的操作完成后,点击export actions导出操作脚本文件,可以命名为recorder.mr,放在PC的某一位置,下面使用时要用它的绝对路径.这时候可以把cmd命令行和monkeyrecorder界面关掉了。(在回放时要关掉录制界面,否则回放执行不了)。录制好的脚本可以二次修改,更加接近你想要的效果。

备注个问题:我录制了一段,回放之后总是出问题。后来发现是回放时的脚本基本上是没有延迟的一个接一个的连续操作,而反应在真机上就有问题了,比如点击某处弹出界面就需要时间,所有在脚本上某些可能真机需要延迟的地方要加等待时间。等待函数:WAIT|{'seconds':5.0,} 。成功回放。

下面是我修改了以后的脚本,可以实现自动登录然后下线的操作。

TOUCH|{'x':159,'y':565,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':279,'y':588,'type':'downAndUp',}TOUCH|{'x':204,'y':761,'type':'downAndUp',}TOUCH|{'x':204,'y':761,'type':'downAndUp',}TOUCH|{'x':204,'y':761,'type':'downAndUp',}TOUCH|{'x':204,'y':761,'type':'downAndUp',}TOUCH|{'x':204,'y':761,'type':'downAndUp',}TOUCH|{'x':204,'y':761,'type':'downAndUp',}TOUCH|{'x':190,'y':491,'type':'downAndUp',}TOUCH|{'x':268,'y':648,'type':'downAndUp',}TOUCH|{'x':213,'y':671,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':379,'y':396,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':223,'y':501,'type':'downAndUp',}TOUCH|{'x':399,'y':506,'type':'downAndUp',}TOUCH|{'x':207,'y':508,'type':'downAndUp',}TOUCH|{'x':301,'y':495,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':210,'y':390,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':421,'y':745,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':187,'y':405,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':154,'y':621,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':259,'y':186,'type':'downAndUp',}WAIT|{'seconds':5.0,} TOUCH|{'x':358,'y':518,'type':'downAndUp',}

4.将下列代码存成一个文件,起名为recorder.py,最好和monkeyrunner.bat在一个目录下

#!/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 sysfrom 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()

5.重新打开一个命令行输入monkeyrunner playback.py recorder.mr(必须是绝对路径)执行回放动作,直到结束。


ps1:monkeyrecorder 菜单栏功能介绍

ButtonDescriptionWait等待时间Press a Button发送,MENU,HOME,or SEARCH 按钮.Press,Down,or Up事件Type Something发送一些字符串Fling用来操作虚拟键盘 
imageExport Action将我们的脚本导出来Refresh Display刷新当前界面ps2:monkeyrecorder还可以获取手机屏幕坐标

1 0
原创粉丝点击