Android Robotium
This tutorial describes how to test Android applications with the Android Robotium testing framework.
Table of Contents
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 Androidactivities.
The homepage of Robotium is located under the following URL: Robotium homepage,
To use Robotium in your Android test project you need to download the
Robotium.jar
library from theRobotium Webpage and add the Robotium.jar
to your project build path.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
Method | Description |
---|---|
solo.getView(int id) | Searches for the view with the specified ID in the current activity |
solo.assertCurrentActivity(text, Activity.class) | Ensure that the current activity equals the second parameter |
getCurrentActivity() .getFragmentManager() .findFragmentById() | Searches for a Fragment |
waitForText(text) | waits for a text on the screen, default timeout 5 secs |
clickOnButton(text) | clicks on a button with the "text" text |
sendKey(Solo.MENU); | Sends the menu key event |
clickOnText(text) | Search for text in the current user interface and clicks on it |
enterText() | Enters a text |
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 |
clickOnSearch() | Allows to click on part of the screen |
goBack() | Pressed the back button |
setDatePicker() | sets the date in a DatePicker |
isCheckBoxChecked() | checks if the checkbox is checked |
takeScreenshot() | saves a screenshot on the device in the i /sdcard/Robotium-Screenshots/ folder. Requires write theandroid.permission.WRITE_EXTERNAL_STORAGE permission in the AndroidManifest.xml of the application under test. |
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));
To run a Robotium test from Eclipse, right-click the test class and select
→ You can run Robotium tests also from the command line.
adb shell am instrument
-w de.vogella.android.test.tester/android.test.InstrumentationTestRunner
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.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();
}
}
No comments:
Post a Comment