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 com.lava.music -v 100 smething and all am getting y

  2. from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
    device = MonkeyRunner.waitForConnection()
    device.installPackage(“/home/teju/Desktop/Music.apk”)
    package = “com.lava.music”
    activity = “.MusicBrowserActivity”
    runComponent = package + “/” + activity
    device.startActivity(component=runComponent)
    MonkeyRunner.sleep(2)
    device.press(“KEYCODE_BACK”, MonkeyDevice.DOWN_AND_UP)
    MonkeyRunner.sleep(2)

    tiz is my example.py file

    package com.lava.music.monkeyrunner;

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

    import com.android.monkeyrunner.MonkeyDevice;
    import com.android.monkeyrunner.core.TouchPressType;
    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);
    sleep(2000);
    }

    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(“”) };
    device.press(args, null);
    sleep(2000);
    }

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

    tiz is my plugin file
    package com.lava.music.monkeyrunner;

    import org.python.util.PythonInterpreter;

    import com.google.common.base.Predicate;

    public class Plugin implements Predicate {

    @Override
    public boolean apply(PythonInterpreter python) {

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

    return true;
    }

    }

    tiz is my touchpresstype class

    package com.android.monkeyrunner.core;

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

    }

    if i run using monkeyrunner example.py its executing
    same thing if i run from adb shell monkey -p com.lava.music -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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s