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.
    • http://developer.android.com/tools/help/MonkeyDevice.html 
  • 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.  
    • http://developer.android.com/tools/help/MonkeyImage.html 
  • Monkey Runner : It captures screen image, comparing two MonkeyImage objects and writing an image to a file.
    • http://developer.android.com/tools/help/MonkeyRunner.html

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 monkeyrunner_recorder.py monkeyrunner_playback.py available here: 
    • https://github.com/miracle2k/android-platform_sdk/tree/master/monkeyrunner/scripts
  • 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 \ monkeyrunner_recorder.py


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 (test.mr example) 
- Close the recorder interface and reopen the command prompt 
- Type: monkeyrunner c:\monkey\monkeyrunner_playback.py  c: \monkey\test.mr 
- 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):
        eraseLog(self.device)

        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 = self.device.shell('pm path ' + packagename)
            if apk_path.startswith('package:'):
                print"App is already installed."
            else:
                print"App not installed, installing APKs..."
                self.device.installPackage(apk)

            print componentname

            self.device.startActivity(component=componentname)

            MonkeyRunner.sleep(5)

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

            #Simulate Device Events Home, Back and Menu buttons

            self.device.press('KEYCODE_HOME', 'DOWN_AND_UP')
            MonkeyRunner.sleep(5)
            self.device.startActivity(component=componentname)
            MonkeyRunner.sleep(5)
            self.device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)
            MonkeyRunner.sleep(5)
            self.device.startActivity(component=componentname)
            MonkeyRunner.sleep(5)
            self.device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

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

            self.device.removePackage(apk)

if __name__ == "__main__":
    main()

- Name this code as testDroid.py. 
- 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\testDroid.py

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


7 comments:

  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

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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

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

    ReplyDelete