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