December 24, 2012
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”);
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.
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:
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.
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.
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