通过录制与回放的方式使用Monkeyrunner来对一个第三方应用进行测试

通过录制与回放的方式使用Monkeyrunner来对一个第三方应用进行测试

 

Monkeyrunner工具的位置在sdk\tools目录下,根据其行为可以将它理解为一个Python解释器。实际上它就是一个基于Jython的Python解释器,也正因为如此,使它调用Java代码写的API很方便。

 

使用Monkeyrunner通常有以下几种方式:

       1.把其当做一个Python解释器,手机连接上PC后,在PC上以CS模式执行命令;

       2.事先编写或者录制测试脚本,然后以解释脚本的方式执行;

       3.编写插件执行;

 

以下展示如何通过录制、回访的方式来使用Monkeyrunner进行黑盒测试:

 

1.在将设备连接到PC上之后,首先切换到tool目录,启动Monkeyrunner:

       启动后的界面如下,可以看到就是一个Python的解释器界面:

2.导入相关的模块:com.android.monkeyrunner和com.android.monkeyrunner.recorder,然后启动录制命令,这时录制程序将被启动,效果如下图。录制程序的左边显示的是当前的手机屏幕,直接通过鼠标在上面操作,右边显示录制的脚本。

3.执行操作,这里以实用UC浏览器打开百度,搜索“android”为例:

直接使用鼠标在左边的屏幕内执行操作,可以看到右边生成的脚本是以鼠标点击坐标的格式存储的。


假如希望录制的脚本能够在不同的设备上运行,最好使用菜单项中的固定命令,比如通过Press a Button菜单指定接下来输入一个点击菜单按钮的命令:

      

 

 

4.导出录制脚本。通过菜单上的Export Action菜单输出录制的脚本文件。

5.回放录制的脚本:

       在Monkeyrunner的源码路径下提供了一个专门用来回放所录制的脚本的Python文件monkey_playback.py,具体内容如下:

      

 #!/usr/bin/envmonkeyrunner
# Copyright 2010, The Android Open SourceProject
#
# Licensed under the Apache License,Version 2.0 (the "License");
# you may not use this file except incompliance 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 oragreed to in writing, software
# distributed under the License isdistributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.
# See the License for the specific languagegoverning permissions and
# limitations under the License.
 
import sys
from com.android.monkeyrunner importMonkeyRunner
 
# The format of the file we are parsing isvery carfeully constructed.
# Each line corresponds to a singlecommand.  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 intoexistence) that specifies the
# arguments for the command.  In most cases, this directly maps to the
# keyword argument dictionary that could bepassed to the underlying
# command.
 
# Lookup table to map command strings tofunctions 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 specifieddevice.
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、付费专栏及课程。

余额充值