monkeyrunner

    The monkeyrunner tool is not related to the , also known as the 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.

    The monkeyrunner tool provides these unique features for Android testing:

    • 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.
    • 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.
    • 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.
    • 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 and subprocess modules to call Android tools such as .
      You can also add your own classes to the monkeyrunner API. This is described in more detail in the section Extending monkeyrunner with plugins.

      The monkeyrunner tool uses , an 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.

    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 object. From this object, the program writes out a .png file containing the screenshot.

    • 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.
    • : 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.
      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:
    1. from com.android.monkeyrunner import <module>

    where 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.

    You can either run monkeyrunner programs from a file, or enter monkeyrunner statements in an interactive session. You do both by invoking the monkeyrunner command which is found in the tools/ subdirectory of your SDK directory. If you provide a filename as an argument, the monkeyrunner command runs the file's contents as a Python program; otherwise, it starts an interactive session.

    The syntax of the monkeyrunner command is

    Table 1 explains the flags and arguments.

    Table 1. monkeyrunner flags and arguments.

    1. monkeyrunner help.py <format> <outfile>

    The arguments are:

    • <format> is either text for plain text output or for HTML output.
    • <outfile> is a path-qualified name for the output file.

    You can extend the monkeyrunner API with classes you write in the Java programming language and build into one or more .jar files. You can use this feature to extend the monkeyrunner API with your own classes or to extend the existing classes. You can also use this feature to initialize the monkeyrunner environment.

    To provide a plugin to monkeyrunner, invoke the monkeyrunner command with the -plugin <plugin_jar> argument described in table 1.

    In your plugin code, you can import and extend the main monkeyrunner classes MonkeyDevice, MonkeyImage, and MonkeyRunner in com.android.monkeyrunner (see ).

    Note that plugins do not give you access to the Android SDK. You can't import packages such as com.android.app. This is because monkeyrunner interacts with the device or emulator below the level of the framework APIs.

    To get access to monkeyrunner's runtime environment, the startup class can implement . For example, this class sets up some variables in the default namespace:

    1. package com.android.example
    2.  
    3. import com.google.common.base.Predicate
    4. import org.python.util.PythonInterpreter
    5.  
    6. class Main: Predicate<PythonInterpreter> {
    7. override fun apply(anInterpreter: PythonInterpreter): Boolean {
    8. /*
    9. * Examples of creating and initializing variables in the monkeyrunner environment's
    10. * namespace. During execution, the monkeyrunner program can refer to the variables
    11. * "newtest" and "use_emulator"
    12. *
    13. */
    14. anInterpreter.set("newtest", "enabled")
    15. anInterpreter.set("use_emulator", 1)
    16. return true
    17. }
    18. }