Android 测试工具Monkey & Monkeyrunner 使用方法
2013-06-19       0  个评论       作者:marshalchen
收藏     我要投稿

    程序编写完成后进行测试一定是必不可少的,Android Sdk给我们提供了Monkey和Monkeyrunner这两个自动化测试工具。时光境迁,随着版本的不断更新迭代,Monkey和Monkeyrunner也在不断的改变,改变的速度甚至已经到了官方文档有时都跟不上了,这无疑给我们的学习带来很多困扰。

       今天我把我近期使用Monkey和Monkeyrunner的一些体会与大家分享,Sdk版本为Rev22.01,Api17(4.22).
 

 


   Monkey是一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流,实现对正在开发的应用程序进行压力测试。Monkey包括许多选项,它们大致分为四大类:

· 基本配置选项,如设置尝试的事件数量。

· 运行约束选项,如设置只对单独的一个包进行测试。

· 事件类型和频率。

· 调试选项。


二、Monkey的基本用法


基本语法如下:


$ adb shell monkey [options]
如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:


$ adb shell monkey -p your.package.name -v 500
一些常用的参数信息:
-v
命令行的每一个-v将增加反馈信息的级别。Level 0(缺省值)除启动提示、测试完成和最终结果之外,提供较少信息。Level 1提供较为详细的测试信息,如逐个发送到Activity的事件。Level 2提供更加详细的设置信息,如测试中被选中的或未被选中的Activity。

事件
-s
伪随机数生成器的seed值。如果用相同的seed值再次运行Monkey,它将生成相同的事件序列。

--throttle
在事件之间插入固定延迟。通过这个选项可以减缓Monkey的执行速度。如果不指定该选项,Monkey将不会被延迟,事件将尽可能快地被产成。

--pct-touch
调整触摸事件的百分比(触摸事件是一个down-up事件,它发生在屏幕上的某单一位置)。

--pct-motion
调整动作事件的百分比(动作事件由屏幕上某处的一个down事件、一系列的伪随机事件和一个up事件组成)。

--pct-trackball
调整轨迹事件的百分比(轨迹事件由一个或几个随机的移动组成,有时还伴随有点击)。

--pct-nav
调整“基本”导航事件的百分比(导航事件由来自方向输入设备的up/down/left/right组成)。

--pct-majornav
调整“主要”导航事件的百分比(这些导航事件通常引发图形界面中的动作,如:5-way键盘的中间按键、回退按键、菜单按键)

--pct-syskeys
调整“系统”按键事件的百分比(这些按键通常被保留,由系统使用,如Home、Back、Start Call、End Call及音量控制键)。

--pct-appswitch
调整启动Activity的百分比。在随机间隔里,Monkey将执行一个startActivity()调用,作为最大程度覆盖包中全部Activity的一种方法。

--pct-anyevent
调整其它类型事件的百分比。它包罗了所有其它类型的事件,如:按键、其它不常用的设备按钮、等等。

约束限制
-p
如果用此参数指定了一个或几个包,Monkey将只允许系统启动这些包里的Activity。如果你的应用程序还需要访问其它包里的Activity(如选择取一个联系人),那些包也需要在此同时指定。如果不指定任何包,Monkey将允许系统启动全部包里的Activity。要指定多个包,需要使用多个 -p选项,每个-p选项只能用于一个包。

-c
如果用此参数指定了一个或几个类别,Monkey将只允许系统启动被这些类别中的某个类别列出的Activity。如果不指定任何类别,Monkey将选 择下列类别中列出的Activity: Intent.CATEGORY_LAUNCHER或Intent.CATEGORY_MONKEY。要指定多个类别,需要使用多个-c选项,每个-c选 项只能用于一个类别。

调试
--dbg-no-events
设置此选项,Monkey将执行初始启动,进入到一个测试Activity,然后不会再进一步生成事件。为了得到最佳结果,把它与-v、一个或几个包约 束、以及一个保持Monkey运行30秒或更长时间的非零值联合起来,从而提供一个环境,可以监视应用程序所调用的包之间的转换。

--hprof
设置此选项,将在Monkey事件序列之前和之后立即生成profiling报告。这将会在data/misc中生成大文件(~5Mb),所以要小心使用它。

--ignore-crashes
通常,当应用程序崩溃或发生任何失控异常时,Monkey将停止运行。如果设置此选项,Monkey将继续向系统发送事件,直到计数完成。

--ignore-timeouts
通常,当应用程序发生任何超时错误(如“Application Not Responding”对话框)时,Monkey将停止运行。如果设置此选项,Monkey将继续向系统发送事件,直到计数完成。

--ignore-security-exceptions
通常,当应用程序发生许可错误(如启动一个需要某些许可的Activity)时,Monkey将停止运行。如果设置了此选项,Monkey将继续向系统发送事件,直到计数完成。

--kill-process-after-error
通常,当Monkey由于一个错误而停止时,出错的应用程序将继续处于运行状态。当设置了此选项时,将会通知系统停止发生错误的进程。注意,正常的(成功的)结束,并没有停止启动的进程,设备只是在结束事件之后,简单地保持在最后的状态。

--monitor-native-crashes
监视并报告Android系统中本地代码的崩溃事件。如果设置了--kill-process-after-error,系统将停止运行。

--wait-dbg
停止执行中的Monkey,直到有调试器和它相连接。-v
命令行的每一个-v将增加反馈信息的级别。Level 0(缺省值)除启动提示、测试完成和最终结果之外,提供较少信息。Level 1提供较为详细的测试信息,如逐个发送到Activity的事件。Level 2提供更加详细的设置信息,如测试中被选中的或未被选中的Activity。

事件
-s
伪随机数生成器的seed值。如果用相同的seed值再次运行Monkey,它将生成相同的事件序列。

--throttle
在事件之间插入固定延迟。通过这个选项可以减缓Monkey的执行速度。如果不指定该选项,Monkey将不会被延迟,事件将尽可能快地被产成。

--pct-touch
调整触摸事件的百分比(触摸事件是一个down-up事件,它发生在屏幕上的某单一位置)。

--pct-motion
调整动作事件的百分比(动作事件由屏幕上某处的一个down事件、一系列的伪随机事件和一个up事件组成)。

--pct-trackball
调整轨迹事件的百分比(轨迹事件由一个或几个随机的移动组成,有时还伴随有点击)。

--pct-nav
调整“基本”导航事件的百分比(导航事件由来自方向输入设备的up/down/left/right组成)。

--pct-majornav
调整“主要”导航事件的百分比(这些导航事件通常引发图形界面中的动作,如:5-way键盘的中间按键、回退按键、菜单按键)

--pct-syskeys
调整“系统”按键事件的百分比(这些按键通常被保留,由系统使用,如Home、Back、Start Call、End Call及音量控制键)。

--pct-appswitch
调整启动Activity的百分比。在随机间隔里,Monkey将执行一个startActivity()调用,作为最大程度覆盖包中全部Activity的一种方法。

--pct-anyevent
调整其它类型事件的百分比。它包罗了所有其它类型的事件,如:按键、其它不常用的设备按钮、等等。

约束限制
-p
如果用此参数指定了一个或几个包,Monkey将只允许系统启动这些包里的Activity。如果你的应用程序还需要访问其它包里的Activity(如选择取一个联系人),那些包也需要在此同时指定。如果不指定任何包,Monkey将允许系统启动全部包里的Activity。要指定多个包,需要使用多个 -p选项,每个-p选项只能用于一个包。

-c
如果用此参数指定了一个或几个类别,Monkey将只允许系统启动被这些类别中的某个类别列出的Activity。如果不指定任何类别,Monkey将选 择下列类别中列出的Activity: Intent.CATEGORY_LAUNCHER或Intent.CATEGORY_MONKEY。要指定多个类别,需要使用多个-c选项,每个-c选 项只能用于一个类别。

调试
--dbg-no-events
设置此选项,Monkey将执行初始启动,进入到一个测试Activity,然后不会再进一步生成事件。为了得到最佳结果,把它与-v、一个或几个包约 束、以及一个保持Monkey运行30秒或更长时间的非零值联合起来,从而提供一个环境,可以监视应用程序所调用的包之间的转换。

--hprof
设置此选项,将在Monkey事件序列之前和之后立即生成profiling报告。这将会在data/misc中生成大文件(~5Mb),所以要小心使用它。

--ignore-crashes
通常,当应用程序崩溃或发生任何失控异常时,Monkey将停止运行。如果设置此选项,Monkey将继续向系统发送事件,直到计数完成。

--ignore-timeouts
通常,当应用程序发生任何超时错误(如“Application Not Responding”对话框)时,Monkey将停止运行。如果设置此选项,Monkey将继续向系统发送事件,直到计数完成。

--ignore-security-exceptions
通常,当应用程序发生许可错误(如启动一个需要某些许可的Activity)时,Monkey将停止运行。如果设置了此选项,Monkey将继续向系统发送事件,直到计数完成。

--kill-process-after-error
通常,当Monkey由于一个错误而停止时,出错的应用程序将继续处于运行状态。当设置了此选项时,将会通知系统停止发生错误的进程。注意,正常的(成功的)结束,并没有停止启动的进程,设备只是在结束事件之后,简单地保持在最后的状态。

--monitor-native-crashes
监视并报告Android系统中本地代码的崩溃事件。如果设置了--kill-process-after-error,系统将停止运行。

--wait-dbg
停止执行中的Monkey,直到有调试器和它相连接。

 

 
三、Monkey实例测试
Windows下(注:2—4步是为了查看我们可以测试哪些应用程序包,可省略):

1、 通过eclipse启动一个Android的emulator

2、 在命令行中输入:adb devices查看设备连接情况

 


List of devices attached

emulator-5554   device

3、 在有设备连接的前提下,在命令行中输入:adb shell 进入shell界面

 

#

4、 查看data/data文件夹下的应用程序包。注:我们能测试的应用程序包都在这个目录下面

 


如:

com.android.htmlviewer
com.android.settings
com.android.netspeed
com.android.providers.userdictionary
com.android.browser
com.android.contacts
com.android.alarmclock


 


5.在其中找到我们需要测试的包名com.marshalchen.MonkeyTest1

 


6、 以com.marshalchen.MonkeyTest1 作为对象进行MonkeyTest

#monkey -p com.marshalchen.MonkeyTest1 -v 500

其中-p表示对象包 –v 表示事件数量

运行过程中,Emulator中的应用程序在不断地切换画面。

按照选定的不同级别的反馈信息,在Monkey中还可以看到其执行过程报告和生成的事件。

如:Sending Pointer ACTION_DOWN x=35.0 y=259.0
:Sending Pointer ACTION_UP x=195.0 y=259.0
:Sending Pointer ACTION_DOWN x=295.0 y=223.0
:Dropped: keys=0 pointers=0 trackballs=0 flips=0
等等信息。


四、Monkey停止的条件
Monkey Test执行过程中在下列三种情况下会自动停止:

1、如果限定了Monkey运行在一个或几个特定的包上,那么它会监测试图转到其它包的操作,并对其进行阻止。

2、如果应用程序崩溃或接收到任何失控异常,Monkey将停止并报错。

3、如果应用程序产生了应用程序不响应(application not responding)的错误,Monkey将会停止并报错。

通过多次并且不同设定下的Monkey测试才算它是一个稳定性足够的程序。

 

 

 

 

 

MonkeyRunner 使用


    Monkey的使用简洁有效,但是总是给人感觉功能不够强大。所以,接下来向大家介绍非常给力的工具MonkeyRunner。
一、什么是MonkeyRunner
   monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。

monkeyrunner工具与monkey工具并无关联。monkey工具直接运行在设备或模拟器的adbshell中,生成用户或系统的伪随机事件流。而monkeyrunner工具则是在工作站上通过API定义的特定命令和事件控制设备或模拟器。

 

monkeyrunner工具为Android测试提供了以下特性:

1.多设备控制:monkeyrunner    API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。       
2.功能测试:    monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。       
3.回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。   
4.可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner API之外,您还可以使用标准的Python os和subpress模块来调用如adb这样的Android工具。
   
    您还可以向monkeyrunner API中添加您自己的类。
    monkeyrunner工具使用Jython(使用Java编程语言的一种Python实现)。Jython允许monkeyrunnerAPI与Android框架轻松的进行交互。使用Jython,您可以使用Python语法来获取API中的常量、类以及方法。

 

 二、一个简单的MonkeyRunner实例
您可以直接使用一个代码文件运行monkeyrunner,抑或在交互式对话中输入monkeyrunner语句。不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。

monkeyrunner的命令语法为:

monkeyrunner -plugin

 

monkeyrunnerbasic.py:

 

[python]
# 导入此程序所需的monkeyrunner模块   
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice  
# 连接当前设备,返回一个MonkeyDevice对象   
device = MonkeyRunner.waitForConnection()  
# 安装Android包,注意,此方法返回的返回值为boolean,由此您可以判断安装过程是否正常   
device.installPackage('myproject/bin/MyApplication.apk')  
# 运行此应用中的一个活动device.startActivity(component='com.marshalchen.Monkeyrunner.MainActivity')   
# 按下菜单按键   
device.press('KEYCODE_MENU','DOWN_AND_UP')  
# 截取屏幕截图   
result = device.takeSnapShot 
# 将截图保存至文件   
result.writeToFile('myproject/shot1.png','png') 

# 导入此程序所需的monkeyrunner模块
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# 连接当前设备,返回一个MonkeyDevice对象
device = MonkeyRunner.waitForConnection()
# 安装Android包,注意,此方法返回的返回值为boolean,由此您可以判断安装过程是否正常
device.installPackage('myproject/bin/MyApplication.apk')
# 运行此应用中的一个活动device.startActivity(component='com.marshalchen.Monkeyrunner.MainActivity')
# 按下菜单按键
device.press('KEYCODE_MENU','DOWN_AND_UP')
# 截取屏幕截图
result = device.takeSnapShot
# 将截图保存至文件
result.writeToFile('myproject/shot1.png','png')
然后转到$SDK\TOOLS下执行 monkeyrunner monkeyrunnerbasic.py 就可执行测试了。


PS:上述内容,部分参考了 子杨 的博客,在此表示感谢。


三、常用功能的Monkeyrunner python脚本介绍:


1.输入等基本操作测试


[python]
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,'emulator-5554') 
if not device: 
    print >> sys.stderr,"fail" 
    sys.exit(1) 
#定义要启动的Activity  
componentName='kg.monkey/.MonkeyActivity' 
#启动特定的Activity  
device.startActivity(component=componentName) 
mr.sleep(3.0) 
#do someting 进行我们的操作  
#输入 a s d  
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('takeSnapshot\\result1.png','png'); 

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,'emulator-5554')
if not device:
    print >> sys.stderr,"fail"
    sys.exit(1)
#定义要启动的Activity
componentName='kg.monkey/.MonkeyActivity'
#启动特定的Activity
device.startActivity(component=componentName)
mr.sleep(3.0)
#do someting 进行我们的操作
#输入 a s d
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('takeSnapshot\\result1.png','png');


2.图形化记录与回放
利用
[plain]
SPAN style="FONT-FAMILY: SimSun">monkeyrunner monkey_recorder.py 

monkeyrunner monkey_recorder.py
脚本,可以打开如图所示可视化模拟器,各种操作可以显示在屏幕右侧,并且通过“Export Actions”导出。

 

 

 

导出之后可以运行monkeyrunner monkey_playback.py monkey_test1.mr,(monkey_test1.mr为刚才保存的脚本),这时可以看到模拟器,进行刚才一样的操作。


补充多设备操作:可以在monkey_playback.py中的main()方法的device设备获取时指定多设备。


下面是这两个有用的脚本:
monkey_recorder.py:


[python]
#!/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) 

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


monkey_playback.py:

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

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

 

 

 

 

 


PS:脚本内容 部分参考了 youxilua ,非常感谢。

 

 

 

四、使用插件扩展Monkeyrunner
您可以用Java语言创建新的类,并打包成一个或多个.jar文件,以此来扩展monkeyrunnerAPI。您可以使用您自己写的类或者继承现有的类来扩展monkeyrunnerAPI。您还可以使用此功能来初始化monkeyrunner环境。
为了使monkeyrunner加载一个插件,您应当如使用如表1中所述的-plugin参数来调用monkeyrunner命令。
在您编写的插件中,您可以导入或继承位于com.android.monkeyrunner包中的几个主要的monkeyrunner类:MonkeyDevice, MonkeyImage和MonkeyRunner.

请注意,插件无法让你访问Android的SDK。您不能导入com.android.app等包。这是因为monkeyrunner是在框架API层次之下与设备或模拟器进行交互的。
插件启动类
用于插件的.jar文件可以指定一个类,使其在脚本执行之前就实例化。如欲指定这个类,您需要在.jar文件的manifest中添加键MonkeyRunnerStartupRunner。其值为启动时运行的类的名称。以下代码段显示了如何在一个ant构建脚本达到这样的目的:
如欲访问monkeyrunner的运行时环境,启动类可以实现com.google.common.base.Predicate。例如,用这个类在默认的命名空间中设置一些变量:


[python]
package com.android.example; 
  
 import com.google.common.base.Predicate; 
 import org.python.util.PythonInterpreter; 
  
 public class Main implements Predicate { 
     @Override 
     public boolean apply(PythonInterpreter anInterpreter) { 
  
         /* 
         * Examples of creating and initializing variables in the monkeyrunner environment's 
         * namespace. During execution, the monkeyrunner program can refer to the variables "newtest" 
         * and "use_emulator" 
         * 
         */ 
         anInterpreter.set("newtest", "enabled"); 
         anInterpreter.set("use_emulator", 1); 
  
         return true; 
     } 
 } 

package com.android.example;
 
 import com.google.common.base.Predicate;
 import org.python.util.PythonInterpreter;
 
 public class Main implements Predicate {
     @Override
     public boolean apply(PythonInterpreter anInterpreter) {
 
         /*
         * Examples of creating and initializing variables in the monkeyrunner environment's
         * namespace. During execution, the monkeyrunner program can refer to the variables "newtest"
         * and "use_emulator"
         *
         */
         anInterpreter.set("newtest", "enabled");
         anInterpreter.set("use_emulator", 1);
 
         return true;
     }
 }


五、常见问题(评论中的问题会不断补充)


1.“device.touch(10,100, 'DOWN_AND_UP')” 为什么报错?ANS:可以把 'DOWN_AND_UP' 替换为 'MonkeyDevice.DOWN_AND_UP' ,这个问题的主要原因跟Monkeyrunner有关。
2.我输入了很多指令,但是很多没有执行Ans:可以尝试  MonkeyRunner.sleep(time_in_seconds) 方法,让不同的动作之间有间隔。

 

更多相关文章

  1. 点击事件XML里的写法
  2. android 获取蓝牙已连接设备
  3. Android View事件传递机制
  4. Android首选项SharedPreference-android学习之旅(六)
  5. android获取版本信息、屏幕信息和设备编号

随机推荐

  1. android 存储联系人(save contact) 总结
  2. 整理出15个Android很有用的代码片段
  3. Android(安卓)中数据库查询方法query()中
  4. Android消息推送机制调研
  5. 关于Android中的radioGroup选择
  6. Android通过tcpdump抓包(wifi, 2g, 3g都
  7. android.database.CursorIndexOutOfBound
  8. Android(安卓)DownloadManager 用法
  9. android 之 菜单
  10. android 定时器的实现