Thursday, December 27, 2012

Simple Text Search using Robotium

Robotium supports Various APIs to test various Android UI functionalities. refer the documentation for the complete list of APIs.
I will explain the usage of the Robotium APIs in real time using a sample android application.

See the sample application shown on the right side, which contains few basic elements like "Text Field, Radio buttons, Toggle button, Text Field/Edit box, Progress bar, Seek bar, Rating bar and an Image Button".

to get familiar with the APIs, let us write test cases for this application.

 

Test case for Simple text search:

Steps :

  1. wait for the activity
  2. Search for the Text
  3. Assert True/False based on the pattern match

Test code:

   1: public void testSimpleTextSearch() throws Exception {

   2:         solo.waitForActivity("MainActivity");

   3:         boolean actual = solo.searchText("Select a Button");

   4:         boolean expected = true;

   5:         assertEquals("Required Text not found",expected, actual);

   6: }

Explanation :

  1. Wait for the Activity : this an important step to add always in the code, before writing test steps. 

    • Every time before running the test case, Robotium will freshly opens the Activity and starts executing test steps.
    • If activity takes more time to launch, Robotium may malfunction thinking that the activity is opened
    • for the best practise, it is always a good idea to wait for the activity before Executing the test steps
    •  solo.waitforActivity( ), wait for the Activity to open

  2. Search for the Text :  Robotium supports different APIs to search for a Text pattern, single match and Multiple match etc ..

    • I will provide more details on using other APIs in coming sessions, Now we will look at simple search pattern
    • solo.searchText( ), will look for the String in the whole screen. It may be a Text View, a Toast, a List item or an Button Text etc..

  3. Assert True/Falseassertion can be done using “assertEquals( )” statement, which will compare the actual and expected values. 

    • in the above code, first argument is the message to display when assertion fails
    • second argument is the expected value
    • third argument is the actual value

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.