Robotium Framework for Android UI Test Automation

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated May 9, 2025

Use this tutorial as a comprehensive guide to using the Robotium Android Application UI Testing Tool.

Robotium is the Android Test Automation framework for testing native and hybrid Android applications. It provides a simple API to write UI automation scripts. However, several tools are available for testing Android applications. Robotium is the most commonly used Android testing tool.

Robotium can write functional, system, and user acceptance test cases. You can run Robotium test cases on the Android emulator and a real Android device.

Robotium Test Automation Tool

Robotium Test Automation Tool

Suggested Reading => Beginner’s Guide to Mobile Application Testing

Benefits Of Robotium

  • Easy to write
  • Simple API (all methods are available only in Solo Class)
  • Automatic delays & timings
  • No need to write code when navigating from one activity to another activity
  • Test Android native apps and the hybrid app
  • Able to handle multiple Android activities
  • Less time to write tests as the API is simple
  • Test cases are robust because of runtime binding to UI components
  • Fast test case execution
  • Integrates easily with Maven and ANT

Pre-Requisites

#1) Download and Install JAVA

  • Download Java
  • Add Java libraries to the PATH and set JAVA_HOME to the root of the Java installation directory in your environment.

#2) Download ADT Bundle

  • Download the ADT bundle from this page.
  • Extract the ADT bundle zip and put it in a folder.
  • Set ANDROID_HOME to the root of the ADT bundle folder in your environment.

Creating A Robotium Project

To make a project, you simply need to follow a few steps.

Step #1: Open Eclipse, which contains your Android application to be tested.

Android application

Step #2: Right-click on the Android project -> Android Tools and click on the new test project.

Step #3: Give a name for the test project and click the Next button.

Create Android Project

Step #4: Select the application under test as the target and click the Finish button.

Select test target

Step #5: A Test project will be created in the Eclipse workspace.

Step #6: Download the Robotium solo jar.

Step #7: Right-click on the test project in the workspace. Go to the Build path and click Configure build path.

Step #8: Switch to the libraries tab, click on the “Add External jars” option, browse the downloaded Robotium jar file, add it to the libraries, and click “OK”.

Java Build Path

Step #9: The Robotium Test project is created successfully. Now we can create classes for the project and start writing the test cases.

Creating Robotium Class

#1) Right-click on the package under the src directory on the test project, and create a new class.

#2) Import the Main Activity class to the Robotium test project.

Syntax:

import com.sasi.attendanceproject.Home;

#3) The new class will inherit properties from the ActivityInstrumentationTestCase2 class

public class AttendanceTest extends
ActivityInstrumentationTestCase2 <home>

Note: Here, Home is the activity to be tested in the Android application.

#4) Create an instance for the Solo class as below

private Solo solo;

Create a constructor for the test class following the given format.

public AttendanceTest() {
    super(Home.class);
    // TODO Auto-generated constructor stub

#6) Create setUp and tearDown methods, as shown below:
setUp method is used to initiate the Instrumentation

public void setUp()throws Exception
{
   solo=new Solo(getInstrumentation(), getActivity());
}

After completing the test, the Teardown method is used to close the activity.

public void tearDown() throws Exception
{ solo.finishOpenedActivities();
}

Some Robotium Methods

#1) assertCurrentActivity (text, Activity)
This method verifies whether the current activity is the activity that is passed as the send parameter.
Syntax:

solo.assertCurrentActivity(“Current Activity”, Home.class);

#2) clickOnButton(text)
This method will click on the button with the specified text.
Syntax:

solo.clickOnButton(“ADMIN”);

#3) clickOnButton(int)
This method will click on the button with the specified index.
Syntax:

solo.clickOnButton(2);

#4) waitForText(text)
This method will wait until the text appears on the activity.
Syntax:

solo.waitForText(“Creating New Password”);

#5) enterText(int, text)
This method will type the text passed as the second parameter to the specified index edit box.
Syntax:

solo.enterText(0,”test”);

#6) clickOnCheckbox(int)
This method will click on the checkbox with the index.
Syntax:

solo.clickOnCheckBox(0);

#7) clickOnRadioButton(int)
This method will click on the Radio button with the given index.
Syntax:

solo.clickOnRadioButton(1);

#8) clickOnImage(int)
This method will click on the image with the given index.
Syntax:

solo.clickOnImage(1);

#9) clearEditText(int)
This method allows you to remove the text in the edit box based on the given index.
Syntax:

solo.clearEditText(0);

#10) waitForText(text)
This method will wait until the text appears on the activity.
Syntax:

solo.waitForText(“Robotium”);

Suggested Read =>> What is Regression Testing?

Example Program

Locating Elements in Android Application

Step #1: Open Eclipse containing the Android Application to be Tested.

Step #2: Expand the project. Go to the res folder. Layout the folder and double-click on the activity XML file you want to automate. It will open the designed activity in the Eclipse editor.

Step #3: Locating Button Element

  • Locating Element By ID

Click on the Element you want to locate. On the right side properties panel, you can find the ID of that element. (ignore @+id/)

properties panel

In the above figure, the ID of the element is btnadmin.

The corresponding Robotium code to locate this button is:

solo.clickOnButton(R.id.btnadmin);
  • Locating Element By Position
robotium tutorial-6

In case, if the ID is not available, then you can locate the element using the order. If the button is in the second position, then you can locate the button by,

solo.clickOnButton(1);
  • Locating Element By Text
locate the element

Even you can click on the button using the text displayed on the button.

Solo.clickOnButton(“ADMIN”);
  • Locating Text box Element

Using a similar way, click on the text box you want to enter data and find the position of the text box from the right side.

enter data
Solo.enterText(0,”Anitha”);
//Username textbox is in 1st position
Solo.enterText(1,”test”);  
//Password textbox is in 2nd position
  • Locating a Radio Button Element

Click on the radio button element. You want to click and find out the position of the radio button.

 Radio Button Element
Solo.clickOnRadioButton(1);
//It will location radio button in the second position.

Sample Code

package com.sasi.attendanceproject.test;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
 
import com.robotium.solo.By;
import com.robotium.solo.Solo;
import com.robotium.solo.WebElement;
import com.sasi.attendanceproject.Home;
 
public class AttendanceTest extends ActivityInstrumentationTestCase2<Home>{
    private Solo solo;
    public AttendanceTest() {
        super("com.sasi.attendanceproject.Home",Home.class);
        // TODO Auto-generated constructor stub
    }
 
    public void setUp()throws Exception{
        solo=new Solo(getInstrumentation(),getActivity());
    }
    public void testAttendance()throws Exception{
        //solo.assertCurrentActivity("Current Activity", Home.class);
        solo.waitForWebElement(By.id("btnadmin"));
        solo.clickOnButton("ADMIN");
        solo.clickOnButton(0);
        solo.waitForText("Creating New Password");
        solo.enterText(0, "test");
        solo.enterText(1, "test");
        solo.clickOnButton("Okay");
        solo.waitForText("Attendance Login");
        solo.enterText(0, "Anitha");
        solo.enterText(1, "test");
        solo.clickOnButton("Login");
        solo.waitForWebElement(By.id("btnaddnew"));
        solo.clickOnButton("Add New Details");
        solo.waitForText("Enter the Employee Details");
        solo.enterText(0, "Anitha");
        solo.enterText(1, "6");
        solo.enterText(2, "Testing Engineer");
        solo.clickOnRadioButton(1);
        solo.clickOnButton("Okay");
        solo.waitForWebElement(By.id("tvempID"));
        System.out.println(solo.getText(0));
 
    }
    public void tearDown()throws Exception{
        solo.finishOpenedActivities();}}

Also Read =>> Complete Guide to start Automation Testing

Executing The Robotium Project

From Eclipse:

Right-click on the project and select Run As -> Android JUnit Test

From Command Prompt:

Step #1: Cd to your Android Test Project Directory

Step #2: Run the following command:

adb shell am instrument -w <<package name of your test
class>>/android.test.InstrumentationTestRunner
robotium tutorial-10

Conclusion

Robotium is the most commonly used Android Test automation tool. No specific configuration code is required to execute Robotium test cases on real devices, as they can be run on both the Android Emulator and real devices.

Robotium can also be easily written in the Maven project and run through continuous integration tools. Thus, Robotium is useful for writing easy/simple Android Test automation scripts.

Recommended reading => 5 Mobile Testing Challenges and Solutions

About the author: This is a guest post by Anitha Eswari. She is currently working as a senior testing engineer with over three years of extensive experience in web and mobile automation testing. She has sound knowledge of various automation testing tools, including mobile testing tools like Robotium and Appium.

In our next article, we will discuss more on Selendroid Tutorial.

Was this helpful?

Thanks for your feedback!

Recommended Reading

  • Kobiton tutorial

    Simple Automation Testing using Kobiton: In this tutorial, we will cover simple automation with the Kobiton tool and, we will also see how to run parallel tests with Kobiton. Step by Step instructions for each step involved in the automation and execution process are explained in simple terms along with…

  • Android Applications TestComplete

    Learn how to automate Android Applications using TestComplete (Part-III) in this tutorial: In the last two TestComplete tutorials, we learned the TestComplete introduction and Data Driven Testing using TestComplete. In this tutorial, let us continue learning this tool with a new feature - how to automate Android Applications. In this modern world,…

  • Security Testing of Web Applications

    How to make sure your web application is secure before release? Web site security testing is important part of software testing life cycle like other functionality and performance testing. This article will guide you with different type of attacks on web applications and information on how to perform security testing…

  • Geb Browser automation solution

    Here is an in-depth tutorial on the process of browser automation through Geb Tool for your benefit: Geb (pronounced "jeb") is the answer to any kind of browser automation challenges. It is a very effective tool to perform automation testing over the web. Geb originated out of the need to…

  • user interface testing

    A Complete Guide to GUI Testing: User Interface Testing Tutorial What is GUI Testing? GUI Testing is a process of testing the application's graphical user interface to ensure proper functionality as per the specifications. It involves checking the application components like buttons, icons, checkboxes, color, menu, windows etc. Visual dynamics…


READ MORE FROM THIS SERIES:



31 thoughts on “Robotium Framework for Android UI Test Automation”

  1. “Open Eclipse containing Android Application to be Tested”
    What the hell does this mean? Can you explain? I have an app to test and how can I open it in eclipse?

    Reply
  2. Hi,
    Can we use Robotium with Android Studio?
    Is Robotium Test Recorder is free or licensed?
    If YES, then how? Can please describe the steps?
    Are the test scripts are generated automatically ot do we have to write those?

    Reply
  3. Good one but whats the app we are going to test here. How we have attached the app (apk – android package kit).

    Yet to give a try for the practical

    Reply
  4. HI Thanks for post,
    i started for my project, but project is completely based on web-services including UI and data are dynamic so to identify a dynamic UI and data. I have check layout there is no images and data. so is any other way to identify the property’s of the dynamic UI & data

    Reply
  5. Very helpful article. I have used your tutorial to be able to create a Test class that is able to test Activities for my Application, now my challenge is that i have both Activities and fragment Classes, how do i create test for that freagments ??

    please help, tell me how to go abpout it ..

    Reply
  6. Thanks a lot for the description. I am a beginner. I would like to know what level java knowledge is required in Robotium? please do answer.

    Reply
    • Hi Anitha,

      iam new to mobile app testing. i need ur support in real time installation and testing the performaance of mobile app…can u please help..please leave a message at 8977168683

      Reply
  7. Its kick to start Mobile Automation….
    Easy to start for beginners….

    most awaited for post happy to see it…..

    thank you Anitha Eswari & Vijay.

    Reply
  8. Hi anitha, I have big doubt pls help!!! is that possible to automate the cache, performance related test cases. If it is so can u pls suggest me how to do that.

    Reply
  9. Hi Anitha,

    Thanks for sharing the good information about mobile automation testing.
    Can you please tell me if somebody is providing online training on core java,robotium , monkey talk and appium.
    I want to learn all these.

    Reply
  10. Hi…i want to learn about automation testing..so i really dont know much about this..here Pre-requisities states to install java and include java libraries…did it mean eclipse..i don’t understand how this can be done..can u pls explain it

    Reply
  11. What if i have two emulator running, what will be the command line and how do i identify multiple emulator. I want to run the same test case for multiple emulator.

    Reply

Leave a Comment