Monday, December 31, 2012

Use Robotium to test the Android Spinners

Spinners in Android, provide a quick way to select one value from the set. in most cases it looks like a drop down menu with several items in the list.  Robotium provides the following APIs to test the Spinners functionality,
  • getCurrentSpinners()
  • isSpinnerTextSelected(int index, String text)
  • isSpinnerTextSelected(String text)
  • pressSpinnerItem(int spinnerIndex, int itemIndex)
to know how to use these functions, let us write a sample test case and test code for the Project shown below, which has a Spinner to select month, contains list of months from January to December.

First image is the one before selecting the Spinner and the second is after selecting the Spinner. 

Test case for Spinners :

Steps :
  1. Select the Spinner item
  2. From the List select ‘January’
  3. Verify the status of selected spinner item
  4. Select the spinner item ‘July’ and ‘December’ and verify their status.
Test Code :
   1: public void testSpinner() throws Exception {

   2:     solo.waitForActivity("MainActivity");

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

   4:     assertEquals("Select a Button Text not found",true, actual);

   5:         

   6:     //click on Image Button

   7:     solo.clickOnImageButton(0);

   8:     assertTrue(solo.waitForText("Image Button is selected"));

   9:                     

  10:     actual = solo.searchText("Select Month");

  11:     assertEquals("Spinner text not found",true, actual);

  12:             

  13:     //select spinner item January

  14:     solo.pressSpinnerItem(0, 1);

  15:     actual = solo.isSpinnerTextSelected(0, "January");

  16:     assertEquals("spinner item January is not selected",true, actual);

  17:             

  18:     //select spinner item March

  19:     solo.pressSpinnerItem(0, 2);

  20:     actual = solo.isSpinnerTextSelected(0, "March");

  21:     assertEquals("spinner item March is not selected",true, actual);

  22:             

  23:     //select spinner item May

  24:     solo.pressSpinnerItem(0, 2);

  25:     actual = solo.isSpinnerTextSelected(0, "May");

  26:     assertEquals("spinner item May is not selected",true, actual);

  27:             

  28:     //select spinner item July

  29:     solo.pressSpinnerItem(0, 2);

  30:     actual = solo.isSpinnerTextSelected(0, "July");

  31:     assertEquals("spinner item July is not selected",true, actual);

  32:             

  33:     //select spinner item Octomber

  34:     solo.pressSpinnerItem(0, 3);

  35:     actual = solo.isSpinnerTextSelected(0, "Octomber");

  36:     assertEquals("spinner item October is not selected",true, actual);

  37:             

  38:     //select spinner item September

  39:     solo.pressSpinnerItem(0, -1);

  40:     actual = solo.isSpinnerTextSelected(0, "September");

  41:     assertEquals("spinner item September is not selected",true, actual);

  42:             

  43:     //select spinner item November

  44:     solo.pressSpinnerItem(0, 2);

  45:     actual = solo.isSpinnerTextSelected(0, "November");

  46:     assertEquals("spinner item November is not selected",true, actual);

  47:             

  48:     //select spinner item December

  49:     solo.pressSpinnerItem(0, 1);

  50:     actual = solo.isSpinnerTextSelected(0, "December");

  51:     assertEquals("spinner item December is not selected",true, actual);

  52: }

Explanation:
  1. Select Spinner Item : spinner item can be selected by using the function ‘solo.pressSpinnerItem()’. Spinner contains months from January to December.

    • to select Spinner item “January”, index starts with 0

    • if we want to select Spinner item “March”, it’s index will not be 3. Please note that, the index calculation in Spinners will be different from the other items.

    • Spinner item index is calculated based on Current Spinner item selected.  In this case, since our current item selected is ‘January’,  to select the item ‘March’, we need to move our position to two steps forward, this can be achieved by using ‘solo.pressSpinnerItem(0,2)’. in this statement first argument is for the Spinner, second argument is the reference to spinner item i.e. ‘March’

    • at this step, our present selected item is ‘March’. Suppose, if we want to select ‘February’, we have to move a step backward by index of ‘-1’. this can be achieved by using ‘solo.pressSpinnerItem(0, -1)

    • Now you are at “February”, if you want to go to “May”, your statement will be ‘solo.pressSpinnerItem(0, 3)

  2. Verify the Spinner item selected : this can be achieved by using the statement ‘solo.isSpinnerTextSelected()’, which returns a boolean value either true or false.

1 comment:

  1. This comment has been removed by the author.

    ReplyDelete

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