Mobile Page Objects with Robotium-Sandwich

In several blog posts I wrote about the Android test automation framework Robotium. Those articles described the setup of the tool and how to split the whole test suite into smaller parts. Splitting the test suite into smaller parts is sometimes neccessary because building huge test suites can be a real challenge. If you don’t think about the architecture and about an abstraction layer of your test suite, the code will be impossible to mantain, impossible to scale and at the end, you will end up in a nightmare.

The guys from appThwack developed a library called ‘Robotium-Sandwich’ which is built on top of the Android instrumentation and the Robotium framework to solve the problem of unmaintainable test suites. The idea behind Robotium-Sandwich is similar to the page objects from Selenium 2.

Robotium-Sandwich has two parts. The first part contains the Android screen definitions. For each screen a seperate class must be created. This class describes the structure of the screen with so called widgets. With this approach you handle the screens as objects and the content (widgets) as object properties.

To create such a screen class, derive your own class from AScreen, pass the target activity class to the default constructor and start using the annotations (widgets) to define the screen. The following code listing from the Robotium-Sandwich page shows an example screen class.

public class StuffScreen extends AScreen {

    public StuffScreen() {

    public ARadioButton radio1;

    public ARadioButton radio2;

    public AEditText sample_edittext;

    @AText("Do more stuff")
    public AButton do_more_stuff;

    public ASpinner my_spinner;

Code-Listing from:

The UI elements are represented by the widgets. Those widgets must be a type from AView. The UI elements must be annotated with an @A* identifier to be a widget. In the listing above you can see, how to annotate UI elements e.g. buttons, radio buttons, edit text fields or spinners. The elements are either described by their ID, text or index. If elements are not annotated with an identifier, Robotium-Sandwich will associate the field with a default identifier e.g. @AIndex(0).

If the screen class is implemented, you need a test class to describe the automated test. The next code listing is showing the test implementation of the above screen class.

public void testStuff() {

        StuffScreen stuffScreen = new StuffScreen();
        stuffScreen.sample_edittext.enterText("Some stuff");

Code-Listing from:

In the test class, within a test method you create an object from the screen class, in this case the object is from StuffScreen. With the object you have access to all screen elements you defined before. Robotium-Sandwich will find the associated widget and will map it to the responsible Android UI element. Besides that, it will perform the action you defined in the test method e.g. click() or selectItemAt().

The following screenshot is showing the simple Android app that was described in the screen class and is tested with the sample test method.stuffscreen

Screenshot source:

Widgets, Identifiers and Webview support

Robotium-Sandwich has a number of predefined widget types. Those widget types are mapped to the responsible Android view classes. If you want to define your own widgets, derive your own widget from AView and implement the needed functions using the raw Android instrumentations or Robotium. Besides the widgets, Robotium-Sandwich provide several identifiers to associate with the widgets and to map to the responsible Android view. The following identifiers are provided by Robotium-Sandwich:

  • @AIndex: A search is triggered, if there are more matches on the screen to find the element by its ID. Can be used together with @AId, @AIdName or @AClass.
  • @AId: Is used to find an element represented by its ID.
  • @AIdName: Use this to find an element by its view ID name (useful if only the binary APK file is available for testing).
  • @AText: Find the element by its text.
  • @AClass: Is mapping a target view type. Can be used with other identifiers.

Like Robotium, Robotium-Sandwich also supports the test automation of web views within Android apps. To automate those web views use the widget AWebElement and one of the identifiers @AName (Find the element by the name) or @CssSelector (Find the element with a CSS selector).

If you want to try Robotium-Sandwich, clone the repository from and have a look at the sample app and the implemented tests or include the jar file to your project.

I really like the idea behind Robotium-Sandwich, to split the implemented tests into two parts to get a better structure of the implemented tests.

What are you thinking about Robotium-Sandwich? Have you tried it or do you use an own approach, that is similar to Robotium-Sandwich?

Happy Testing!

By Daniel Knott