Robotium for Android

Introduction

Robotium is an open source test framework that automatically writes powerful and robust black-box test cases for android applications, similar in nature to Selenium which is used for web application testing. Test Driven Development is a methodology where tests are written before the actual application.

  • Write a test case
  • Watch it failing
  • Write the actual source code in you application which enables the test to pass
  • Watch the test passing
  • Go back to step 1

With the support of Robotium, test case developers can write scripts for functional, system and acceptance testing and spanning multiple Android activities as well. Robotium is released under Apache License 2.0. Version 2.3 and it was released on April 21, 2011.
Robotium test is a subclass of junit.framework.Test case in which, by using the Robotium library, a Solo object is created which allows easy access to the views in your activities.
Through the solo object, you can set values in input fields, click on buttons and get results from other User Interface components. Methods of Assert class in JUnit can then be used to check those results.

Installation

It’s very simple to install Robotium for android. We just have to download the Robotium-Solo.jar file and add it to the build path of your project. You will understand in the future because I’m going to do a demo by using Robotium.

How to test Sample Android Application with Robotium

1. First of all you have to setup the android development environment. So, follow this document and at the end you will have the working environment installed in your PC.

2. Now we are going to build a simple calculator using android. Click on File menu, select New and click on the Others, From New window, Drag down to Android option, expand it, and select Android Project and Click on Next.

3. From New Android Project Window, enter Project Name as ‘AndroidCalculator’. Then click next

4. Select Android 2.2and click Next

5. Set the package name as ‘com.cse.calculator’ and click Finish.

6. Now you have completed in initializing the android project. Now go to Project Explore in Eclipse > Expand AndroidCalculator > Expand res folder > Expand layout folder. There should be a main.xml file. Open it.

7. Then paste the following code in main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/hello"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#00ff00" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtFirstNumber" />

<EditText
android:id="@+id/etFirstNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="numberDecimal" >
</EditText>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txtSecondNumber" />

<EditText
android:id="@+id/etSecondNumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="numberDecimal" >
</EditText>

<TextView
android:id="@+id/tvAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ff0000"
android:layout_marginBottom="10dp"/>

<Button
android:id="@+id/bMultiply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/multiply"
android:textColor="#0000ff"
android:textSize="20dp" >
</Button>

</LinearLayout>

8. Now place this code in res > values > strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
<string name="hello">Enter two values and click on Calculate to multiply them.</string>
<string name="app_name">AndroidCalculator</string>
<string name="txtFirstNumber">Enter First Number</string>
<string name="txtSecondNumber">Enter Second Number</string>
<string name="multiply">Multiply</string>
</resources>

9. Now open src > com.cse.calculator > AndroidCalculatorActivity.java and paste the following code and save it.

package com.cse.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidCalculatorActivity extends Activity implements OnClickListener{

private EditText firstValue, secondValue;
private Button multiply;
private TextView answer;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// initialize variables
firstValue = (EditText) findViewById(R.id.etFirstNumber);
secondValue = (EditText) findViewById(R.id.etSecondNumber);
multiply = (Button) findViewById(R.id.bMultiply);
answer = (TextView) findViewById(R.id.tvAnswer);

// add listener to the button
multiply.setOnClickListener(this);

}

/** called when multiply button is clicked **/
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.bMultiply:
//Getting first & second values and passing to show result
showResult(firstValue.getText(), secondValue.getText());
break;
}
}

private void showResult(Editable first, Editable second) {
float num1 = Float.parseFloat(first.toString());
float num2 = Float.parseFloat(second.toString());
float result = num1 * num2;
answer.setText(String.valueOf(result));
}
}

10. Now run the android application by right clicking on the Project > Run As > Android Application

11. Let’s begin to create a Test Project for this calculator using Robotium. So go to File > New > Android Test Project.

12. Then, give the project name as ‘AndroidCalculatorTest’ and click next.

13. Then, select the android project (i.e. AndroidCalculator) to be tested and click finish.

14. In test project from project explorer window right click on com.cse.calculator.test select New then others. On New window expand Java and then expand Junit category and select Junit Test Case and click on Next.

15. On New Junit Test Case screen, most of the options will be automatically filled as we have already created test project (AndroidCalculatorTest) with project (AndroidCalculator). We need to enter the Name of Test case, which I will enter TestCal, as I am going to test (AndroidCalculatorActivity.java) of AndroidCalculator project. On next section check Setup(), tearDown() & Constructor options and click on Finish.

16. Now right click on the ‘AndroidCalculatorTest’ project > New > Folder and name the folder as ‘Robotium’

17. Then add the Robotium.jar file which is downloaded before, in the installation section in this tutorial to the Robotium folder. Right click on the jar file in the project explorer > Build Path > Add to build path. OK now you are go to go with Robotium

18. In our create test case we will access the contents of AndroidCalculator and do followings,

  • Call/Access first & second input controls (Edit Fields)
  • Enter values of our own choice
  • Access & Click on Multiply button
  • Put assert to verify their multiplication result into result field. And add following code into TestCal.java class and save it.

And add following code into TestCal.java class and save it.

package com.cse.calculator.test;

import java.util.ArrayList;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.EditText;
import android.widget.TextView;
import com.cse.calculator.AndroidCalculatorActivity;
import com.cse.calculator.R;
import com.jayway.android.robotium.solo.Solo;

public class TestCal extends ActivityInstrumentationTestCase2<AndroidCalculatorActivity> {

private Solo solo;

public TestCal() {
super("com.cse.calculator", AndroidCalculatorActivity.class);
}

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

public void testDisplayBlackBox() {
// Enter 10 in first editfield
solo.enterText(0, "10");
// Enter 20 in first editfield
solo.enterText(1, "20");
// Click on Multiply button
solo.clickOnButton("Multiply");
// Verify that resultant of 10 x 20
assertTrue(solo.searchText("200"));
}

public void testDisplayWhiteBox() {
// Defining our own values to multiply
float firstNumber = 10;
float secondNumber = 20;
float result = firstNumber * secondNumber;

// Access First value (editfiled) and putting firstNumber value in it
EditText FirsteditText = (EditText) solo.getView(R.id.etFirstNumber);
solo.enterText(FirsteditText, String.valueOf(firstNumber));

// Access Second value (editfiled) and putting SecondNumber value in it
EditText SecondeditText = (EditText) solo.getView(R.id.etSecondNumber);
solo.enterText(SecondeditText, String.valueOf(secondNumber));

// Click on Multiply button
solo.clickOnButton("Multiply");
assertTrue(solo.searchText(String.valueOf(result)));
TextView outputField = (TextView) solo.getView(R.id.tvAnswer);

ArrayList<TextView> currentTextViews = solo.getCurrentTextViews(outputField);
assertFalse(currentTextViews.isEmpty());
TextView output = (TextView) currentTextViews.get(0);

// Assert to verify result with visible value
assertEquals(String.valueOf(result), output.getText().toString());
}

@Override
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
}
}

19. Now as we are almost done so now its time to run our test case. Right click on TestCal.java file select Run As option and then click on Android Junit Test. It will start running Junit test. Select the emulator or device to run the test (we will be using Android default emulator) , and wait for a while to see the magic of Robotium.

If things are going fine

  • Emulator will load, Unlock it.
  • AndroidCalculator application will load
  • It will automatically enter first & second values in First and Second Edit Field, and click on
    Multiply button (you can see all this happening as record & play scripts)
  • After successfully execution it will show green bar showing the successful execution and all results are passed.

Pros and Cons for Robotium

Benefits:

  • Multiple application components that provide a screen with which users can interact in order to do certain work is provided in this framework.
  • Test cases are automatically generated when using this framework thus ensuring minimal time for code generation.
  • Compared to standard instrumentation tests readability of test cases is greater.
  • GUI components binding with the run-time help to improve the test cases as it will easier than with a non-GUI model.
  • Apache Ant and Apache maven which are build automation tools can be integrated with Robotium.

What we can’t do with Robotium:

  • Robotium has a big problem with auto scrolling methods for an example if you are looking for the text, which is not shown, Robotium will stack in the end of the scroll view and make assertTrue(false) function to stop scrolling.
  • You will need to implement some logic to click items in the scroll/list view. Because of Robotium clicks in the center of the view, you will always get exception or assertTrue(false) when try to click view with only 20% part shown.
  • Cross Activities testing, Robotium is able to work only with same certificate application, otherwise you will have to inject events exception (eg: you are not able to do clicks on screen keyboard).
  • Robotium has assertTrue(false) logic for reporting problems/unexpected situations instead of returning some Enum value or Boolean (success/fail) so for a good stress tests which run 24/7 you need to add your own methods which will not stop test, just handle ‘method fail to click x y’ result value.
  • Robotium has no mechanism to handle expected/unexpected alerts/pop ups/dialogues. For an example I/Os java script tests has very simple Boolean flag and callback for handling alerts.

Robolectric vs Robotium

Robolelectric is another testing tool which doesn’t need a device/emulator as Robotium.With compared to Robotium there are not only plus points but also the minus points. When talking about its plus points It can be pointed out as a very fast and stable testing tool which dramatically sped up TDD cycles and works really well for most common testing needs.

Robolelectric,

  • Runs in JVM
  • Shadows android SDK classes
  • No device/emulator necessary for run
  • High performance
  • great turnaround during development
  • powerful API

Few minus points,

  • Android API simulation isn’t 100% accurate
  • incomplete shadows
  • limited since not running on device
  • cannot distinguish between API levels

But Robolelectric is under development and is very useful because instrument/Emulator based test such as Robotium are painfully slow though they follow the Android API to the letter(Randomly fail). They are great for testing the edges of the API that aren’t simulated by Robolelectric and for testing actual phone behavior in the situation.

Calculon Vs Robotium

Calculon is a unit testing tool which runs on Dalvik, the process virtual machine (VM) in Google’s Android operating system. It is more focus to unit testing than Robotium.Calculon is a DSL for testing views and activities. As Robotium seems to most mature and reliable it is preferred. A test should be easy to write, easy to read. So calculon comes to the rescue for those. Calculon deployed as a JAR and is a DSL(as far as it is possible in java) as mentioned above. It is open source and tests with calculon are written as functional activity tests.

Advertisements