Home » How to setup a test environment for Android using Robotium

How to setup a test environment for Android using Robotium

This post should show how easy it is, to setup an robotium test environment for Android Apps using the Eclipse IDE. First of all you have to download the latest Android SDK depending to your operating system from http://developer.android.com/sdk/index.html. After downloading the zip file you can unzip it to:

  • /opt for Linux system
  • /Applications for Mac OSx systems
  • c:Programs for Windows systems

AVD – Manager
Start the Android avd manager with the command ./tools/android. If the avd manager window has started open the section “Available Packages” and install the Android sdk versions you need for development. The download and installation process can take a while.

AvD Manager

 

 

 

If the installation of the SDKs is ready, you can create an Android Simulator using the avd manager window. Open the section “Virtual Devices”. Click the new button on the right side, choose an emulator name. Then select the Android SDK Version you want to use for the emulator e.g. Android 2.2 – API Level 8 from the target drop down box .

Select the checkbox snapshot enabled and create the emulator. The created emulator should be visible in the list.

Create a new virtual Android emulator

 

 

 

 

 

 

 

 

 

Select the emulator and start it.

Android virtual Devices

 

Install ADT and configure Eclipse
Download and install Eclipse from http://www.eclipse.org/downloads/. After the installation of eclipse you have to install the Android Development Tools (ADT) Plugin. Do the following steps to install the plugin:

  1. Open the install new software section in eclipse Help -> Install New Software
  2. Click Add, in the top right corner
  3. In the Add Repository Dialog that appears enter a name for the plugin e.g. ADT and add the following URL https://dl-ssl.google.com/android/eclipse/ Note: If you have trouble with this URL, try using http in the location URL instead of https
  4. Press the OK button
  5. In the Available Software dialog, select the checkbox next to “Developer Toos” and click next
  6. In the next window click next
  7. Read and accept the license agreements and click finish
  8. After the installations is completed restart eclipse

If the installation of the ADT plugin was successful you find an ADT section in your eclipse views.

ADT in Eclipse

The last thing you have to do is, link the previous installed plugin to your Android installation on your computer. To do this open Window -> Preferences select Android from the left panel and enter the SDK location using the browse button. Select the location and click Apply.

Android Debugging Bridge
To install an Android app to an device the Android debug bridge (adb) daemon must be up and running. Create a start script to the autostart section of your computer (the following script based on a linux installation):

#!/bin/bash
/opt/android/platform-tools/./adb kill-server
sudo /opt/android/platform-tools/./adb start-server

Android App code
The groundwork is done for testing an Android app. The next thing you have to do is, load the app code from a github or svn repository to eclipse using the import functionality. If this is done you can create a new Android Test Project with New –> Android –> Android Test Project. In the next window enter a Test Project name. In the section test target choose the previous imported Android app. Selecht the Android Build Target. Click finish to create the Android Test project.

Create a new Android Test Project

 

 

 

 

 

 

 

 

 

The next step is to add the robotium.jar file to the build path. Open the project properties, right click on the project –> Properties. In the section Java Build Path click the Add Jars… Button and import the latest version of robotium (Download latest version from http://code.google.com/p/robotium/downloads/list). Click OK and you are ready to write test classes with robotium.

Add the Robotium.jar file to the Android Test Build Path

 

 

 

 

 

 

 

AndroidManifest and Example Test Class
The first thing you should do is to change the Android target package in your AndroidManifest.xml to the Android app you want to test e.g:

<instrumentation android:targetPackage="YOURAPP" android:name="android.test.InstrumentationTestRunner" />

To write your first robotium test class, create a package in your Android Test Project. Within this package create a new java class that extends the class ActivityInstrumentationTestCase2 from robotium to use all the methods robotium provides. The following code listing shows a really simple test class with setup and teardown Method and one testmethod.

import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
public class ExampleTest extends ActivityInstrumentationTestCase2 {

   private static final String TARGET_PACKAGE_ID = "YOURAPPPACKAGE";
   private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = 
      "YOURAPPPACKAGE.activities.MainActivity";

private static Class launcherActivityClass;
private Solo solo;

static {
   try {
      launcherActivityClass =
         Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
   } catch (ClassNotFoundException e) {
         throw new RuntimeException(e);
   }
}

public ExampleTest() throws ClassNotFoundException {
   super(TARGET_PACKAGE_ID, launcherActivityClass);
}

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

@Override
public void tearDown() throws Exception {
   try {
      solo.finalize(); //Robotium 2.x
      solo.finishOpenedActivities(); //Robotium3.0
   } catch (Throwable e) {
      e.printStackTrace();
   }
   getActivity().finish();
   super.tearDown();
}

// first testmethod
public void TestSomething() throws Exception{
   solo.searchText("TextHeadline");
   solo.enterText(0, "MyName");
   assertTrue("ERROR: Text not found", solo.searchText("Some Text"));
}
}

To start the test do a right click on the test class choose Run As –> Android JUnit Test. If a device is plugged to the computer the app apk and the test apk file will be copied to the device. The test will automatically be executed on the device. If no device is connected to the computer robotium will automatically start the Android simulator if one was created before. NOTE: USB Debugging must be enabled on the device to execute the tests.
The testrun results will be visible in the JUnit section in Eclipse.

That’s it. You should now be able to setup your own Android robotium setup to test Android apps. Keep on testing!