Thursday, September 28, 2017

Use Robotium to test the Android Spinners

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);

download file now