Automated Testing of Application on Android using ROBOTIUM – Part 3
In continuation to my previous blog where we learned how do the basic setup and configuration, in this blog we will discuss how to create base test script, object identification and executing the test scripts in Android simulator.
Creating a Base Test Script
Create a Class file to create test script extending from ActivityInstrumentationTestCase2. We will need to import android.test.ActivityInstrumentationTestCase2 for creating junit test for Android applications. We will also need to import com.jayway.android.robotium.solo.Solo to write automation scripts using Robotium. Please see sample script below.
package com.demo.ags.androidmobile;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
@SuppressWarnings(“unchecked”)
public class SMS_Tester extends ActivityInstrumentationTestCase2 {
// Target Package ID identified using re-sign.jar or logcat
private static final String TARGET_PACKAGE_ID = “com.sa”;
//Starter activity name identified using re-sign.jar or logcat
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = “com.sa.SmsSend”;
private static Class<?> launcherActivityClass;
static{
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
// Constructure of SMSTester class to initialize the class
@SuppressWarnings(“unchecked”)
public SMS_Tester () throws ClassNotFoundException {
super(TARGET_PACKAGE_ID, launcherActivityClass);
}
private Solo solo;
// Intializing SOLO object and making to refer ApplicationToTest
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
// This methods contains the actual script to perform on Application
public void testCanOpenSettings(){
solo.pressMenuItem(0);
}
// this Method destroys the Solo object & closes the app
@Override
public void tearDown() throws Exception {
try {
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}
}
Make sure you have added Robotium- solo2.2.jar to your library path.
Object Identification
Robotium provides basic object identification methods Like Index and Visible Text. If we have access to the source code of the Application-Under-Test (AUT) (can be viewed in the R (Resource) file content) OR developers could provide details about ID’s assigned to each control, it makes development of test scripts much more robust and simpler. However it is not possible to find out the ID’s assigned to each control, we will have to use Index OR Visible Text properties to perform actions on the controls.
Typically there are many spy tools like firebug, flex spy, snoop etc. to identify object properties but in case of android applications, there are no such tools available to find out the properties of the controls.
IF we have to write script based on Index OR Logical text and there are two edit boxes on the screen, they will have index 0, Index1 respectively, in the order they appear on the screen.
To Enter Text in Editbox
solo.enterText(0,”9991111999”);
solo.enterText(1,”demouser”);
To Click on Command button having caption “Send SMS”
solo.clickOnButton(“Send SMS”);
Using Regular Expression for Object Identification
We can also use regular expressions to help with identification of objects for writing test scripts.
Example: if application contains UI as below.

Regular expression can also be used for performing actions as well as synchronizing the execution of the test scripts with AUT.
Actions: To click on button we can script as follows.
solo.clickOnButton(“.*List.*Checkboxes”);
Synchronization: To wait till the window having the text ‘www’ exist, we can script as below
solo.waitForText(“.*www.*”);
Application Deployment
Before proceeding to execution of scripts, deploy the application (apk) to test on Target Device (Simulator or Mobile Device).
Installation Syntax:
C: adb install <Pathapplication.apk>
OR
Use Android Injector is a small, easy to use application designed to enable you to quickly and easily install apps that you have downloaded to your computer in the form of “.apk” files onto your Android phone or device.
Download Source: http://download.cnet.com/Android-Injector/3000-18511_4-75337986.html
Script Execution
We can execute scripts either from within eclipse or from command prompt or from device (if Test APK is already deployed).
To run the test script from within Eclipse, Right click on Project and select Run as ->Android Junit Test.
Note: Before running make sure that AUT is deployed & having same signature as test project
To run the test script from Command prompt, use the following command:
C:> adb shell am instrument w com.Example.ApplicationTesting/android.test.InstrumentationTestRunner
To run the test script from device or emulator, click on OR choose Start Menu->Dev Tools->Instrumentation->select Test Project.

Hi all,
I try to run robotium from the ubuntu terminal and its works fine, but now i want to pass the arguments (like, wants to run the test 100 times or 200 times) at the time of running from the command line(terminal). How can i do that using ubuntu terminal??
Hi sukanta,
Please refer my blog:
http://allianceglobalservices.com/blog/automated-testing-of-application-on-android-using-robotium-part-4/#.T0vBvocgfQo
hi,
I want to do cts test, but it’s looking very complex to me. Can any one help me out by describing the cts test from the beginning. Means after setting up android sdk and avd plugins then what we have to do for doing CTS test. Please reply soon..