Monkeyrunner 录制脚本&回放

一、录制回放过程

monkeyrunner的源代码可以在android源代码里找到,monkeyrunner源代码里有个scripts文件夹,里面有monkey_recorder.py和monkey_playback.py两个python文件,这两个文件就是用来录制和回放的~!

连上设备,使用cmd打开命令行=>输入monkeyrunner  xxx/xxx/monkey_record.py(这个要填完整路径,不然会提示找不到) 

这样就可以录制啦~~

出现录制的界面,点击屏幕即会生成坐标,please see the top,还有按button,输入文本,滑动等事件

录制完毕后选择Export Actions就将脚本保存下来了,保存的时候不需要后缀名。

录完脚本,下面就是回放了

打开命令行=>输入monkeyrunner monkey_playback.py(这里写完整路径)刚刚export的脚本(写明完整路径)

这样就可以回放了~

二、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(**a

rg),

    '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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值