Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Android automated black box testing with Robotium

Cris Kim

Requirements

  • Android SDK (version r11)
  • Robotium (version 3.0)
  • Eclipse Helios

Introduction

Automated tests are key in maintaining high quality software. They help catch bugs immediately when introducing new code and also ensure that new features do not conflict with existing functionality. Another advantage of automated tests, specially for Android applications is that it greatly speeds up testing when you have multiple builds for different devices. In this blog post I’ll go over how we use robotium in our TASS mobile project to implement automated black box tests.

Robotium is a plugin that extends existing instrumentation tests available on Android and addresses its shortcomings. Limitations such as slow execution times and having to manually delay the application to wait for the desired screen to be ready. For example, delays caused by waiting for a network request to complete before proceeding to the next screen. Robotium works by sending screen commands such touch, swipe and keyboard input to the Android emulator running an application and verifying the resulting screen.

Create Android Test Project

We’ll first create a new Android Test project; make sure that the Android project that will be tested is currently in the workspace.

  1. File → New → Other. Select Android/Android Test Project. Click Next.
  2. Fill out next screen. Make sure that under Test Target you select the project that will be tested.
  3. Download robotium 3.0 jar and place it in your project under a lib/ folder. Create the folder if necessary.
  4. Add the robotium jar file to the build path. Right click on the project → Build Path → Configure Build Path
  5. Click Add JARs.
  6. Select robotium-solo-3.0.jar. Click OK.

Write Unit Test

  1. Create a new unit test. Right click on the test project → New → JUnit Test Case
  2. Name the unit test. I called mine, MainTest. Click OK.
  3. Insert the following snippet to TestMain.java. This snippet opens the login screen (Home.class) on our TASS mobile application and enters the username and password located at the first textbox and second textbox. It will then click the login button. It verifies the success of the authentication by looking for the username text in the authenticated screen.
package com.tallan.tass.android.test;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.Smoke;

import com.jayway.android.robotium.solo.Solo;
import com.tallan.tass.android.Home;

public class TestMain extends ActivityInstrumentationTestCase2<Home> {

	private Solo solo;

	public TestMain() {
		super("com.tallan.tass.android", Home.class);
	}

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

	@Smoke
	public void testLogin() throws Exception {
		solo.clearEditText(0);
		solo.clearEditText(1);
		solo.enterText(0, "ckim");
		solo.enterText(1, "p4ssword");
		solo.clickOnImageButton(0);

		boolean expected = true;
		boolean actual = solo.searchText("ckim");
		assertEquals("Logged in", expected, actual);
	}

	@Override
	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();
	}

}

Run Unit Test

  1. First launch the test target application. Right click, Debug As → Android Application. This will launch the application in the emulator.
  2. Run the unit test. Right click the test project. Run As → Android JUnit Test.
  3. We can see the result of the test under the JUnit tab and the test passed.

    Conclusion

    Robotium makes writing unit tests a breeze and is a solid option for your Android project. Another added benefit of Robotium is that it can integrated with Maven to have a fully automated build and test process. The sample unit test can be downloaded at the link below.

Resources

TestMain.java

1 Comment. Leave new

Hi..i am new to robotium……is core Java enough for writing test cases

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\