monkeyrunner测试教程

monkeyrunner测试教程,第1张

  什么是monkeyrunner

  monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器。通过monkeyrunner,您可以写出一个Python程序去安装一个Android应用程序或测试包,运行它,向它发送模拟击键,截取它的用户界面图片,并将截图存储于工作站上。monkeyrunner工具的主要设计目的是用于测试功能/框架水平上的应用程序和设备,或用于运行单元测试套件,但您当然也可以将其用于其它目的。

  monkeyrunner工具同Monkey工具的差别

  Monkey:

  Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流。

  monkeyrunner:

  monkeyrunner工具则是在工作站上通过API定义的特定命令和事件控制设备或模拟器。

  monkeyrunner的测试类型

  1. 多设备控制:monkeyrunner API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。

  2.功能测试: monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。

  3.回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。

  4. 可扩展的自动化:由于monkeyrunner是一个API工具包,您可以基于Python模块和程序开发一整套系统,以此来控制Android设备。除了使用monkeyrunner API之外,您还可以使用标准的Python os和subprocess模块来调用如adb这样的Android工具 。

  5.monkeyrunner工具使用Jython(使用Java编程语言的一种Python实现)。Jython允许monkeyrunnerAPI与Android框架轻松的进行交互。使用Jython,您可以使用Python语法来获取API中的常量、类以及方法。

  运行monkeyrunner

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

  monkeyrunner测试教程,monkeyrunner测试教程,第2张

  monkeyrunner的命令语法为:

  monkeyrunner -plugin 《plugin_jar》 《program_filename》 《program_opTIons》

  monkeyrunner测试教程

  如果你需要实现自动测试,Android的monkeyrunner 工具可以帮助你实现自动测试,它提供了一组API可以用来控制Android设备或模拟器,使用monkeyrunner,你可以编写Python 程序来安装Android应用或是测试包,运行应用或测试,发送按键消息,并可以截屏,然后保存在计算机中。monkeyrunner 主要目的是用来在应用程序或框架层次来测试应用程序或运行单元测试包,但你也可以用作其它目的。

  monkeyrunner 工具包不同于UI/ApplicaTIon Exerciser Monkey(也称为Money),money 通过adb shell 来运行,可以模拟“猴子”随机按键或是发送系统消息给指定的应用来实现Stress 测试。

  monkeyrunner API 主要通过下面三个包:

  MonkeyRunner: 主要提供了monkeyrunner 应用的辅助方法以及,用来链接设备或是模拟器的方法,并提供UI支持等。

  MonkeyDevice: 代表一个设备或是模拟器,提供安装,卸载应用的方法,启动一个AcTIvity,发送按键或是Touch 事件等。

  MonkeyImage: 代表一个截屏图像,可以截取不同格式的图像,比较两个MonkeyImage图像,保存图像等。

  下面为一个 Python 写的monkeyrunner 应用, 因为涉及到Python 语言,这里不详细说明了

  [python] view plain copy# Imports the monkeyrunner modules used by this program

  from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

  # 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(‘myproject/bin/MyApplication.apk’)

  # sets a variable with the package‘s internal name

  package = ’com.example.android.myapplication‘

  # sets a variable with the name of an Activity in the package

  activity = ’com.example.android.myapplication.MainActivity‘

  # sets the name of the component to start

  runComponent = package + ’/‘ + activity

  # Runs the component

  device.startActivity(component=runComponent)

  # 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(’myproject/shot1.png‘,’png‘)

  详细的API说明请参考Android文档 ,如果你需要实现自动测试,编写测试代码,可以使用Python通过monkeyrunner API来实现。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2717825.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-17
下一篇 2022-08-17

发表评论

登录后才能评论

评论列表(0条)

保存