Android testing with monkeyrunner – a monkeyrunner tutorial

This is what you have been waiting for .. A simple tutorial about all the things you need to know about monkeyrunner testing tool provided by android SDK.


What is monkeyrunner?

It is a tool which provides an API for writing programs that control an android device fromoutside of android code.


What you can do with monkeyrunner?

You can write python programs to test the applications on one or more devices and/or emulators. You can do following things and more with monkeyrunner.

  • Installs an application or test package

  • Runs an application

  • Send keystrokes or touch events to it

  • Take screen shots of the user interface

  • Store screen shots on your workstation

You can do all those things from your PC or laptop remotely.


Why monkeyrunner?

This is primarily designed to test applications and devices at the functional/framework level and for running unit/functional test suites.


What is unique about monkeyrunner?

Unique features of monkeyrunner includes

  • Multiple device control

  • Functional testing with screen capture

  • Regression testing – run an application against a particular result

  • Extensible automation

What is under the hood? 

It uses Jython, an implementation of python that uses the Java programming language.

What is more on this tutorial?

I will discuss more about running and configuring monkeyrunner in the following tutorial.


8 thoughts on “Android testing with monkeyrunner – a monkeyrunner tutorial

  1. Pingback: monkeyrunner tutorial – Running tests on multiple devices with monkeyrunner « chanakaudaya

    • hi uday am very new to tiz tool ,if i run my python script usind adb shell monkey -p -v 100 smething and all am getting y

  2. from import MonkeyRunner, MonkeyDevice
    device = MonkeyRunner.waitForConnection()
    package = “”
    activity = “.MusicBrowserActivity”
    runComponent = package + “/” + activity
    MonkeyRunner.sleep(2)“KEYCODE_BACK”, MonkeyDevice.DOWN_AND_UP)

    tiz is my file


    import org.python.core.PyInteger;
    import org.python.core.PyObject;
    import org.python.core.PyString;

    public class MonkeyHelper {

    public static void tap(MonkeyDevice device, int x, int y) {
    PyObject[] args = { new PyInteger(x), new PyInteger(y),
    new PyString(TouchPressType.DOWN_AND_UP) };
    device.touch(args, null);

    public static void press(MonkeyDevice device, String key) {
    String keyCode = “KEYCODE_” + key.toUpperCase();
    PyObject[] args = { new PyString(keyCode), new PyString(TouchPressType.DOWN_AND_UP),
    new PyString(“”) };, null);

    private static void sleep(long millis) {
    try {
    } catch (InterruptedException e) {

    tiz is my plugin file

    import org.python.util.PythonInterpreter;


    public class Plugin implements Predicate {

    public boolean apply(PythonInterpreter python) {

    python.set(“hello”, “Hello, monkeyrunner!”);

    return true;


    tiz is my touchpresstype class


    public class TouchPressType {
    public static final String DOWN_AND_UP = TouchPressType.DOWN_AND_UP;


    if i run using monkeyrunner its executing
    same thing if i run from adb shell monkey -p -v 100 am not getting

    actually i want to just launch the music player and exit through adb how to do it

  3. Hello my family member! I want to say that this post is awesome,
    great written and come with almost all vital infos.
    I’d like to peer extra posts like this .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s