Monkeyrunner实现UI自动化

准备工作:

1、安装android sdk,保证sdk文件下的tools文件夹下有一个monkeyrunner.bat文件

2、创建一个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)

3、创建一个回放脚本play_back.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 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()


在tools文件下运行cmd:monkeyrunner monkey_recorder.py(该录制文件需要相对路径)

Monkeyrunner实现UI自动化_第1张图片

界面会弹出如下:

Monkeyrunner实现UI自动化_第2张图片

该窗口的功能:

1、可以自动显示当前手机的界面

2、自动刷新当前手机的最新状态

3、点击手机界面可对手机进行操作,同时会反应到真机,而且会在右侧空白处插入操作脚本

4、wait:用来插入下一次操作的时间间隔,点击后即可设置时间(单位:S)

     press a button:用来确定需要点击的按钮,包括menu、home、search,以及对按钮的press、down、up、属性

     type something:用来输入户内容到输入框

     fling:用来进行拖动操作,可以向上、下、左、右,以及操作的范围

     export action:用来导出脚本

     refresh display:用来刷新手机界面,估计只有在断开手机后,重新连接才会用到

当我们在这个工具点击时,真机也会执行相同的操作,并且右侧会显示我们每一次点击的位置坐标,我们最好在每个操作直接插入wiat等待

然后export 脚本文件。如:d:\test.mr 录制的内容大致如下(脚本的后缀为mr)。

PRESS|{'name':'MENU','type':'downAndUp',}

WAIT|{'seconds':2.0,}

TOUCH|{'x':432,'y':1376,'type':'downAndUp',}

WAIT|{'seconds':4.0,}

TOUCH|{'x':560,'y':140,'type':'downAndUp',}

TOUCH|{'x':465,'y':148,'type':'downAndUp',}

TYPE|{'message':'nike',}

WAIT|{'seconds':4.0,}

TOUCH|{'x':975,'y':148,'type':'downAndUp',}

DRAG|{'start':(432,1449),'end':(432,289),'duration':1.0,'steps':10,}


最后cmd运行刚才录制生成的脚本如下:

Monkeyrunner实现UI自动化_第3张图片

预期的结果就是我们刚才在手机上所进行的操作。


你可能感兴趣的:(自动化)