傻啦吧唧的程序员丶

android 测试 Monkey 和 MonkeyRunner 的使用

一、Monkey的使用

  Monkey使用起来比较简单,简而言之就是模拟手机点击效果,随机发送N个点击动作给手机,主要对于程序的稳定和承受压力的测试。

1.首先连接上你的手机或者启动模拟器;

2.运行CMD,进入命令输入框;

3.输入 adb shell monkey -p your.package -vvv 500 > e:\exception.txt (可能有朋友会问为什么不进入adb shell 之后才操作呢? 因为进入adb shell 中他没有可操作的权限了,也就不能创建exception.txt 文件,会报错。)

4.最后运行结束后可以在exception.txt 文档中查看你的运行结果。

解释:

  -p 后面也就是app的包名,如果你是在测试你自己的app,尽量在manifest.xml中去复制,也可以adb shell 中 进入 data/data  用命令 ls 列出来,找出你需要的包。

  -vvv 这个是输出三种运行的三种状态,就是详细输出事件等级,这个3个v就是输出等级1至3的所有事件。

  至于500 则是随机发送500个点击事件给app点击使用。

  存储到exception.txt是为了更好的查看测试结果。

  其中也可以加上 --throttle 3000 每执行一次有效的事件后休眠3秒,如果又需要也可以添加上去。

二、MonkeyRunner的使用

  这个是通用运行脚本文件来测试的,目的性强,更有针对性,更容易控制。

1.首先连接上你的手机或者启动一个模拟器;

2.运行CMD,进入你的sdk的tools文件夹下,在下面有一个monkeyrunner.bat;

3.然后就是创建你的脚本文件,里面有你写好的一系列的操作。(注意:这里你的脚本文件一定要是UTF-8文件格式,不然会出错。

新建一个testmonkeyrunner.py

#导入我们需要用到的包和类并且起别名
import sys
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner import MonkeyDevice as md
from com.android.monkeyrunner import MonkeyImage as mi
 
#connect device 连接设备
#第一个参数为等待连接设备时间
#第二个参数为具体连接的设备
device = mr.waitForConnection(1.0,'ca985d92')
if not device:
    print >> sys.stderr,"fail"
    sys.exit(1)
#定义要启动的Activity
componentName='com.org.hl.john.monkeytest/.MainActivity'
#启动特定的Activity
device.startActivity(component=componentName)
mr.sleep(3.0)
#do someting 进行我们的操作
#输入 helloworld
device.type('helloworld')
#输入回车
device.press('KEYCODE_ENTER')
#return keyboard
#device.press('KEYCODE_BACK')
#------
#takeSnapshot截图
mr.sleep(3.0)
result = device.takeSnapshot()
 
#save to file 保存到文件
result.writeToFile('./shot.png','png');

这里借用了一下其他朋友的代码,表示非常感谢!

在命令行输入

monkeyrunner testmonkeyrunner.py

就会根据你脚本里设置的包名和activity启动应用,然后根据脚本里的一系列的操作,跟着操作。

 

记录和回放

  使用脚本,启用一个可视化操作的界面

新建一个(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)

然后在命令行运行:

monkeyrunner monkey_recorder.py  

就会弹出一个可视化的操作界面

然后可以在手机上进入你的app,如图:

跟着就可以在app里面做你想要测试的操作,必须是在电脑上这个界面上操作。

 

Wait 等待时间
Press a Button 发送,MENU,HOME,or SEARCH 按钮.Press,Down,or Up事件
Type Something 发送一些字符串
Fling 用来操作虚拟键盘 
image
Export Action 将我们的脚本导出来
Refresh Display 刷新当前界面

 

 

当你操作完成后可以选择export action 导出我们的脚本,我保存的名称是action.mr,保存到tools目录下

导出后打开可以看到里面一系列的操作,但是这样是用不了的,还必须写到可运行的脚本里。

新建脚本:monkey_playback.py (也保存到tools目录下)

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

写这个脚本还是必须先研究一下Phthon这个语言,楼主暂时还没有研究,所以暂时还不能解释上面的代码。。。。。

输入:

monkeyrunner play_black.py action.mr

运行即可,之前你操作的动作就重复实现了。

 

 

以上仅供参考!

 

--throttle 3000

posted on 2016-04-11 16:44  傻啦吧唧的程序员丶  阅读(433)  评论(0编辑  收藏  举报

导航