Methods & Tools Software Development Magazine

Software Development Magazine - Programming, Software Testing, Project Management, Jobs

Robotium - Testing Android User Interface

Renas Reda

Robotium is a test framework created to make it easy to write powerful and robust automatic UI test cases for Android applications. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities.

Robotium tests can be run on both emulator and device.

Web Site: http://Robotium.org
Version tested: 3.2.1
System requirements: Android SDK
License & Pricing: Free, Apache 2
Support: Issue tracker at http://code.google.com/p/robotium/issues/list,
Mailing list at https://groups.google.com/forum/?fromgroups#!forum/robotium-developers

Benefits of Robotium

  • Easy to write
  • Shorter code
  • Automatic timing and delays
  • Automatically follows current Activity
  • Automatically finds Views
  • Automatically makes own decisions, e.g. when to scrolls
  • Test execution is fast

Installation

Robotium is downloaded as a single JAR file, which can be placed in any folder. To actually start using Robotium, the JAR must only be put in the classpath of the test project.

Robotium can be used with all the different build automation tools. Perform the below steps to use Robotium with Maven:

1. Set up your project(s) as usual for Maven, see maven-android-plugin.

2. Add a dependency to Robotium in your test project:

<dependencies>
<dependency>
<groupId>
com.jayway.android.robotium</groupId>
<artifactId>
robotium-solo</artifactId>
<version>
3.2.1</version>
</dependency>
...
<</dependencies>

NB: For the version number, enter the version number of the latest release!

Usage

Robotium can be used both for testing applications where the source code is available and applications where only the apk is available (implementation details not known). For more information, instructions, and step by step tutorials see the Robotium website.

To use Robotium, create an Android tests project and add a test class. Usually the ActivitityInstrumentationTestCase2 test class is used to write Robotium test cases. However, Robotium is compatible with all the Android test classes. When writing Robotium test cases only one class is used: Solo.

The below example illustrates how a Robotium test case might look like when used in conjunction with ActivityInstrumentationTestcase2:

public class EditorTest extends ActivityInstrumentationTestCase2<EditorActivity> {
private Solo solo;
public EditorTest() {
super("com.test.editor", EditorActivity.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testPreferenceIsSaved() throws Exception {

solo.sendKey(Solo.MENU);
solo.clickOnText("More");
solo.clickOnText("Preferences");
solo.clickOnText("Edit File Extensions");

Assert.assertTrue(solo.searchText("rtf"));
solo.clickOnText("txt");
solo.clearEditText(2);
solo.enterText(2, "robotium");
solo.clickOnButton("Save");
solo.goBack();
solo.clickOnText("Edit File Extensions");
Assert.assertTrue(solo.searchText("application/robotium"));
}

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

Some Robotium commands

getCurrentActivity()

clickOnButton(String regex)

clickInList(int line)

enterText(int index, String text)

searchText(String regex)

waitForText(), waitForActivity(), waitForView()

clickOnMenuItem(String text)

goBack(), goBackToActivity(String name)

More information

More information on how to use Robotium can be found on the website. An example test project as well as various step by step tutorials with images can be downloaded from Robotium.org.

Gettings started: http://code.google.com/p/robotium/wiki/Getting_Started

Tutorials: http://code.google.com/p/robotium/wiki/RobotiumTutorials


More Software Testing Resources


Click here to view the complete list of tools reviews

This article was originally published in the Summer 2012 issue of Methods & Tools