Tutorial details

Android user interface testing with Robotium | App Code for Sale | Preview

Android user interface testing with Robotium | Android Tutorial

How to test Android applications with the Android Robotium testing framework

Overview PAGE TOP

Lars Vogel

Version 1.5

Copyright © 2011, 2012, 2013 Lars Vogel

12.06.2013

table_1.JPG

Android Robotium

This tutorial describes how to test Android applications with the Android Robotium testing framework.

1. Robotium PAGE TOP

Robotium is an extension of the Android test framework and was created to make it easy to write user interface tests for Android applications. Robotium allows you to define test cases across Android activities.

The homepage of Robotium is located under the following URL: Robotium homepage,

2. Installation and usage of Robotium PAGE TOP

To use Robotium in your Android test project you need to download the Robotium.jar library from the Robotium Webpage and add the Robotium.jar to your project build path.

3. Using Robotium PAGE TOP

The main class for testing with Robotium is Solo. Solo is initialized with the instrumentation of the testcase and the first activity to test.

package de.vogella.android.test.target.test;

import junit.framework.Assert;
import android.test.ActivityInstrumentationTestCase2;

import com.jayway.android.robotium.solo.Solo;

import de.vogella.android.test.target.SimpleActivity;
import de.vogella.android.test.target.SimpleListActivity;

public class SimpleActivityTest extends
    ActivityInstrumentationTestCase2<SimpleActivity> {

  private Solo solo;

  public SimpleActivityTest() {
    super(SimpleActivity.class);
  }

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


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

Solo provides methods to call the Android user interface. The following table lists several of these methods.

Table 1. Test methods on Solo

table2.JPG

The following shows an example for the Robotium test method.

// Check that we have the right activity
solo.assertCurrentActivity("wrong activity", SimpleActivity.class);

// Click a button which will start a new Activity
// Here we use the ID of the string to find the right button
solo.clickOnButton(solo.getString(R.string.button1));
// Validate that the Activity is the correct one
solo.assertCurrentActivity("wrong activity", SimpleListActivity.class);
solo.clickInList(1);
// searchForText has a timeout of 5 seconds
assertTrue(solo.waitForText("Android")); // Assertion
solo.clickInList(2);
assertTrue(solo.waitForText("iPhone")); // Assertion
solo.clickInList(3);
assertTrue(solo.waitForText("Blackberry")); // Assertion
solo.goBack();
solo.clickOnButton("Button2");
solo.clickOnButton("Button3");

// Open the menu
solo.sendKey(Solo.MENU);
solo.clickOnText("Preferences");
solo.clickOnText("User");
solo.clearEditText(0);
Assert.assertTrue(solo.searchText(""));
solo.enterText(0, "http//:www.vogella.com");
Assert.assertTrue(solo.searchText("http//:www.vogella.com"));
solo.goBack();

To test internationalized strings you can access the string resources file from the project under test via the getString(id) method. For example:

// Here we use the ID of the string to find the right button solo.clickOnButton(solo.getString(de.vogella.android.test.target.R.string.button1));

4. Executing tests PAGE TOP

To run a Robotium test from Eclipse, right-click the test class and select Run-As → Android JUnit Test

You can run Robotium tests also from the command line.

adb shell am instrument 
  -w de.vogella.android.test.tester/android.test.InstrumentationTestRunner 

5. Tutorial Robotium PAGE TOP

Create an Android project called com.vogella.android.test.robotium.target. Display three buttons in the first activity. If the first button is pressed start another activity which displays a list.

Create a test project called com.vogella.android.test.robotium.targetTest. Create a folder called libs and place the Robotium jar into it. ADT should add this JAR file automatically to the build path of your project.

table392.JPG

Define the following test class. Adjust your Andriod application until the test passes correctly.

package de.vogella.android.test.target.test;

import junit.framework.Assert;
import android.test.ActivityInstrumentationTestCase2;

import com.jayway.android.robotium.solo.Solo;

import de.vogella.android.test.target.SimpleActivity;
import de.vogella.android.test.target.SimpleListActivity;

public class SimpleActivityTest extends
    ActivityInstrumentationTestCase2<SimpleActivity> {

  private Solo solo;

  public SimpleActivityTest() {
    super(SimpleActivity.class);
  }

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

  public void testPreferenceIsSaved() throws Exception {
    // Check that we have the right activity
    solo.assertCurrentActivity("wrong activiy", SimpleActivity.class);

    // Click a button which will start a new Activity
    // Here we use the ID of the string to find the right button
    solo.clickOnButton(solo
        .getString(de.vogella.android.test.target.R.string.button1));
    // Validate that the Activity is the correct one
    solo.assertCurrentActivity("wrong activiy", SimpleListActivity.class);
    solo.clickInList(1);
    // searchForText has a timeout of 5 seconds
    assertTrue(solo.waitForText("Android")); // Assertion
    solo.clickInList(2);
    assertTrue(solo.waitForText("iPhone")); // Assertion
    solo.clickInList(3);
    assertTrue(solo.waitForText("Blackberry")); // Assertion
    solo.goBack();
    solo.clickOnButton("Button2");
    solo.clickOnButton("Button3");

    // Open the menu
    solo.sendKey(Solo.MENU);
    solo.clickOnText("Preferences");
    solo.clickOnText("User");
    solo.clearEditText(0);
    Assert.assertTrue(solo.searchText(""));
    solo.enterText(0, "http//:www.vogella.com");
    Assert.assertTrue(solo.searchText("http//:www.vogella.com"));
    solo.goBack();

  }

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

Reference PAGE TOP

http://www.vogella.com/articles/Robotium/article.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.