Mobile Testing: Automated Testing for Android with Robotium

Robotium is a test framework created to make it easy to write powerful and robust automatic black-box test cases for Android applications so test developers don’t need any further information about the Android app’s structure or implemented classes. All they need is the name of the main class and the path that links to it. With the support of Robotium, test case developers can write function, system and acceptance test scenarios, spanning multiple Android activities. This blog post is meant to serve as a mini-tutorial on setting up Robotium as an automated acceptance testing framework for Android applications. The post will summarize all the info related to setting up & writing tests in Robotium.

Robotium officially supports Android 1.6 and up. Robotium has full support for Activities, Dialogs, Toasts, Menus and Context Menus.

Robotium provides the following benefits:

  • You can develop powerful test cases, with minimal knowledge of the application under test.
  • The framework handles multiple Android activities automatically.
  • Minimal time needed to write solid test cases.
  • Readability of test cases is greatly improved, compared to standard instrumentation tests.
  • Test cases are more robust due to the run-time binding to GUI components.
  • Blazing fast test case execution.
  • Integrates smoothly with Maven or Ant to run tests as part of continuous integration
  • With Robotium it is possible to run test cases on applications that are pre-installed http://code.google.com/p/robotium/wiki/RobotiumForPreInstalledApps
  • Robotium can be integrated into continuous integration and can get code coverage for Robotium tests.(http://code.google.com/p/robotium/wiki/QuestionsAndAnswers)

Introduction to Robotium:

To develop stable and reliable tests, Robotium offers many methods that react to different graphical elements within an Android app, such as.

  • clickOnText(“Secure Login”);
  • clickOnButton(“Save”);
  • searchText(“Logout”);
  • goBack();
  • getButton();
  • isRadioButtonChecked();

With these simple methods, robust automated tests can be implemented really quickly. By combining them with JUnit, you then have additional ways of checking values or user interactions on the device for the correct response, which in turn makes these tests even more powerful.

Creating and running testcases:

Step 1: robotium.jar

First of all test developers have to create an Android test application and by request they need to add the application which you want to test and the version of Android. Then, you have to download and add the  Robotium 2.5 library. The jar is added in Eclipse by clicking on the project >> Properties >> Java build path>> Add (external ) jar.

Step 2: Create Java class

A: To start the first Robotium test class, a Java class that extends the Robotium Framework class ActivityInstrumentationTestCase2 has to be created. This class provides methods and activities to interact with the app. The core of the automated tests is the Robotium object Solo which provides access to the entire Robotium framework along with all of the provided methods. The first step before using the object is to initialize it in the setUp() method. In this method the object is initialized with central Android activity.

public class TestLogin extends ActivityInstrumentationTestCase2 {
      private Solo solo;
      public TestLogin(){
            super("com.Example.ApplicationTesting", DashboardActivity.class);
      }
      @Override
      protected void setUp() throws Exception {
            super.setUp();
            solo = new Solo(getInstrumentation(), getActivity());
     }

B: The really simple example in code aims to demonstrate the functionality of Robotium and the solo object. The example shows a test involving the login process. The testLogin() method is created to test the login process. Within this method, the following lines of Java code were written:

public void test_login(){
     solo.enterText(0,username);
     solo.enterText(1,password);
     solo.clickOnButton("Login");
     assertTrue(solo.searchText("Please wait. Logging in."));
     solo.waitForActivity("com.pointabout.personal.MainTabActivity", 3000);
     solo.assertCurrentActivity("The activity should be Main Tab", "MainTabActivity");
     solo.sendKey(Solo.MENU);
     solo.clickOnText("Logout");
     solo.waitForText("Are you sure you want to log out");
     solo.clickOnButton("Logout");
     solo.waitForText("You have been logged out of Personal.");

The solo object provides access to the enterText method which requires two parameters that allow some text to be entered into a text input field within the app. The first value of the parameters represents the IDs of the input text field on the login screen. The second parameter is the string that should be entered. The clickOnButton() method “clicks” the button to login the user.

The waitForActivity()method waits for the login activity of the app until the user is logged in. After a successful login, Robotium uses the assertCurrentActivity() method to verify if the Main activity is shown . At the end of the test method, a JUnit assertTrue verification is performed to check whether or not the main activity. Also thru solo you can have access to the device buttons (Menu, Back, Home) sendKey().

The id’s of elements the test developer can found from eclipse. Open the application on device at the page  who elements you need to find, and open the hierarchy view from Eclipse, and all information related to id’s are there.

C:  At the end of a test run, the tearDown() method is called to close all activities and to clean up the solo object.

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

Note:

If you do not know the exact package and launcher names, follow these steps in the DOS prompt:

> launch the emulator

> adb install testapplication.apk

>  adb  logcat

The exact package name and  launcher name will be printed.

Step 3: Run Test Case

Eclipse: After all test cases are created right click on the test project Run As >> Run As Android JUnit Test.

ADB:

Use adb to Install the application apk

 >> adb install ApplicationToTest.apk

Use adb to install the test project apk:

 >> Adb install ExampleTesting.apk

Run the test cases:

 >>adb shell am instrument ?w  com.Example.ApplicationTesting/android.test.InstrumentationTestRunner

References: http://code.google.com/p/robotium/

Sabin Pilipautanu

Sabin Pilipautanu

Engineering Manager

Sabin Pilipautanu is an Engineering Manager in 3Pillar’s Cluj, Romania office, and the head of our quality assurance competency center. As Engineering Manager, Sabin is responsible for collaborating with all members of our software development teams, including software architects, developers, and quality assurance testers. He works with our clients to help formulate and plan major software development initiatives into version release plans then oversees development of those development projects in an agile development environment. As head of 3Pillar’s quality assurance competency center, Sabin is responsible for establishing corporate best practices for software testing across a variety of platforms. This includes implementing best practices and conducting training for test strategy, test planning, mobile testing, test case design and reporting, and automated testing. Sabin has a BS in Mathematics from Liceul Teoretic Ion Luca Vatra Dornei in Romania.

Leave a Reply

Related Posts

Shift Your Mindset to Find Product Success 3Pillar CEO David DeWolf spoke on the importance of seeing software development through the lens of product at the recent “Product Matters” event in I...
Flexing Your (Underused) Creative Muscle On this episode of The Innovation Engine, we talk about how to start flexing what is likely an underused muscle, if you're like 98 percent of the popu...
Where Are the Female Leaders in Tech (& How Can We Attr... On this episode of The Innovation Engine, we examine the dearth of female leaders in technology and look at ways we can mentor and encourage more fema...
Microservice & Serverless Architectures – Is Eith... On this episode of Take 3, we explore microservices and serverless architectures - what they are, why microservices have gained so much in popularity ...
5 Cost Saving Strategies When Using AWS Amazon Web Services, or AWS, offers reliable and scalable cloud computing services. More and more companies are realizing the benefits and are migrati...

SUBSCRIBE TODAY


Sign up today to receive our monthly product development tips newsletter.