MonkeyRunner is a tool that comes with the Android SDK. It is basically a tool using python scripts to run automation for Android apps.
The Android SDK site has a simple example of a monkeyrunner script and how to run it. See “A Simple monkeyrunner program” and “Running monkeyrunner” – both can be found here. For a start, don’t run with the plug-in option.
One thing to note about monkeyrunner is that unlike UI Automation or any of the other automation tools, you don’t search for a UI element and click (or tap) on it. Instead, you use the keypad keys to navigate. I found using the up, down, left, right and center keys most useful. Also there doesn’t seem to be a way to look for UI elements and extract the labels or even accessibility keys.
There are 3 main components for monkeyrunner: MonkeyRunner, MonkeyDevice and MonkeyImage classes.
The MonkeyRunner class contains static utility methods to do a run – the most important being the WaitForConnection() method which returns a MonkeyDevice class for actual testing.
The MonkeyDevice class is the most important as its methods control the device eg. install a package, perform button press, drag, start Activities.
The MonkeyImage is used for capturing screenshots.
Some Issues to take note of
There is also a typo in the script found in the SDK documentation:
# 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
Will mean that
com.example.android.myapplication/.MainActivity which is correct.
Also for some reason, I could not get the script to run with
as it seemed that the ‘press’ method required 3 params instead of 2. I just added a dummy string param at the end eg. ‘xx’.
Note that the script is written in Python – here is a decent for the Python language I found useful.