May 15, 2014

Testing Android Applications using MonkeyRunner

Monkeyrunner  is a tool from Android SDK. It provides an API using commands to execute tests in Android devices or Android emulators. It is possible to write a Python program that installs an Android application and perform it actions. 
The API has the following classes of commands: 

  • Monkey Device: it provides connection to a device or emulator.
  • Monkey Image: It can run packages, provides methods for installing and uninstalling packages, start an Activity, and send keyboard or touch events to an application.  
  • Monkey Runner : It captures screen image, comparing two MonkeyImage objects and writing an image to a file.

MonkeyRunner Example

To use the  monkeyrunner, it is necessary to include in the environment variable PATH the android tools directory. 
... \ sdk \ build-tools \ android-4.4; 
... \ sdk \ tools 

  • Create a folder called monkey in the c drive of the computer and put the scripts available here: 
  • Create subfolders images, apks-script, log and apks_to_test

  • Open the emulator android or conect your Android device in USB (usb_debug must eon)
  • Open a command prompt and type: 
  • monkeyrunner c: \ monkey \

The interface monkeyrunner recorder will appear. 
With the mouse, try to perform actions on the emulator screen, like opening an application. 
The actions will appear on the right of the recorder interface.

MonkeyRunner Recorder

In the recorder interface , save the scripts clicking on the menu Export Actions
- Save the script as a file extension .mr ( example) 
- Close the recorder interface and reopen the command prompt 
- Type: monkeyrunner c:\monkey\  c: \monkey\ 
- Press enter and note that the emulator will play back your recorded actions on Emulator or in the Android device.

If you prefer, it is possible to create scripts directly in python and run. in monkeyrunner.

Follow the python code bellow, it tests installation of an application, it simulates the buttons' actions Back, Home and Menu and then it generates a log file and saves screen image.  

class DroidTest:

    rootfolder = 'c:\\monkey\\'
    apkDir = rootdir + 'apks_to_test\\'
    imageDir = rootdir + 'images\\'
    logDir = rootdir + 'log\\'

    def __init__(self, device, count):
        self.device = device
        self.count = count

    def run(self):

        for apk in glob.glob(self.apkDir + '/*.apk'):
            print "Testing apk %s" % apk
            packagename,activity = get_package_activity_name(apk)
            componentname = packagename + "/." + activity

            apk_path ='pm path ' + packagename)
            if apk_path.startswith('package:'):
                print"App is already installed."
                print"App not installed, installing APKs..."

            print componentname



            image = self.device.takeSnapshot()
           image.writeToFile(self.imageDir + 'screenshot_' + packagename  + str(self.count) + '.png','png')

            #Simulate Device Events Home, Back and Menu buttons


            log(self.logDir + 'test' + str(self.count) +'.log', self.device);


if __name__ == "__main__":

- Name this code as 
- Put an apk in the folder \apks_to_test
- Start the emulator or Android device and then type the following command in prompt:  monkeyrunner c:\mokey\

The python program will run and it will execute the buttons tests.


  1. This is what I am looking. Really need this post for my research. Thank you so much for giving us a very a great views.Software Installation Automation

  2. Thank you for sharing such useful information. Your article helped me to understand career prospects in software testing industry. Testing is the part of the software development. It assists the programmers and developers to create stunning application catering the requirement of individuals. automation testing training in Chennai

  3. Thanks for your informative article on software testing. LoadRunner is popular automation software testing tool that used to validate a software application by generating actual load. Further, it gives precise information about a software application or environment. Loadrunner training institute in Chennai

  4. Really awesome blog. Thanks for sharing this informative blog. I wish to be a regular contributor of your blog. Can you please update your blog with latest testing trends in IT industries. Just now I have completed Software Testing Training in Chennai at a reputed training institutes. If you are looking for best Software Testing Training Institutes in Chennai reach FITA located at Chennai, India. Rated as No.1 placement and training center in Chennai. For more details visit this site.

  5. I have read your blog and i got a very useful and knowledgeable information from your blog.its really a very nice article. I did Loadrunner Training in Chennai. This is really useful for me. Suppose if anyone interested to learn Manual Testing Training in Chennai reach FITA academy located at Chennai Velachery.

  6. Good Information.
    Thanks a lot for sharing this with us, was a really interesting post. and I want to share information about the
    B.Tech | Ex-MBA | MCA | Diploma | Distance Learning Program
    B.Tech | Diploma |Engineering Courses

  7. Thanks for posting the useful information to my vision. Java is a programming language, now a day lots of websites & application created using java, because it’s more secure than others and reliable too. The popular
    JAVA Training in Chennai helps you to get your bright career.

    JAVA Training Institutes in Chennai | JAVA Course in Chennai

  8. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing. Android Training in chennai | Android Training chennai | Android course in chennai | Android course chennai

  9. The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers. Software testing institutes in Chennai