一、录制回放前检查
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 mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = 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 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()
5.重新打开一个命令行输入monkeyrunner playback.py recorder.mr(必须是绝对路径)执行回放动作,直到结束。
ps1:monkeyrecorder 菜单栏功能介绍
Button | Description |
Wait | 等待时间 |
Press a Button | 发送,MENU,HOME,or SEARCH 按钮.Press,Down,or Up事件 |
Type Something | 发送一些字符串 |
Fling | 用来操作虚拟键盘 |
Export Action | 将我们的脚本导出来 |
Refresh Display | 刷新当前界面 |