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 .

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

Android testing with monkeyrunner - a monkeyrunner tutorial

This is what you have been waiting for .. A simple tutorial about all the things you need to know about monkeyrunner testing tool provided by android SDK.

What is monkeyrunner?
It is a tool which provides an API for writing programs that control an android device from outside of android code.

What you can do with monkeyrunner?
You can write python programs to test the applications on one or more devices and/or emulators. You can do following things and more with monkeyrunner.
  • Installs an application or test package
  • Runs an application
  • Send keystrokes or touch events to it
  • Take screen shots of the user interface
  • Store screen shots on your workstation
You can do all those things from your PC or laptop remotely.

Why monkeyrunner?
This is primarily designed to test applications and devices at the functional/framework level and for running unit/functional test suites.

What is unique about monkeyrunner?
Unique features of monkeyrunner includes
  • Multiple device control
  • Functional testing with screen capture
  • Regression testing - run an application against a particular result
  • Extensible automation
What is under the hood?
It uses Jython, an implementation of python that uses the Java programming language.

What is more on this tutorial?
 I will discuss more about running and configuring monkeyrunner in my next tutorial ...

cheers !!!!!!!!!!!!

Sunday, March 25, 2012

What is testing on Android?

android .. android .. android ...
it is almost one of the most famous names in the world right now. 50% of people who are chanting this word might not know actually what it is. But that is the way things happen in modern world. people are convinced to buy what the companies produced, without considering the real need of that product. that's enough for that.

Let's talk about the serious stuff. I hope you know what android is and what you can do with that. You came here to learn about testing on android. As a beginning i will introduce you the concepts as i have understood.

As you know, Android is a software stack which provides a kernel, set of libraries and a runtime, application framework and set of basic applications. (If it is too much, just ignore it. those are for completeness).

Testing on android platform can be done in several layers. (ex: kernel level, runtime level, application framework level and application level).

Within these set of tutorials i am hoping to discuss about testing on application and application framework layers.

Android has provided a good testing framework based on Junit framework used by the Java as the default testing enviornment. We can develop any kind of test with this framework. monkeyrunner and monkey are tools provided with android system for automated testing.

  • monkey tool - this is a tool which can be used to test an application against a random clicks on the application (Like hopper tool in Windows mobile)
  • monkeyrunner - this is a python based test tool which can be used run python scripts for testing android applications
Rather than these tools provided by the android framework, there is an open source testing framework. Which is known as Robotium and you can use this library to develop automated test cases for your application.

My next tutorial will discuss about constructing android  junit samples and robotium samples.

cheers !!!!!!!!!!