Automating Android Native Apps testing using Robotium

One of the better test automation tools for Android native apps that have come out of the open source movement is Robotium.

Software QAs and testers familiar with Selenium will find it easy to find their way around the tool since it provides API calls to (a) get a UI element, (b) enter text to an input box, (c) perform asserts, (d) click on buttons. This means that once you’ve identified what these calls are and what calls to setup and tear-down, you’re off to a good start. There is a video on the Robotium website (see http://code.google.com/p/robotium/) showing a demo of the tool used to test a simple Android app.

Unlike Selenium 1.0 where most of us used the “record/playback” feature the most, most of the newer tools like Robotium requires the tester to write some Java code to write the tests. Robotium is also integrated with JUnit. This means that you can bring to bear most of the existing knowledge you have of writing JUnit tests.

There are some tools out there where we create add extra code to the app to have it basically work as a HTTP server taking in test commands eg. get label. A good example of this is the iPhone Driver from the WebDriver set of tools (this will be described in another post). Robotium also requires you to add code, but the test code and app code reside together.

Installing Eclipse and Android SDK

A major pre-requisite for working with Robotium is installing Eclipse and the Android SDK. See Setting up your Android Native App Test Environment.

Note: it is always useful to install example projects as it gives you a stable base to work with ie. at least 1 piece of code that should work ok. You can then use it as reference for future projects.

Installing Robotium Test Project
  1. The Robotium “Getting Started” page has a link to download the Example project – see http://code.google.com/p/robotium/wiki/Getting_Started
  2. Unzip the Example code
  3. You should see a “NotePadTest” folder. Sub-folders include bin,src,Robotium-Solo2.4
  4. bin is where the classes and apk (Android package) are found. src is source code for the test. Robotium-Solo2.4 is where the Robotium jars are found. res is the resources folder for the NotePad app eg. images, layouts etc
  5. Follow the instructions on the Getting_Started Wiki to install the project on Eclipse.
  6. You may see some errors like “unable to resolve target android-9” etc. To solve this, you need to install the right right click on NotePadTest->Properties->Android. Select a Build Target eg. Android 2.2.
  7. Also make sure you have installed Sample code as we’ll need the NotePad sample app
  8. Next, add the NotePad project into the NotePadTest project by: Right click on NotePadTest->Properties->Java Build Path->Projects->Add and select NotePad project
  9. Now, run the JUnit test by doing the following: Right click on NotePadTest->Run As->Android JUnit Test
You should see the Android emulator pop up and (after some time) the NotePad app “going through the motions” of the tests.

Deconstructing the Test Code
Looking at the NotePadTest class (to view that, navigate the “src” folder under the NotePadTest project) you’ll see:

public class NotePadTest extends ActivityInstrumentationTestCase2<NotesList>{

private Solo solo;

The com.jayway.android.robotium.solo.Solo class is the key class here as you’ll see later.

Next we see the constructor method where we add the app’s Activity class so that the test class knows what class of the app to test.


public NotePadTest() {
super("com.example.android.notepad", NotesList.class);
}

Set up and tear down code (almost the same for all tests):


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


public void tearDown() throws Exception {
try {
//Robotium will finish all the activities that have been opened
solo.finalize();
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}

Now comes the meat of the code: testAddNote, testEditNote, testRemoveNote methods. Deconstructing again:

solo.clickOnMenuItem("Add note");
this is to click on a MenuItem with label “Add note”

solo.enterText(0, "Note 1")
Enter text on first (ie. index 0) input box

solo.goBack();
Go back

solo.goBackToActivity("NotesList");
Go back to the main Activity class “NotesList”

solo.searchText("Note 1")
Search for text “Note 1” and return boolean true/false

solo.setActivityOrientation(Solo.LANDSCAPE)
Change orientation to landscape

The AndroidManifest.xml

The manifest part of the xml will need to have package="com.jayway.test" or the package of your test code.

Next the instrumentation part will need to have android:targetPackage="com.example.android.notepad" or the package of the app to be tested.

Additional Information
The Robotium team has also provided a number of tutorials – including how to set up Eclipse and write Robotium test code. They can be found here: http://code.google.com/p/robotium/wiki/RobotiumTutorials
Advertisements

Leave a comment

Filed under Android, Native App, QA, Testing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s