Tuesday, September 26, 2017

Use Robotium to test Android Checkbox

Use Robotium to test Android Checkbox


Check Boxes in Android can be tested using Robotium. The following are different functions supported by Robotium to check the Checkbox functionality.  
  • getCurrentCheckBoxes()
  • isCheckBoxChecked(int index)
  • isCheckBoxChecked(String text)
  • clickOnCheckBox(int index)
Let us see the Usage of these functions with a sample test case for the project shown on the Right side (it is a second activity in the Example Project1 used in the previous posts).
The Activity contains three checkboxes, let us write some sample code to test them.

Test case for Android Check Boxes :

Steps :
  1. Click on the Check box �Android�
  2. Click on the Check box �iPhone�
  3. Verify the Status of check boxes
  4. Unselect the Checkboxes �iPhone� and �Android�
  5. Verify the Status again
Test Code :

public void testCheckBoxes() throws Exception {

 solo.waitForActivity("MainActivity");

 

 //click on Image Button

 solo.clickOnImageButton(0);

 

 //look for checkbox text

 actual = solo.searchText("Windows");

 assertEquals("Windows text not found",true, actual);

 

 //select checkbox Android

 solo.clickOnCheckBox(0);

 

 //select checkbox iPhone

 solo.clickOnCheckBox(2);

 

 //check the Status of checkbox Android

 boolean actual1 = solo.isCheckBoxChecked(0);

 assertEquals("Android is not Selected",true, actual1);

 

 //check the Status of checkbox iPhone

 boolean actual2 = solo.isCheckBoxChecked(2);

 assertEquals("iPhone is not Selected",true, actual2);

 

 // unselect Android and iPhone

 solo.clickOnCheckBox(0);

 solo.clickOnCheckBox(2);

 

 //check the status again

 actual1 = solo.isCheckBoxChecked(0);

 assertEquals("Android OS is Selected",false, actual);

 

 actual2 = solo.isCheckBoxChecked(2);

 assertEquals("iPhone is Selected",flase, actual2);

}
Explanation:

  1. Select/Unselect Checkbox : �solo.clickOnCheckBox()� can be for both Select and Unselect checkbox. First time, when used this function, it will select the option and second time when performed the same action, it will unselect the Checkbox.
  2. Verify the Status of Checkboxes : �solo.isCheckBoxChecked()� function is used to verify the status of Checkbox. function returns �true�, if it is selected and �false� otherwise.
  3. Please note that, in the assert statements, while verifying the status of unselected check boxes, we used the expected result as �false�.


download file now