Thursday, December 27, 2012

Using Robotium to test Android Radio Buttons



Robotium Support Various functions to test the Android Radio buttons functionality. the following are the few APIs supported by Robotium,
  • getCurrentRadioButtons()
  • clickOnRadioButton(int index)
  • isRadioButtonChecked(int index)
  • isRadioButtonChecked(String text)
Let us write a test case  for the example project shown on the Right side.

Test case for Radio Buttons:

Steps :
  1. Check the current status of Radio buttons
  2. Select the Radio button ‘Android’
  3. Select the Radio button ‘iPhone’
  4. Verify the Status of Radio buttons
Test code : 
   1: public void testRadioButton() throws Exception {

   2:     solo.waitForActivity("MainActivity");

   3:       

   4:     //check if Android Button is selected?

   5:       actual = solo.isRadioButtonChecked(0);

   6:  

   7:         if(actual == false){

   8:             Log.d("Incube_FormWidget", "E1: Android Button is not selected");

   9:         }else{

  10:             Log.d("Incube_FormWidget", "E1: Android Button is selected");

  11:         }

  12:         

  13:     assertEquals("Android Button is checked",false, actual);

  14:         

  15:         

  16:         //check if iPhone Button is selected

  17:         actual = solo.isRadioButtonChecked(1);

  18:  

  19:         if(actual == false){

  20:             Log.d("Incube_FormWidget", "E2: iPhone Button is not selected");

  21:         }else{

  22:             Log.d("Incube_FormWidget", "E2: iPhone Button is selected");

  23:         }

  24:         assertEquals("iPhone Button is checked",false, actual);

  25:     

  26:         //select Radio Button Android

  27:         solo.clickOnRadioButton(0);

  28:  

  29:         actual = solo.isRadioButtonChecked("Android");

  30:         if(actual == true){

  31:             Log.d("Incube_FormWidget", "E3: Android Button is selected");

  32:         }else{

  33:             Log.d("Incube_FormWidget", "E3: Android Button is not selected");

  34:         }

  35:         assertEquals("Android Button not selected",true, actual);

  36:         

  37:         //select Radio Button iPhone

  38:         solo.clickOnRadioButton(1);

  39:  

  40:         actual = solo.isRadioButtonChecked("iPhone");

  41:         if(actual == true){

  42:             Log.d("Incube_FormWidget", "E4: iPhone Button is selected");

  43:         }else{

  44:             Log.d("Incube_FormWidget", "E4: iPhone Button is not selected");

  45:         }

  46:         

  47:         assertEquals("iPhone Button is not selected",true, actual);

  48:                 

  49: }

Explanation :
  1. Verify the status of Radio buttons : ‘solo.isRadioButtonChecked()’ is used to verify the status of radio buttons, which returns ‘true’ if the status is checked, otherwise return ‘false’

    • argument can be index or a string
  2. Select the Radio button : ‘solo.clickOnRadioButton()’ is used to select the radio button of a given index
  3. in the above code we used, ‘Log.d()’, which is used to write the log to ‘Logcat’,

    • first parameter passed to Log.d is the ‘Key’, which can be used to uniquely identify the logs in the Logcat.
    • second parameter is the actual message, we are printing to Logcat
    • there are other options like Log.i, Log.d etc.. which i will explain in the upcoming tutorials..

No comments:

Post a Comment

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