Automated UI testing for Android applications with Robotium

Automatic UI testing helps to keep the quality of your software high and helps you notice if your code changes affect the implementation of the actual use cases. When you are creating Android applications you would like to be able to test them without the help of many human testers. Human testing of UI of the same application on multiple devices is a waste of time, so you want to eliminate it through automation, like you would do it with Selenium for web application. For Android applications there is a similar tool for testing called Robotium. It is a UI testing tool which simulates touching, clicks, typing, and other users’ actions relevant for Android applications. Writing tests using Robotium is very easy as you can see from below.

Bitbar, the company I work for, has created a Continuous Integration environment with automated UI testing for Android applications. Here are some benefits of using it:

  • Automated execution of UI tests (created in Robotium) on each commit.
  • Testing against many configurations, real devices and targets that emulate real devices
  • Visual presentation of test results in human friendly form
  • Screen shots from the UI testing process, so you can see how your application looks on real devices
  • As an example of using Robotium I show you how to create tests for one of my Android applications used for monitoring of Hudson’s jobs.

    Create Android Test Project:

    If you are Eclipse user (and your Eclipse has ADT plugin) you should open project which you want to test  and then chose menu File → New → (Other → Android) → Android Test Project. Create test project for existing Android project. To run it select Run As –> Run As Android JUnit Test.
    Other non-Eclipse) developers should use android tool from Android SDK. (In this example I use it in test project directory) :

    android create test-project -p . -m ../HudsonMonitor
    -n HudsonMonitorTest

    - after –p put the path to your test project directory
    - after -m put the path to project which you will be testing
    - after –n put the name of test project
    To build it you should use ant like ant debug or ant release (for release) but remember to add robotium.jar file to this project’s libs directory (if you don’t do this you will have NoClassDefFoundError for com.jayway.android.robotium.solo.Solo class.
    To install the application and the tests on emulator or device use:

    adb -s emulator-5554 install -r bin\HudsonMonitor-debug.apk
    adb -s emulator-5554 install -r bin\hudson_monitor_test-debug.apk

    To run the tests:

    adb shell am instrument
    -w com.android.hudson.tests/android.test.InstrumentationTestRunner

    but remember to unlock emulator before you run the tests.

    Creating Test Cases:

    Ok, so now we should create class which will be implementing a test case. The only thing that we should know about the tested project is the name of the package and the name of the main class. Test case class in our test project looks like this:

    import com.android.hudson.HudsonMonitor;
    import com.jayway.android.robotium.solo.Solo;
    import android.test.ActivityInstrumentationTestCase2;
    import android.test.suitebuilder.annotation.Smoke;

    public class HudsonMonitorTest extends ActivityInstrumentationTestCase2{

    private Solo solo; // this is Robotium class used to support test cases that span over multiple activities.

    public HudsonMonitorTest() {
    super("com.android.hudson", HudsonMonitor.class);
    }

    public void setUp() throws Exception {
    super.setUp();
    solo = new Solo(getInstrumentation(), getActivity()); //takes in the instrumentation and the start activity.
    }

    @Override
    public void tearDown() throws Exception {
    try {
    solo.finalize();
    } catch (Throwable e) {
    e.printStackTrace();
    }
    getActivity().finish();
    super.tearDown();
    }

    }

    Our application has four Tabs: “Statistic”, “Details”, “Webpage” and “Configuration”. One of ways to choose one of them is to click an element with text, for example choose “Configuration”:

    solo.clickOnText("Configuration");

    The first test checks “adding new Hudson” use case. On configuration screen we have two EditTexts and a ListView of monitored Hudson’s jobs. In first EditText the test should typing feed URL (ex. hudson:8080) for Hudson and in the second its name (e.g. hudson) which will be shown in our app to identify this Hudson instance. In Robotium widgets on the screen are numerated from top to bottom and from left to right and start form 0, so our code for this action is:

    // 0 because it is the first EditText on screen
    solo.enterText(0, "hudson:8080");
    // 1 because it is the second
    solo.enterText(1, "hudson");

    To add this Hudson to HudsonMonitor application we should click on ImageButton with plus sign . It is first ImageButton from top and left so it has number 0:

    // for ImageButton use only clickOnImageButton method,
    // but for simple button use clickOnButton
    solo.clickOnImageButton(0);

    Then we must check whether the item with this Hudson feed URL has been added to the list:

    boolean expected = true;
    boolean actual = solo.searchText("(http://hudson:8080)");
    assertEquals("Can't add new Hudson Instance", expected, actual);

    The entire source code of this test is as follows:

    @Smoke
    public void testAddNewHudson() throws Exception {
    solo.clickOnText("Configuration");
    solo.enterText(0, "hudson:8080");
    solo.enterText(1, "hudson");
    solo.clickOnImageButton(0);
    boolean expected = true;
    boolean actual = solo.searchText("(http://hudson:8080)");
    assertEquals("Can't add new Hudson Instance", expected, actual);
    }

    Second test should check details of activity which has a Spinner and a table with details about builds. To choose an item from Spinner we can use the following method:

    solo.pressSpinnerItem(0, 1);

    This method will only select one item from the Spinner, When we need check something on Spinner item’s list we should use the following code:

    solo.clickOnView(solo.getCurrentActivity().
    findViewById(R.id.SpinnerDetailList));
    solo.clickInList(2);

    To use this code we must import R from tested Project (HudsonMonitor). Using method …OnView we can test every widget on the screen, but we must have more information about project which we are testing.

    Summary

    Robotium is very easy to use and can help to keep very high quality of your software throughout the project lifecycle. Bitbar has created an automated build and test environment for Android application developers who can build and test their applications on many configurations, real devices and targets that emulate real world devices.
    In our Continuous Integration service for Android applications the developer can automate his testing process for multiple emulators of the real android devices.
    You can specify your Android targets and all parameters for emulators which will be used for testing your application.

    For every developer's code submission (commit or push) our system builds it and tests the application automatically. You can also see the test results in a human friendly form, including screenshots from testing process for each emulator.

    If you have problems or questions about Robotium please put it in the blogpost comments and I will try to help you.
    If you are interested in our Automated build and test System for Android applications, please contact us at info(at)bitbar.com.

Archive

Subscribe to our newsletter

Wrong e-mail format.
E-mail address already subscribed.
Thanks for subscribing. Please check your email to verify. Don't forgot to check your spam folder.
Send