Android Test Tool monkeyrunner--google official website translation

Source: Internet
Author: User

Recently in the review before the note, in retrospect Monkeyrunner looked at the Google website content, write well. Just translate it and share it. In fact, the Google website is really a good place to learn.

Basic knowledge

The Monkeyrunner tool provides an API for controlling Android devices and emulators outside of Android code. Through Monkeyrunner. You can write a Python program to install an Android application or test package. Execute it and send an analog keystroke to it. Intercepts its user-interface picture. And will be stored on the workstation. The Monkeyrunner tool is designed primarily for applications and devices on test function/frame level, or for performing unit test kits, but you can of course use them for other purposes as well.


The Monkeyrunner tool is not associated with the monkey tool.

The Monkey tool executes directly in the Adbshell of the device or emulator. Generates a pseudo-random event stream for a user or system. The Monkeyrunner tool, however, is a specific command and event control device or simulator that is defined on the workstation through the API.
The Monkeyrunner tool provides the following features for Android testing:

    • Multi-Device control: The Monkeyrunner API enables test suites to be implemented across multiple devices or simulators. You can connect all your devices at the same time. It is also possible to start all emulators at once, and then connect to each device in sequence according to the program. Then perform one or more tests. You can also use a program to launch a configured emulator to perform one or more tests.

    • Function test: Monkeyrunner is able to implement a function test for an application on its own initiative. You provide input values for keystrokes or touch events, and then observe the screenshots of the output results.

    • Regression test: Monkeyrunner is able to perform an application and compare its result screenshot to a given known good result screenshot to test the stability of the application.

    • Extensible Self-initiative: Because Monkeyrunner is an API toolkit, you can develop a complete set of systems based on Python modules and programs. To control the Android device.

      In addition to using Monkeyrunner APIs. You can also use the standard Python OS and subprocess modules to invoke Android tools such as ADB.

You can also add your own classes to MONKEYRUNNERAPI. We will discuss this in detail later.
The Monkeyrunner tool uses Jython (a Python implementation using the Java programming language). Jython agrees that the Monkeyrunner API interacts easily with the Android framework.

Use Jython. You can use Python syntax to get constants, classes, and methods in the API.

A simple example of a monkeyrunner program

The following is a simple Monkeyrunner program that will connect to a device and create a Monkeydevice object. Use the Monkeydevice object. The program installs an Android app package, executes one of the activities, and sends a key event to it.

The program will then turn the results. Create a Monkeyimage object and use this object to save to a. png file.

# the Monkeyrunner module required to import this program  from Com.android.monkeyrunner import Monkeyrunner, Monkeydevice # connect the current device, Returns a Monkeydevice object  device = monkeyrunner.waitforconnection () # install Android pack, note , the return value returned by this method is a Boolean, so you can infer that the installation process is normal  device.installpackage ( myproject/bin/ myapplication.apk ' ) # perform an activity in this app device.startactivity (component= ' Com.example.android.myapplication.MainActivity ')  # Press menu button  Device.press ( ' Keycode_menu ' ,  ' down_and_up ' ) Span class= "Hljs-preprocessor" ># capture screen result = Device.takesnapshot# will be saved to file  result.writetofile ( ' myproject/shot1.png ' ,  PNG ' ) 
Monkeyrunner's API

The Monkeyrunner includes a total of three modules in the Com.android.monkeyrunner package:

    • Monkeyrunner: A class that provides a tool method for a Monkeyrunner program. This class provides a way to connect a monkeyrunner to a device or emulator. It also provides a user interface for creating a Monkeyrunner program and a way to display built-in Help.

    • Monkeydevice: Represents a device or emulator. This class provides methods for installing and uninstalling packages, starting an activity, and sending a keyboard or touch event to an application.

      You can also use this class to perform test packs.

    • Monkeyimage: Represents an object. This class provides a way to convert bitmaps into various formats, compare two Monkeyimage objects, and write images to files.

In a python program. You will use these classes in the form of a Python module. Monkeyrunner tools do not actively import these modules themselves. You must use a from statement that resembles the following, for example:

  fromcom.android.monkeyrunner import

Of The name of the class you want to import. You can import more than one module in a from statement. separated by commas.

Executive Monkeyrunner

You can execute Monkeyrunner directly using a code file, or enter the Monkeyrunner statement in an interactive conversation. Either way, you need to call the Monkeyrunner command under the Tools subfolder of the SDK folder.

Assume that you provide a file name as an execution parameter. The Monkeyrunner will view the contents of the file as a Python program and execute it; It will provide an interactive dialog environment.
The syntax for the Monkeyrunner command is:

monkeyrunner -plugin  <程序文件名称><程序选项>
    • -plugin: (optional) specify a. jar file that contains the Monkeyrunner plug-in. For a lot of other content about the Monkeyrunner plugin. below. To specify multiple files. This number can be used multiple times.

    • < program file name: Assuming you specify this parameter, Monkeyrunner will treat the file content as a Python program and execute it.

      If this parameter is not specified, an interactive session is opened.

    • < program options;: (optional) < program file name > the required number of programs specified in the program.

Monkeyrunner provides help

You can use the following command to generate Monkeyrunner API references:

monkeyrunner  help.py

Description of the parameters:
can be text or HTML. Represents plain text and HTML output, respectively.
Specifies the full path name of the output file.

Extending Monkeyrunner with plug-ins

You can create new classes in the Java language and package them into one or more. jar files. To extend the Monkeyrunner API.

You can extend the Monkeyrunner API by using classes that you write yourself or by inheriting existing classes.

You can also use this feature to initialize the Monkeyrunner environment.


In order for the Monkeyrunner to load a plug-in. You should invoke the Monkeyrunner command as you would with the-plugin parameter as described earlier.

In the plug-in you write, you can import or inherit several basic Monkeyrunner classes that are located in the Com.android.monkeyrunner package: Monkeydevice, Monkeyimage, and Monkeyrunner.

Please note that. The plugin does not allow you to access the Android SDK. You cannot import packages such as Com.android.app. This is because Monkeyrunner interacts with the device or emulator under the framework API hierarchy.

Plug-in startup class

The. jar file for the plug-in can specify a class so that it is instantiated before the script executes. To specify this class, you need to add the key monkeyrunnerstartuprunner in the manifest of the. jar file.

Its value is the name of the class that is executed at startup.

The following code snippet shows how to accomplish this in an ant build script:

<jar jarfile="myplugin" basedir="${build.dir}"><manifest><attribute name="MonkeyRunnerStartupRunner" value="com.myapp.myplugin"/></manifest></jar>

To access Monkeyrunner's execution-time environment, the startup class can implement Com.google.common.base.Predicate.

For example, use this class to set some variables in the default namespace:

 PackageCom.android.example;ImportCom.google.common.base.Predicate;ImportOrg.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; } }

Monkeyrunner to the end of the translation, with a good self-motivated testing framework for Android Development has a great help.

Android Test Tool monkeyrunner--google official website translation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.