当前位置: 代码迷 >> Android >> android monkeyrunner 一
  详细解决方案

android monkeyrunner 一

热度:58   发布时间:2016-05-01 16:21:33.0
android monkeyrunner 1
帮助文档的目录

file:///E:/android/android-sdk-windows/docs/guide/developing/tools/monkeyrunner_concepts.html

The monkeyrunner tool provides an API for writing programs that control an Android device or emulator from outside of Android code. With monkeyrunner, you can write a Python program that installs an Android application or test package, runs it, sends keystrokes to it, takes screenshots of its user interface, and stores screenshots on the workstation. The monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for running unit test suites, but you are free to use it for other purposes.

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

The monkeyrunner tool is not related to the UI/Application Exerciser Monkey, also known as the monkey tool. The monkey tool runs in an adb shell directly on the device or emulator and generates pseudo-random streams of user and system events. In comparison, the monkeyrunner tool controls devices and emulators from a workstation by sending specific commands and events from an API.

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

 
The monkeyrunner tool provides these unique features for Android testing:
monkeyrunner工具为Android测试提供了以下特性:

Multiple device control: The monkeyrunner API can apply one or more test suites across multiple devices or emulators. You can physically attach all the devices or start up all the emulators (or both) at once, connect to each one in turn programmatically, and then run one or more tests. You can also start up an emulator configuration programmatically, run one or more tests, and then shut down the emulator.

多设备控制:monkeyrunner    API可以跨多个设备或模拟器实施测试套件。您可以在同一时间接上所有的设备或一次启动全部模拟器(或统统一起),依据程序依次连接到每一个,然后运行一个或多个测试。您也可以用程序启动一个配置好的模拟器,运行一个或多个测试,然后关闭模拟器。
       
       Functional testing: monkeyrunner can run an automated start-to-finish test of an Android application. You provide input values with keystrokes or touch events, and view the results as screenshots.

功能测试:    monkeyrunner可以为一个应用自动贯彻一次功能测试。您提供按键或触摸事件的输入数值,然后观察输出结果的截屏。  
    
Regression testing - monkeyrunner can test application stability by running an application and comparing its output screenshots to a set of screenshots that are known to be correct.

回归测试:monkeyrunner可以运行某个应用,并将其结果截屏与既定已知正确的结果截屏相比较,以此测试应用的稳定性。  
    
Extensible automation - Since monkeyrunner is an API toolkit, you can develop an entire system of Python-based modules and programs for controlling Android devices. Besides using the monkeyrunner API itself, you can use the standard Python os and subprocess modules to call Android tools such as Android Debug Bridge.

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

You can also add your own classes to the monkeyrunner API. This is described in more detail in the section Extending monkeyrunner with plugins.    

   您还可以向monkeyrunner API中添加您自己的类。我们将在使用插件扩展monkeyrunner一节中对此进行详细讨论 。

The monkeyrunner tool uses Jython, a implementation of Python that uses the Java programming language. Jython allows the monkeyrunner API to interact easily with the Android framework. With Jython you can use Python syntax to access the constants, classes, and methods of the API.
monkeyrunner工具使用Jython(使用Java编程语言的一种Python实现)。Jython允许monkeyrunnerAPI与Android框架轻松的进行交互。使用Jython,您可以使用Python语法来获取API中的常量、类以及方法。

A Simple monkeyrunner Program
一个简单的monkeyrunner程序实例

Here is a simple monkeyrunner program that connects to a device, creating a MonkeyDevice object. Using the MonkeyDevice object, the program installs an Android application package, runs one of its activities, and sends key events to the activity. The program then takes a screenshot of the result, creating a MonkeyImage object. From this object, the program writes out a .png file containing the screenshot.
以下为一个简单的monkeyrunner程序,它将会连接到一个设备,创建一个MonkeyDevice对象。使用MonkeyDevice对象,程序将安装一个Android应用包,运行其中一个活动,并向其发送按键事件。程序接下来会将结果截图,创建一个MonkeyImage对象,并使用这个对象截图将保存至.png文件。
  # Imports the monkeyrunner modules used by this programfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice# Connects to the current device, returning a MonkeyDevice objectdevice = 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')# Runs an activity in the applicationdevice.startActivity(component='com.example.android.myapplication.MainActivity')# Presses the Menu buttondevice.press('KEYCODE_MENU','DOWN_AND_UP')# Takes a screenshotresult = device.takeSnapShot# Writes the screenshot to a fileresult.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.example.android.myapplication.MainActivity')# 按下菜单按键device.press('KEYCODE_MENU','DOWN_AND_UP')# 截取屏幕截图result = device.takeSnapShot# 将截图保存至文件result.writeToFile('myproject/shot1.png','png')


The monkeyrunner API

The monkeyrunner API is contained in three modules in the package com.android.monkeyrunner:
monkeyrunnerAPI于com.android.monkeyrunner包中包含三个模块:

    MonkeyRunner: A class of utility methods for monkeyrunner programs. This class provides a method for connecting monkeyrunner to a device or emulator. It also provides methods for creating UIs for a monkeyrunner program and for displaying the built-in help.
    MonkeyDevice: Represents a device or emulator. This class provides methods for installing and uninstalling packages, starting an Activity, and sending keyboard or touch events to an application. You also use this class to run test packages.
    MonkeyImage: Represents a screen capture image. This class provides methods for capturing screens, converting bitmap images to various formats, comparing two MonkeyImage objects, and writing an image to a file.

    MonkeyRunner:一个为monkeyrunner程序提供工具方法的类。这个类提供了用于连接monkeyrunner至设备或模拟器的方法。它还提供了用于创建一个monkeyrunner程序的用户界面以及显示内置帮助的方法。       
    MonkeyDevice:表示一个设备或模拟器。这个类提供了安装和卸载程序包、启动一个活动以及发送键盘或触摸事件到应用程序的方法。您也可以用这个类来运行测试包。       
    MonkeyImage:表示一个截图对象。这个类提供了截图、将位图转换成各种格式、比较两个MonkeyImage对象以及写图像到文件的方法。       

In a Python program, you access each class as a Python module. The monkeyrunner tool does not import these modules automatically. To import a module, use the Python from statement:
在python程序中,您将以Python模块的形式使用这些类。monkeyrunner工具不会自动导入这些模块。您必须使用类似如下的from语句:

from com.android.monkeyrunner import <module>
fromcom.android.monkeyrunner import

where <module> is the class name you want to import. You can import more than one module in the same from statement by separating the module names with commas.
其中,为您想要导入的类名。您可以在一个from语句中导入超过一个模块,其间以逗号分隔。
  相关解决方案