Recently in the review before the note, in retrospect Monkeyrunner looked at the Google website content, written well, translated out to share. 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. With Monkeyrunner, you can write a Python program to install an Android application or test package, run it, send it an analog keystroke, intercept its user interface picture, and store it on your workstation. The main purpose of the Monkeyrunner tool is to test applications and devices at the functional/frame level, or to run unit test suites, but you can of course use them for other purposes.
The Monkeyrunner tool is not associated with the monkey tool. The Monkey tool runs directly in the Adbshell of the device or emulator, generating a pseudo-random stream of events for the 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 can implement test suites across multiple devices or emulators. You can pick up all the devices at the same time, or you can start all the emulators at once, connect to each device sequentially, and then run one or more tests. You can also use a program to launch a configured emulator to run one or more tests.
Functional testing: Monkeyrunner can automatically implement a functional test for an application. You provide input values for keystrokes or touch events, and then observe the screenshots of the output results.
Regression test: Monkeyrunner can test the stability of an application by running an app and comparing its result screenshot to a given known good result screen.
Scalable Automation: Because Monkeyrunner is an API toolkit, you can develop a complete set of systems based on Python modules and programs to control your Android device. In addition to using the Monkeyrunner API, you can 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'll discuss this in more detail later.
The Monkeyrunner tool uses Jython (a Python implementation using the Java programming language). Jython allows the Monkeyrunner API to interact easily with the Android framework. With Jython, you can use Python syntax to get constants, classes, and methods in the API.
A simple example of a monkeyrunner program
Here is a simple Monkeyrunner program that will connect to a device and create a Monkeydevice object. Using the Monkeydevice object, the program installs an Android app package, runs one of the activities, and sends a key event to it. The program will then create a Monkeyimage object with the result, 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 Boolean, so you can tell if the installation process is normal device.installpackage () # run 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
Monkeyrunner contains 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 run test packages.
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 as a Python module. The Monkeyrunner tool does not automatically import these modules. You must use a from statement similar to the following:
fromcom.android.monkeyrunner import
Where, for the class name that you want to import. You can import more than one module in a from statement, separated by commas.
Run Monkeyrunner
You can run Monkeyrunner directly with a code file, or enter the Monkeyrunner statement in an interactive conversation. Either way, you need to invoke the Monkeyrunner command under the tools subdirectory of the SDK directory. If you provide a file name as a running parameter, Monkeyrunner will view the file content as a Python program and run it, otherwise 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 more information about the Monkeyrunner plugin, see below. To specify multiple files, you can use this parameter multiple times.
< program file name: If 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 turned on.
< program options;: (optional) < program file name > The required parameters for the program specified in.
Monkeyrunner provides help
You can use the following command to generate an API reference for Monkeyrunner:
monkeyrunner help.py
Parameter description:
can be text or HTML, representing plain text and HTML output, respectively.
Specifies the full path name of the output file.
Extending Monkeyrunner with plug-ins
You can extend the Monkeyrunner API by creating new classes in the Java language and packaging them into one or more. jar files. 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 described in the previous-plugin parameter.
In the plug-in you write, you can import or inherit several of the main 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. The name of the class whose value is run at startup. The following code snippet shows how to achieve this in an ant build script:
<jar jarfile="myplugin" basedir="${build.dir}"><manifest><attribute name="MonkeyRunnerStartupRunner" value="com.myapp.myplugin"/></manifest></jar>
To access the Monkeyrunner runtime 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, the use of a good automated test framework for Android Development has a great help.
Android Test Tool monkeyrunner--google official website translation