Friday, April 13, 2012

monkeyrunner tutorial – Running tests on multiple devices with monkeyrunner

OK.. Great … Now you know what is monkeyrunner and how to run tests with monkeyrunner. Let’s discuss about one important feature of the monkeyrunner tool. 

Running tests on multiple devices with a single python script.

It looks like a big ask. But that is as simple as drinking a Sri Lankan Tea …
By following my previous posts
now you are running some scripts with monkeyrunner. Let’s add some little bit of information to that script.

#First you need to import the modules which you are using in this script

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

#Then you have to connect to the device which you are running your test

device = MonkeyRunner.waitForConnection()

Wait here !!!!!!

In this script you are waiting for a connection to a running android device or emulator implicitly. Rather than doing like that, you can explicitly tell what emulator or device you may need to connect. You can do that like this.

# Timeout value for waiting for connection 10 seconds

timeout = 10000# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection(timeout, “emulator-5554″)

here you specify which device you need to connect by the id of the emulator or device.
 If you want to connect to another device within the same script, you can do like this

device2 = MonkeyRunner.waitForConnection(timeout, “emulator-5556″)

Then you can do all the operations you have done with device parameter to device2 parameter.
With this kind of scripting, you can run commands on as many devices you have with you. I believe in sample scripts in tutorials. Here is a sample script in action.

# Imports the monkeyrunner modules used by this program

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Timeout value for waiting for connection

timeout = 10000# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection(timeout, “emulator-5554″)

# Installs the Android package. Notice that this method returns a boolean, so
# you can test to see if the installation worked.
device.installPackage(‘/home/chanaka/workspace/SimpleIntentPreference/bin/SimpleIntentPreference.apk’)
# sets a variable with the package’s internal name
package = ‘com.chanaka.android.sip’
# sets a variable with the name of an Activity in the package
activity = ‘.SimpleIntentPreferenceActivity’
# 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(160, 460, ‘DOWN_AND_UP’)
# Wait for few seconds
MonkeyRunner.sleep(2)
# Connects to the current device, returning a MonkeyDevice object
device2 = MonkeyRunner.waitForConnection(timeout, “emulator-5556″)
# Installs the Android package. Notice that this method returns a boolean, so
# you can test to see if the installation worked.
device2.installPackage(‘/home/chanaka/workspace/SimpleIntentPreference/bin/SimpleIntentPreference.apk’)
# sets a variable with the package’s internal name
package = ‘com.chanaka.android.sip’
# sets a variable with the name of an Activity in the package
activity = ‘.SimpleIntentPreferenceActivity’
# sets the name of the component to start
runComponent = package + ‘/’ + activity
# Runs the component
device2.startActivity(component=runComponent)
# Wait for few seconds
MonkeyRunner.sleep(2)
# Presses the Menu button
device2.press(‘KEYCODE_MENU’, MonkeyDevice.DOWN_AND_UP)
# Wait for few seconds
MonkeyRunner.sleep(2)
#Touch the new status button
device2.touch(160, 460, ‘DOWN_AND_UP’)
# Takes a screenshot
result = device.takeSnapshot()
# Wait for few seconds
MonkeyRunner.sleep(2)
# Writes the screenshot to a file
result.writeToFile(‘/home/chanaka/status_update.png’,'png’)
#Touch the first preference
device2.touch(160, 30, ‘DOWN_AND_UP’)
#Touch the entre button
device2.press(‘KEYCODE_DPAD_CENTER’, MonkeyDevice.DOWN_AND_UP)
# Takes a screenshot
result2 = device2.takeSnapshot()
# Writes the screenshot to a file
result2.writeToFile(‘/home/chanaka/shot1.png’,'png’)


That's it. Cheers !!!!!!!!!!!!!!!

Android testing with Monkey tool – a comprehensive guide


If you are in the android scene for some time, you may have heard about this tool. Monkey tool is a simple but very important tool to test android applications for their stability. It is a command-line tool that you can run on any emulator instance or on a device. Basically it sends out random key events and clicks to the device or emulator. If you have done testing on WM devices, you may have used a similar testing tool called hopper. In this tutorial i am hoping to guide you through the basic operations you can do with monkey tool and also some more advance and specific tasks.
The Monkey includes a number of options, but they break down into four primary categories:
  • Basic configuration options – such as setting the number of events to attempt.
  • Operational constraints – such as restricting the test to a single package.
  • Event types and frequencies – such as key events, click events and time interval between events.
  • Debugging options.
When the Monkey runs, it generates events and sends them to the system. It also watches the system under test and looks for three conditions, which it treats specially:
  • If you have constrained the Monkey to run in one or more specific packages, it watches for attempts to navigate to any other packages, and blocks them.
  • If your application crashes or receives any sort of unhandled exception, the Monkey will stop and  report the error.
  • If your application generates an application not responding(ANR) error, the Monkey will stop and report the error.
Depending on the verbosity level you have selected, you will also see reports on the progress of the Monkey and the events being generated.
That’s enough …. where is the actual test? ….
How to run monkey?

As mentioned above, monkey is a command line tool. In a command line or in a linux terminal window, you can run your first monkey test with the following command.
$>adb shell monkey -v 100

adb - connect to the device
shell - start the terminal (command line) on the device/emulator
monkey - start the monkey tool
-v - verbose mode
100 - number of random events to be sent

You will see some messages displaying on your command line window and if you are fortunate you may experience that monkey tool exits before it completes 100 events due to some exception or ANR scenario. But that will depend on the random events it sends and the applications you have.
How to run monkey on my own application?

OK.. Now you know how to run monkey and what are things it can do up to some level. Let’s run this test entirely on your own application. Let’s you have installed the package com.marakana.android.yamba” and you want to test this application against random clicks.
$>adb shell monkey -p com.marakana.android.yamba -v 500

-p - This will specify the application package you need to test.

How to run the same event sequence again?

Sometimes you may want to check whether a particular failure always happens or randomly happens in an application. In a such scenario, you may need to run the exact same event sequence with monkey tool. You can achieve that goal like this.
Run your first test with
$>adb shell monkey -p com.marakana.android.yamba -s 999 -v 500

Go back to the same status in the device and run the above command again

$>adb shell monkey -p com.marakana.android.yamba -s 999 -v 500

-s - This will give the random seed value to generate the random events.

Note: It is essential to start both the tests with the same status in the device. Otherwise It will not give you the same result even though it sends the same events.

This tool has many more features and you can learn about these features from the android developer guide here http://developer.android.com/guide/developing/tools/monkey.html

Cheers !!!!!!!

Monday, April 2, 2012

robotium button click not working solution

I also had this issue and i couldn't find a solution with robotium. Then i move to the android instrumentation provided with the SDK. What i have done is mentioned below. This worked without any issues for me. Here it is ..




First define a private variable for the button as a member variable
private Button msButton;  

 Then get the button instance with in the setup method.


mButton = (Button) solo.getCurrentActivity().findViewById(
                com.marakana.android.simple_intent.R.id.sendButton);


Then call the button click with in the UI thread in the test method as mentioned below.

//Test button method

public void testButtonClick() {
//Get the current activity and request to run onUI thread        
        solo.getCurrentActivity().runOnUiThread(new Runnable() {

            public void run() {

//Request focus for the button
                mButton.requestFocus();
            }
        });

//Send the button click event
        this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
       
    }

That's it .. Now you can test your button with other Robotium features.
Cheers !!!!!!!!

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

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