Android testing with monkeyrunner – a monkeyrunner tutorial part II

I hope that now you have an idea about what is monkeyrunner and what you can do with it. Let’s start doing them.


How to install?

Oh .. oo .. you don’t need to install it separately. It comes with Android SDK package. Inside your android-sdk-windows or linux package, inside tools directory, you can find this tool monkeyrunner.


How to run monkeyrunner?

If you want to run monkeyrunner from anywhere within the command line, you need to add the path of the monkeyrunner directory(\tools ) to your system PATH variable. changing path variable can be seen in this simple post .



once you have the PATH variable set, you are ready to go…. But wait .. you should have something to run with monkeyrunner. It is none other than a python script.

Let’s write a simple python script to test your application.


How to write your first script? 

monkeyrunner API provides 3 modules which are located in namespace. You will be using these modules to run your tests with monkeyrunner tool.

  • MonkeyRunner: A class of utility methods for monkeyrunner programs. This class provides a method for connecting monkeyrunner to a device or emulator. It also provides methods for creating UIs for a monkeyrunner program and for displaying the built-in help.

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

  • MonkeyImage: 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.

If you can’t understand what these modules are for and what you are going to do with them, It’s OK. you will know about them as you read the next section.

  1. First you need to import the modules which you are using in this script

from import MonkeyRunner, MonkeyDevice

  1. Then you have to connect to the device which you are running your test

device = MonkeyRunner.waitForConnection()

  1. Then you need to install the application package to the device


(Here the folder name is given as a relative path. Yamba folder should be located in the same directory in which you are currently in. You can give the absolute path as well.

For Windows users,

you can give the path as (‘C:\\Program Files\\Yamba\\bin\\Yamba.apk’) as your absolute path.

  1. set the package and the activity to be started

package = ‘’
activity = ‘.MainActivity’
runComponent = package + ‘/’ + activity

  1. Run the activity on the device


  1. Wait for 2 seconds before sending the next command


  1. Send a menu button press event‘KEYCODE_MENU’, MonkeyDevice.DOWN_AND_UP)

  1. Wait for 2 seconds before sending the next command


  1. Send a touch event to a button located at bottom center of an HVGA device

device.touch(200, 390, ‘DOWN_AND_UP’)

  1. Wait for 2 seconds before sending the next command


  1. Take a screenshot of the resulting window

result = device.takeSnapshot()

  1. Write the screenshot to a file


Name this script as and save it on your current directory. (You can save it anywhere…)


Now it looks like this

# Imports the monkeyrunner modules used by this program

from import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object

device = MonkeyRunner.waitForConnection()

# Installs the Android package. Notice that this method returns a boolean, so

# you can test to see if the installation worked.


# sets a variable with the package’s internal name

package = ‘’

# sets a variable with the name of an Activity in the package

activity = ‘.MainActivity’

# sets the name of the component to start

runComponent = package + ‘/’ + activity

# Runs the component


# Wait for few seconds


# Presses the Menu button‘KEYCODE_MENU’, MonkeyDevice.DOWN_AND_UP)

# Wait for few seconds


#Touch the new status button

device.touch(200, 390, ‘DOWN_AND_UP’)

# Wait for few seconds


# Takes a screenshot

result = device.takeSnapshot()

# Wait for few seconds


# Writes the screenshot to a file


Then on the command line or in a terminal window type this command

monkeyrunner -v ALL

(wait … what is this -v ALL thing …

-v is for verbose mode

ALL is for printing all the messages from monkeyrunner tool )


now you can see that the test is running on your device(s) or emulator(s).


cheers !!!!!!!!!



26 thoughts on “Android testing with monkeyrunner – a monkeyrunner tutorial part II

  1. Just wish to say your article is as surprising. The clearness for your post is simply spectacular and that i can suppose you’re knowledgeable on this subject. Fine together with your permission allow me to grasp your feed to stay up to date with forthcoming post. Thanks a million and please continue the enjoyable work.

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

  3. can you share the apk file ( I need to know the ” package = ‘’
    activity = ‘.MainActivity’
    runComponent = package + ‘/’ + activity ” )

    • Currently i don’t have this yamba.apk file. It is not a big issue. You can use any apk file which is created during the build of your project. You need to identify the package name and the Activity you are going to test. That’s all you need.

  4. Hi,

    I dont have much info regarding monkeyrunner.

    I have written test cases for my project using Robotium. but for couple of scripts i’m thinking of monkeyrunner. Can we combine robotium and monkeyrunner test case in one project?


  5. Thanks for the article. On thinking further with monkey runner, for providing voice inputs to the device, which method of MonkeyDevice, MonkeyRunner, MonkeyImage can be thought of.

    Thanks in advance.

  6. you’re in point of fact a just right webmaster. The site loading velocity is amazing. It seems that you are doing any unique trick. Also, The contents are masterwork. you have done a great task in this subject!

  7. Superb site you have here but I was wanting to know if you knew
    of any community forums that cover the same topics discussed in this article?
    I’d really like to be a part of group where I can get responses from other experienced individuals that share the same interest. If you have any suggestions, please let me know. Thanks a lot!

  8. Thank you *so* much for this! Helped me a great deal in the early days of learning Monkeyrunner.

    Question though — how do you verify a button press actually happened?

  9. Thanks for such a detailed doc. however while trying to execute my python script I am getting an error on line 2(device = MonkeyRunner.waitForConnection()).

    Error: Adb rejected adb port forwarding command: cannot bind socket.

    I have tried manually killing and restarting ADB server, I also tried port forwarding but not sure if I was doing it right.I also restarted my m/c as well as the device. I tried the same code in an emulator as well. It is constantly giving the same error.

    What other solution you would recommend?

  10. I’ve been exploring for a bit for any high-quality articles or blog posts in this kind
    of space . Exploring in Yahoo I at last stumbled upon
    this site. Studying this info So i’m satisfied to express that I have a very excellent uncanny feeling I discovered exactly
    what I needed. I most indubitably will make sure to do not overlook
    this web site and give it a glance on a relentless basis.

  11. Αɗmiring the Һarԁ work you put ntߋ your site andd in deƿth information youu provide.
    It’s nice too comе achross a blog everу once in a while that isn’t the
    same old rehasheԀ information. Εxcellent read!
    Ӏ’ve saved yoսr site and I’m adding your RSS feeds to myy Gogle account.

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 )

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