UI Testing with Robotium


This framework is designed to provide black box tests for Android applications. This means that you test for expected outcomes instead of specific methods. It does require the application to be running in an emulator or device. Robotium builds off of the core Android integration testing libraries but provides an "extra" layer on top to make our testing easier with the Solo driver. Through the solo object, you can set values in input fields, click on buttons and get results from other UI components. Methods of JUnits Assert class can then be used to check those results.

Robotium Example

Let's take a look at writing blackbox integration tests with Robotium. First, you need to setup a "Test Project" much the same way as you would for the Android Testing Framework.

Next, we need to add the robotium jar to our Test Project. Download the latest "robotium-solo-X.X.jar". You need to add the robotium JAR to the Libraries on the projects Build Path.

Build Path

Now, within the integration test case, in the setUp method of your testing class, you can create a Solo object and specify the Activity to be started. The Solo object can then be used in your testXXX methods to get and set values in/from UI components and click buttons and such. A skeleton integration testing class with Robotium looks like:

// Import the resource object constant from our app for easy access
import com.codepath.example.simpleapp.R;

public class RobotiumActivityFunctionalTest extends
    ActivityInstrumentationTestCase2<FirstActivity> {
  // Solo Robotium helper object
  private Solo solo;

  public RobotiumActivityFunctionalTest() {

  // Setup test case with solo
  protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
  public void testSomething() {
    // Test something here

  // Finalize solo object
  public void tearDown() throws Exception {
    try {
    } catch (Throwable e) {

Now, let's write the tests for the basic behavior of our simple app which is to allow the user to enter text, hit the "Launch" button and then view that text in a second activity:

public void testStartSecondActivity() throws Exception {		
  final String fieldValue = "Testing Text";
  // Set a value into the text field
  solo.enterText((EditText) solo.getView(, fieldValue);
  // find button and click it
  // or solo.clickOnView(solo.getView(;

  // Wait 2 seconds for the start of the activity
  solo.waitForActivity(SecondActivity.class, 2000);
  solo.assertCurrentActivity("Should be second activity", SecondActivity.class);
  // Search for the textView
  TextView textView = (TextView) solo.getView(;
  // Validate the text on the TextView
  assertEquals("Text should be the field value", fieldValue, 

  // Return to the original activity
  // We have to manage the initial position within the emulator

The full source for this test can be found here. Compare this with the official Android testing and notice how much cleaner and clearer tests are with Robotium.

Run Tests

Now, in Eclipse we can right-click and select "Run As..." and then select "Android JUnit Test" and the tests will execute within the test runner. The tests should both pass and the "test bar" should be green.

Solo Actions

There are many actions which you can take during tests with Robotium via the Solo object including these which are the most common:

Method Description
getView(int id) Searches for the view with the specified ID in the current activity
assertCurrentActivity(text, Activity.class) Ensure that the current activity equals the second parameter
getCurrentActivity() Searches for the current activity and returns
waitForText(text) waits for a text on the screen, default timeout 5 secs
clickOnButton(text) clicks on a button with the "text" text
clickOnText(text) Search for text in the current user interface and clicks on it
enterText(editText, text) Enters text in the specified EditText
searchText(text) Searches for a text in the current user interface, return true if found
searchButton(text) Searches for a button with the text in the current user interface
goBack() Presses the back button
takeScreenshot() saves a screenshot on the device
waitForActivity(SecondActivity.class, 2000) Waits for an Activity matching the specified name.
waitForText(text, 2000) Waits for the specified text to appear.
waitForView( Waits for a View matching the specified resource id.

For more details, make sure to check the Robotium documentation.

Robotium in Google's Espresso way


Most of Robotium examples rely on ActivityInstrumentationTestCase2, which is already marked as deprecated. Instead, Use ActivityTestRule using the Android Testing Support Library.

Another reason to change from ActivityInstrumentationTestCase2 to ActivityTestRule is that it uses generated boilerplate code for initializing and finishing tests. Furthermore, @SmallTest, @MediumTest, and @LargeTest annotations have been deprecated but a timeout parameter can specify the expected duration of the text:

Instead of:

public void testStartSecondActivity() throws Exception {
    solo.waitForActivity("SecondActivity", 2000); 

We would use:

@Test(timeout = 2000)
public void startMainActivityIsProperlyDisplayed() throws InterruptedException {
    solo.waitForActivity("MainActivity", 1000);

Before we start using ActivityTestRule with Robotium testing, we need to need to change our existing configuration.


Start new Android Application project

For better experience create a new Android Empty Project and follows these steps:

Add the Android Testing Support Library dependencies in project's build.gradle file:

dependencies {
    androidTestCompile ''
    androidTestCompile ''
    androidTestCompile ''
    androidTestCompile ''

Define testInstrumentationRunner in project's build.gradle file

Don't forget about defining your testInstrumentationRunner runner! Otherwise your tests won't run.

Just add this line:

testInstrumentationRunner ""

Finally your build.gradle file should look like below:

apply plugin: ''
android {
  compileSdkVersion 24
  buildToolsVersion "24.0.1"
  defaultConfig {
     applicationId "com.example.piotr.myapplication"
     minSdkVersion 15
     targetSdkVersion 21
     versionCode 1
     versionName "1.0"
     testInstrumentationRunner ""
  buildTypes {
     release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
  dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     testCompile 'junit:junit:4.12'
     implementation ''
     implementation ''
     androidTestCompile ''
     androidTestCompile ''
     androidTestCompile ''
     implementation ''    

Create MainActivityTest testing class

Now we have all needed configuration to start test coding using Robotium.

Just create a new Java class and write the code below:

  public class MainActivityTest {

  private Solo solo;
  private static final String MAIN_ACTIVITY = MainActivity.class.getSimpleName();
  public MyActivityTestRule<MainActivity> mActivityRule = new MyActivityTestRule<>(MainActivity.class);
  public void setUp() throws Exception {
     solo = new Solo(mActivityRule.getInstrumentation(), mActivityRule.getActivity());
  @Test(timeout = 5000)
  public void checkIfMainActivityIsProperlyDisplayed() throws Exception {
    solo.waitForActivity("MainActivity", 2000);
              R.string.error_no_class_def_found, MAIN_ACTIVITY), MAIN_ACTIVITY);
    solo.getText("Hello World").isShown();

Running the tests

To run tests, right-click on the name of class or method and select Run[nameOfTest]``

Other annotations

As you may notice there are some new annotations to remember:

  • @RunWith() - describes which runner we would use in our test class.
  • @Rule - defines where your actual TestRule is defined
  • @Test - thanks to this annotation, you can name your test method as you would like to (i.e. no more testSomethingToDo)


Fork me on GitHub