android基础知识12:android自动化测试05—monkeyRunner

        之前的几篇文章,我们分别介绍了monkey,junit, Robotium,以及基于junit的android测试框架进行介绍,本篇文章我们将对monkeyRunner 进行介绍。

1、什么是monkeyrunner
        monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。
2、monkeyrunner工具同Monkey工具的差别
Monkey:
Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流。
monkeyrunner:
monkeyrunner工具则是在工作站上通过API定义的特定命令和事件控制设备或模拟器。
3、monkeyrunner的测试类型

  • 1、多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。
  • 功能测试: monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。
  • 回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。
  • 可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner API之外,您还可以使用标准的Python os和subprocess模块来调用Android Debug Bridge这样的Android工具
4、运行monkeyrunner
         您可以直接使用一个代码文件运行monkeyrunner,抑或在交互式对话中输入monkeyrunner语句。不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。
monkeyrunner的命令语法为:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
5、实例1
5.1 简单实例
以sample中的ApiDemos为例,先将其生成ApiDemos.apk。
前提:已有device连接
1)、 将ApiDemos.apk放在$Android_Root\tools下。
2)、 在$Android_Root\tools下新建一个monkeyrunnerprogram.py文件,里面内容为:
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('./ApiDemos.apk')
# Runs the component
device.startActivity(component='com.example.android.apis/.ApiDemos')
# Presses the Menu button
device.press('KEYCODE_MENU','DOWN_AND_UP')
# Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./shot1.png','png')
注意:SDK上的例子有些错误,不可直接复制,否则执行命令时会发生错误。具体可与我的上面这段代码对照。
3)、 打开命令行转到Android_Root\tools目录下运行一下命令:
monkeyrunner monkeyrunnerprogram.py
若无错误,则运行完成以后,$Android_Root\tools目录下会生成shot1.png文件。注意,在运行过程中,若没有错误,命令行没有任何输出。
5.2 扩展实例
因为ApiDemos首页上按下MENU键没有菜单出现,为了更加形象化,在实例五的基础上继续试验:
1)、 在$Android_Root\tools下新建一个monkeyrunnerprogram1.py文件,里面内容为:
# Imports the monkeyrunner modules used by this program  
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file 
result.writeToFile('./shotbegin.png','png')
# Presses the Down button
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
# Takes a screenshot
result = device.takeSnapshot()
# Writes the screenshot to a file
result.writeToFile('./shotend.png','png')
2)、将画面定位在Apidemos的首页,并将光标定位在第一项上。
3)、$Android_Root\tools目录下运行一下命令:
monkeyrunner monkeyrunnerprogram1.py
4)、在运行过程中我们可以看见光标不断向下移动,并且可以在当前目录下我们自定义的截图:
运行前:shotbegin.png


6、扩展应用实例
下面提供一些常用的脚本,自己看着来改吧..
monkey_recorder.py
monkey_placback.py
help.py
具体下载地址为: monkeyrunner_py脚本.rar
虽然,少了些东西,但是,并不影响我们大部分的需要.接下来用一段典型的monkeyRunner代码讲解!
注意!如果monkeyrunner脚本文件要使用中文,记得格式保存为utf8,不然会ASCNII(忘了怎么拼写了..)无法支持错误
6.1 takescreen.py
文件takescreen.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()
if not device:
    print >> sys.stderr,"fail"
    sys.exit(1)
#定义要启动的Activity
componentName='com.example.android.notepad/.NotesList'
#启动特定的Activity
device.startActivity(component=componentName)
mr.sleep(3.0)
#do someting 进行我们的操作
#输入 a s d
#device.press('KEYCODE_HOME')
#device.touch(418, 736,'DOWN_AND_UP')
#mr.sleep(3.0)
#device.touch(180, 520,'DOWN_AND_UP')
#mr.sleep(3.0)
device.press('KEYCODE_MENU')
mr.sleep(3.0)
device.touch(243, 745,'DOWN_AND_UP')
mr.sleep(3.0)
device.type('woaini')
device.press('KEYCODE_ENTER')
mr.sleep(3.0)
device.type(',')
device.press('KEYCODE_ENTER')
mr.sleep(3.0)
device.type('yiwen')
device.press('KEYCODE_ENTER')
mr.sleep(3.0)
device.press('KEYCODE_BACK')
#device.press('KEYCODE_HOME')
#device.type('asd')
#输入回车
#device.press('KEYCODE_ENTER')
#return keyboard 点击返回用于取消等下看到截图的下方的白条
#device.press('KEYCODE_BACK')
#------
#takeSnapshot截图
mr.sleep(3.0)
result = device.takeSnapshot()
 
#save to file 保存到文件
result.writeToFile('result1.png','png');
这里用到的notelist实例类似于android提供的notepad实例。
其运行结果为(图result1.png):


6.2 monkeyRunner 的记录和回放
        前面讲的都是一些在命令行上的操作,我可记不住那么多的指令操作,我可不知道,我点击的这个点的坐标是多少,我多么希望,我能够在可视化界面里面讲我的操作记录下来,然后,直接重新播放,就像宏一样,我可以很高兴的告诉你,MonkeyRunner有这个功能实现起来也非常简单,我提供的打包文件中有一个,monkey_recorder.py,直接在命令行中打上:
monkeyrunner monkey_recorder.py

其中手机屏幕部分,与当前连接的手机设备的屏幕显示是一致的。
对其中脚本显示的一些说明:

接下来运行我们的保存的脚本,然后,你就看到模拟器,进行你刚才一样的操作
monkeyrunner monkey_playback.py monkey_test.mr
打开我们的文件可以看到其实就是一些monkeyrunner的一些脚本
TOUCH|{'x':329,'y':132,'type':'downAndUp',} 
TOUCH|{'x':100,'y':100,'type':'downAndUp',} 
TOUCH|{'x':296,'y':407,'type':'downAndUp',} 
TOUCH|{'x':296,'y':407,'type':'downAndUp',} 
TOUCH|{'x':296,'y':407,'type':'downAndUp',} 
TOUCH|{'x':296,'y':407,'type':'downAndUp',} 
TOUCH|{'x':351,'y':227,'type':'downAndUp',}
当然,有界面为什么不用呢~~~呵呵~
补充一点:如果我们要进行多设备测试怎么办呢?
我们可以打开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()




至此,我们已经简单介绍完monkeyRunner ,相信大家已经对其它有一个大致的了解,并且可以简单应用了。
如果大家还不够了解,可以看看“monkeyrunner_py脚本.rar”中的所有源码,以及其中monkeyRunner各个类的说明文件。 
另外谷歌原始说明文件为:http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html,有兴趣可以看看。


参考文献:

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值