Friday, March 30, 2012

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 .
http://chanakaudaya.blogspot.com/2012/03/how-to-change-path-variable-in-ubuntu.html



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 com.android.monkeyrunner 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 com.android.monkeyrunner 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
device.installPackage('Yamba/bin/Yamba.apk')

(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 = 'com.marakana.android.yamba'
activity = '.MainActivity'
runComponent = package + '/' + activity

  1. Run the activity on the device

device.startActivity(component=runComponent)

  1. Wait for 2 seconds before sending the next command

MonkeyRunner.sleep(2)

  1. Send a menu button press event

device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

  1. Wait for 2 seconds before sending the next command

MonkeyRunner.sleep(2)

  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

MonkeyRunner.sleep(2)

  1. Take a screenshot of the resulting window

result = device.takeSnapshot()

  1. Write the screenshot to a file

result.writeToFile('Yamba/status_update.png','png')



Name this script as test.py 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 com.android.monkeyrunner 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.
device.installPackage('Yamba/bin/Yamba.apk')
# sets a variable with the package's internal name
package = 'com.marakana.android.yamba'
# 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
device.startActivity(component=runComponent)
# Wait for few seconds
MonkeyRunner.sleep(2)
# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
# Wait for few seconds
MonkeyRunner.sleep(2)
#Touch the new status button
device.touch(200, 390, 'DOWN_AND_UP')
# Wait for few seconds
MonkeyRunner.sleep(2)
# Takes a screenshot
result = device.takeSnapshot()
# Wait for few seconds
MonkeyRunner.sleep(2)
# Writes the screenshot to a file
result.writeToFile('Yamba/status_update.png','png')

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

monkeyrunner -v ALL test.py

(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 !!!!!!!!!

2 comments:

  1. Kudos !! Nice tutorial to begin with . Keep posting and keep up the good work .

    ReplyDelete