51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 24687|回复: 161
打印 上一主题 下一主题

MonkeyRunner介绍

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2014-9-9 07:17:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 jane1229 于 2014-9-9 07:19 编辑



本文是自己搜集的一点资料

文档内容参考包含了一下内容:
http://www.chuanke.com/v1983382-106000-218426.html
http://blog.csdn.net/zuoyu55/article/details/7718447
http://blog.csdn.net/simmer_ken/article/details/7637556
等等

其中包含自己写的几个例子,请大家多多指点。
部分内容如下:

六、实例
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)录制操作




3、回放操作


2、MonkeyRunner执行Python脚本实例——发送短信(这是网上找的例子:连接:http://www.cnblogs.com/dimdusk/archive/2013/04/15/3011208.html)
目标:对手机短信程序进行压力测试。
#导入我们需要用到的包和类并且起别名
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 连接设备
#第一个参数为等待连接设备时间
#第二个参数为具体连接的设备ID
device = mr.waitForConnection(1.0,‘device_id’)
if not device:
    print >> sys.stderr,"fail"
    sys.exit(1)
#定义要启动的Activity
componentName='com.android.mms/.ui.BootActivity'
#启动特定的Activity
device.startActivity(component = componentName)
#等待时间,防止操作太快,设备反应不及时
mr.sleep(1.0)
#do someting 进行我们的操作
#新建短信
#参数是屏幕上坐标值,最后一个参数是动作
device.touch(57,747,'DOWN_AND_UP')
mr.sleep(1.0)
#输入收件人号码
device.type('10086')
#发送短信条数
for i in range(0,10):
    #输入短信内容
    device.touch(187,402,'DOWN_AND_UP')
    device.type('Hello')
    mr.sleep(1.0)
    #发送短信
    device.touch(432,380,'DOWN_AND_UP')
    mr.sleep(1.0)
    device.touch(51,752,'DOWN_AND_UP')
    mr.sleep(1.0)
#takeSnapshot截图
mr.sleep(1.0)
result = device.takeSnapshot()
#save to file 保存到文件,D盘的根目录下
result.writeToFile('d:\\result2.png','png');
3、monkeyrunner测试apk包安装卸载
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = MonkeyRunner.waitForConnection()
#install package NotesList.apk
#可以通过创建samples project 然后运行安装NotesList.apk到手机设备上
#device.installPackage('./installPackage/NotesList.apk')
device.installPackage('NotesList.apk')
print ('install success')
#remove package NotesList.apk
device.removePackage ('com.example.android.notepad')
print ('removed success')
MonkeyRunner.sleep(15)

4、使用控件ID写脚本

------------------------------------------------------------
由于帖子字数限制。。
如有需要可以下载附件。。。需要回复可见

游客,如果您要查看本帖隐藏内容请回复

本帖子中包含更多资源

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

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

使用道具 举报

  • TA的每日心情
    开心
    2016-2-1 08:26
  • 签到天数: 113 天

    连续签到: 1 天

    [LV.6]测试旅长

    3#
    发表于 2014-9-9 10:29:06 | 只看该作者
    看看了。谢谢


    如有需要可以下载附件。。。需要回复可见
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
    发表于 2014-9-10 09:14:46 | 只看该作者

    学习
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2014-9-10 10:00:30 | 只看该作者
    现在也在使用monkeyrunner,前来求学
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    6#
    发表于 2014-9-10 10:18:59 | 只看该作者
    看看,准备性能测试
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    8#
    发表于 2014-9-16 15:38:27 | 只看该作者
    Monkeyrunner是要学的
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    10#
     楼主| 发表于 2014-9-25 22:08:13 | 只看该作者

    哈哈,之前以为帖子没通过。。
    一回来,居然有这么多消息
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    11#
    发表于 2014-9-29 14:16:26 | 只看该作者
    语句可循环吗
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    15#
    发表于 2014-10-13 17:10:46 | 只看该作者
    还挺复杂的,看看
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    16#
    发表于 2014-10-15 10:39:43 | 只看该作者
    恩很不错的哈哈哈哈哈
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    20#
    发表于 2014-10-16 14:04:47 | 只看该作者
    看看了。谢谢
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-9-21 08:16 , Processed in 0.090238 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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