How to record your Android tests with Robotium Recorder

A couple of weeks ago, I wrote a post about the release of Robotium Recorder.
Today I want to show you, how to install and how to use the tool to record your Android tests. This tutorial based on the example Notepad app provided by the Robotium project, the source code can be found here.

Prerequisites & Installation

Before you start with recording, you have to install the Java JDK and the Android SDK. Be sure you have the latest version of the Android SDK installed or updated. If your development environment is up to date, you can import the sample app, provided by the Robotium project. Please follow the instructions in the sample, on how to insert the existing project to eclipse.

If Java, the Android SDK and the sample project is downloaded and installed, start Eclipse and open the Install New Software section in the Help menu (Help → Install New Software). In the input field “Work with” enter: http://recorder.robotium.com/updates and Press the Add… button and enter a name for the installation.

1_install_robotium_via_help

Eclipse is loading the information from the added Robotium Recorder URL. If the URL was added correctly, the Robotium Recorder package is listed. Select Robotium Recorder and press Next

2_robotium_installation

After the package has been checked, click Next and accept the licenses. Click the Finish button and the installation is starting (Hint: A security warning will show up during the installation, just accept the warning, that’s OK). If the installation was successful you need to restart Eclipse.

Start Recording

Now you have two options to start recording your tests with the recorder. The first one is to do a right click on an existing project → Robotium Recorder → New Robotium Test.

5_start_robotium_recorder_from_project

If you click New Robotium Test, the Robotium Recorder Wizard with prefilled project settings will be opened. In this case, the NotePad.apk (highlighted in yellow) and the test project name are already provided in the wizard.

5_1_start_robotium_recorder_from_project

If you don’t have a workspace project (source code), you can create a new Robotium Recorder Test via the menu: File → New → Other → Android – Robotium Recorder → New Robotium Test.

3_start_robotium_recorder

After clicking the Next button, the wizard will show up. If apk files are available within the Eclipse workspace, these files will be shown in a list. If no apk files are shown, you can select the binary file from your hard drive with the select apk button. Robotium Recorder is signing the APK file with the local debug certificate automatically after you added the file to the wizard.

4_start_recording

If you click Next, the “Start the recording” window is shown.

7_New_Recording

This window has several buttons. There is the button New Robotium Test (enabled), a Save button to save a session (disabled), a Take Screenshot button to take a screenshot (disabled) and the Delete step button to delete a recorded step (disabled). All disabled become active either during the recording session or at the end of it.

6_1_recorder_Settings

In lower left corner there is a Settings button providing the following three options.

  1. Use sleeps – This option can be selected to use sleeps in playback mode. The test case will be executed in the same time, the app was recorded. This is a nice feature especially for apps that consume a lot bandwidth.
  2. Keep app data – The app data will be kept when starting a new test record.
  3. Identify class over string – With this option the default view identifier, the resource ID is used instead the string identifier (the text that is displayed by the view).

To start a new recording, click the button New Robotium Test. Robotium Recorder will now install and execute the APK file to the connected device or emulator. In the background, eclipse is changing to the JUnit view. You can see that a NotesListExecutor is running to record the interaction.

7_1_startRecorder

Every touch, scroll, rotation and typing will be recorded and can be seen in the Robotium Recorder Wizard.

7_2_recording

While the recorder is running, you have the chance to insert a take screenshot step or to delete a step you just made. To enable both buttons you need to click on of the steps you just recorded.
If you want to stop the recording just click the Stop Recording button. After stopping the recording the Save button will be enabled. At this stage you have still have the chance to delete a step or to add a screenshot step into the recordings.

7_2_1edit_recording

When pressing the Save button, a dialog for entering a name is showing up. The entered name will be the java class name of the recorded session.
If the session is saved, you should find a new project “NotePadTest” in your Eclipse workspace. This project has the typical Android project structure. For the previous added name, you can find a java class that includes the steps you made during the recording session. 
If you already familiar with Robotium, you will basically find the same structure in this one class.

7_4_recording_result

If you need more information about Robotium, please read the following posts ‘How to setup a test environment for Android using Robotium‘ and ‘Robotium, Jenkins and ant‘.

Recorded Code

The recorded java class extends from the Robotium ActivityInstrumentationTestCase2<NotesList> class. It has the known Robotium solo object to interact with the app. The setUp() and tearDown() methods are also the same as in a normal Robotium project. Every recorded class has it’s own setUp() and tearDown() method. The recorded steps are located in the testRun() method. See the following recorded testRun method.

public void testRun() {

   // Wait for activity: ‘com.example.android.notepad.NotesList’
   solo.waitForActivity(com.example.android.notepad.NotesList.class,          2000);

   // Click on action bar item
   solo.clickOnActionBarItem(0x7f06000b);

   // Wait for activity: ‘com.example.android.notepad.NoteEditor’
   assertTrue(“com.example.android.notepad.NoteEditor is not found!”,          solo.waitForActivity(com.example.android.notepad.NoteEditor.class));

   // Enter the text: ‘Test robotium recorder’
   solo.clearEditText((android.widget.EditText)
     solo.getView(com.example.android.notepad.R.id.note));

   solo.enterText((android.widget.EditText)
     solo.getView(com.example.android.notepad.R.id.note), “Test
robotium recorder”
);

   // Hide the soft keyboard
   solo.hideSoftKeyboard();

   // Enter the text: ‘Test robotium recorder ‘
   solo.clearEditText((android.widget.EditText)
     solo.getView(com.example.android.notepad.R.id.note));

   solo.enterText((android.widget.EditText)
     solo.getView(com.example.android.notepad.R.id.note), “Test robotium
       recorder”);
}

Every step that is made by Robotium Recorder has a comment, explaining the step that was recorded. This can be useful especially for beginners. If you are already familiar with Robotium the testRun() method is very easy to understand. You can extend the testRun() method manually with more Robotium test steps. 

ClickToAssertTM

Click to Assert is a feature that allows you, to verify that e.g. a view, label, image or other UI elements are available on the current screen. The feature can be used during the recording session by just touching these elements. This is a really useful feature to quickly assert that elements are present.
You maybe noticed that Robotium Recorder is asserting the change of an Activity automatically. This is done with:

// Wait for activity: ‘com.example.android.notepad.NoteEditor’
assertTrue(“com.example.android.notepad.NoteEditor is not found!”,
   solo.waitForActivity(com.example.android.notepad.NoteEditor.class));

If you want to use the click to assert feature you simple touch the element you want to verify. For example, the current view can be asserted by touching the top of the view.

// Assert that: ‘Create note’ is shown
assertTrue(“‘Create note’ is not shown!”,
   solo.waitForView(solo.getView(android.R.id.title)));

Execute the Recordings

After you recorded the testcases, you can playback/ re-run them by doing a right click on the test project or test class and choose Run As → Android JUnit Test. 

7_5_execute_tests_again

Eclipse will change in the JUnit Mode and the tests will be executed either on the device or emulator. The test results will be shown in the known JUnit stile.

7_5_1_junit_report

Update Recordings

Once you recorded and saved a test, you can’t use the Robotium Recorder to update the session again. If you want to change something, you need to do this manually. Maybe in on of the next releases this feature will be available. 

How to enter a license key

If you use the trial version of Robotium Recorder you will see the following window after 10 recording sessions. 

8_trial_period

If you want to use the recorder in more detail, there is currently a limited time offer of $295 per one year license. See the Robotium Recorder website for that.

If you purchased a license you need to activate your installation. To insert the license key create new Robotium Recorder project in Eclipse: File → New → Other → Android – Robotium Recorder Test. If you used the 10 recording sessions already, the wizard will offer you a window to enter your key. Just enter the license key and press the Next Button. Now you can use the recorder with no limits.

8_1_enter_license_key

Conclusion

The installation of Robotium Recorder is very easy and simple. The same applies for recording the tests. The tool has one easy Wizard, where everything can be configured. The recorded test code is also very easy to understand with a clear structure. The tool is great for fast creation of test cases and basic test suites. To get a first impression the 10 recordings are enough, but if you want to do more stuff with the recorder you need a license.
I really like the click to assert feature. It is really easy and simple to use. Besides that, it saves you a lot of time, asserting elements in your app.

While using the recorder one thing came to my mind. Keep the recording sessions short and simple! I was recording quite some interaction and at the end the testRun() method was huge and hard to maintain. As always, it is recommended to think about the test scenarios before you record them :). You should also keep in mind, that you still need knowledge about Robotium and its features to extend the recordings with more assertions and features.

I am really looking forward to more features in the recorder, maybe there will be a feature to update your recordings with the wizard.

What are your impressions with the Recorder?

Have fun and Greets.

By Daniel Knott