51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 3625|回复: 5
打印 上一主题 下一主题

收集的Monkeyrunner介绍

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2014-9-9 07:54:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、什么是monkeyrunner
monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。
使用monkeyrunner前,需要安装和配置JDK、SDK和Python。
二、monkeyrunner工具同Monkey工具的差别
Monkey:
Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流。
monkeyrunner:
monkeyrunner工具则是在工作站上通过API定义的特定命令和事件控制设备或模拟器。

Monkeyrunner缺点:
1、        monkeyrunner本身存在bug,长时间跑,自身也会出现crash
2、        monkeyrunner无论导入什么库,都是app层的,黑盒测试,相当于android ui layout上又架了一层,模拟点击操作
三、monkeyrunner的测试类型
1、多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。
2、 功能测试: monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。
3、 回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。
4、 可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner API之外,您还可以使用标准的Python os和subprocess模块来调用Android Debug Bridge这样的Android工具。
四、运行monkeyrunner
您可以直接使用一个代码文件运行monkeyrunner,抑或在交互式对话中输入monkeyrunner语句。不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。
monkeyrunner的命令语法为:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
-plugin <plugin_jar>
为 monkeyrunner 指定一个包含插件的.jar 文件。学习更多内容见 Extending monkeyrunner with plugins.指定多个插件,请多次输入参数。
<program_filename>
如果您提供这个参数,monkeyrunner 命令 按 Python 程序的方式运行文件的内容;否则,它开启一个会话窗口。<程序选项>
<program_options>  
<program_file>中程序的标志和参数
五、monkerunner脚本基本方式
# 导入此程序所需的monkeyrunner模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# 连接当前设备,返回一个MonkeyDevice对象
device = MonkeyRunner.waitForConnection()
# 安装Android包,注意,此方法返回的返回值为boolean,由此您可以判断安装过程是否正常
device.installPackage('myproject/bin/MyApplication.apk')
# 运行此应用中的一个活动 device.startActivity(component='com.example.android.myapplication.MainActivity')
# 按下菜单按键
device.press('KEYCODE_MENU','DOWN_AND_UP')
# 截取屏幕截图
result = device.takeSnapShot
# 将截图保存至文件
result.writeToFile('myproject/shot1.png','png')
六、实例
1、关于录制和回放,获取坐标点
1. 将以下代码拷贝到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)
2. 将以下代码拷贝到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(**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()
3. 获取坐标点、脚本
(1)打开monkey_recorder.py

(2)录制操作


由于字数限制,请查看附件
由于字数限制,附件不能上传完整的。

参考资料如下:
http://www.chuanke.com/v1983382-106000-218426.html
http://blog.csdn.net/zuoyu55/article/details/7718447
http://blog.csdn.net/s ... etails/7637556


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4
回复

使用道具 举报

  • TA的每日心情
    奋斗
    12 小时前
  • 签到天数: 2383 天

    连续签到: 2 天

    [LV.Master]测试大本营

    2#
    发表于 2014-9-19 13:33:57 | 只看该作者
    预留着,还在黑盒折腾中
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2014-11-11 22:52
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    3#
    发表于 2014-11-11 23:03:14 | 只看该作者
    点个赞,不过没学过Python的孩子表示回放的代码看不懂
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2016-8-23 15:39
  • 签到天数: 44 天

    连续签到: 1 天

    [LV.5]测试团长

    4#
    发表于 2015-11-23 14:33:05 | 只看该作者
    学习了,谢谢分享!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2016-1-29 14:53
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    5#
    发表于 2016-2-20 10:18:23 | 只看该作者
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    6#
    发表于 2016-5-26 15:19:37 | 只看该作者
    都是一样的
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-14 21:22 , Processed in 0.067379 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表