Tuesday 14 August 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 .


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



9 comments:

  1. Thanks for this post! In OS X 10.8 paths can be found in /etc/paths.

    And TouchPressType should be the same in touch() as press(). I.e MonkeyDevice.DOWN_AND_UP, not 'DOWN_AND_UP’ which renders an error.

    ReplyDelete
  2. Thanks for the Post. Just wanted to confirm, just .apk file is enough (or) do we need access to source code?

    ReplyDelete
  3. Hi,
    I am Sabari, I am trying to automate my application on Android device. I am new to MonkeyRunner and Python scripting.

    I want your help to develop a script to launch my app -> Compare with the resultant screen with available screen, either BMP/String comparison.

    If possible, Kindly share the script

    Thank you,

    ReplyDelete
  4. Hi,

    I have a android mobile application and i am able to launch that application by clicking the icon present in home screen.

    Is there any other way/Command to launch that application automatically.

    Need immediate solutions.

    Thanks,

    ReplyDelete
  5. Hi Subhashini,

    Is there any app. or command to retrieve the strings present in the Phone UI/application.

    I want to automate the Phone UI & functional testing. Can u help me on this.

    Thanks

    ReplyDelete
  6. Hi,
    How can we touch one item of an gridview in this manner? (the items are added programmatically)

    Thanks

    ReplyDelete
  7. It is a nice information shared. Keep updating on your posts.
    Mobile App Development Company in USA

    ReplyDelete
  8. The game industry is growing, especially among small, independent development companies. If you're looking for Game Development Companies , let's take a glimpse at some top game development.

    ReplyDelete