Showing posts with label CheckBox. Show all posts
Showing posts with label CheckBox. Show all posts

Monday, December 31, 2012

Robotoum to test Android Toast Messages

Toasts in Android are the small popup appear on the screen, used to provide feedback about an operation. Robotium supports various functions to detect the Toast messages appear on the Screen.
  • searchText(String text)
  • waitForText(String text)
these functions helps in detecting the Toast messages appear on the screen.

Let us see the usage of these functions with the help of a test case for the example project shown on the Right side. 
Right side shown is the second activity in the Example project1. there are three checkboxes present, on selecting the checkboxes and clicking on ‘Submit’ button will show toast message on the screen.

Test case for detecting Toast Message:

Steps :
  1. go to second activity in the Example Project1 by clicking on the Image button (the picture is not shown here..)
  2. in the second activity, select Checkboxes ‘Android’ and ‘Windows’ and click on ‘Submit’ button
  3. detect the Toast Message appear on the screen
  4. Assert True/False, based on the toast seen
Test code :
   1: public void testToastMessages() 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:     //look for submit button

  11:     actual = solo.searchButton("Submit");

  12:     assertEquals("Submit Button not found",true, actual);

  13:             

  14:     //select two checkboxes and click on submit and look for the Toast

  15:     solo.clickOnCheckBox(0);

  16:     solo.clickOnCheckBox(1);

  17:     solo.clickOnButton("Submit");

  18:     assertTrue(solo.waitForText("Android and Windows are Selected"));

  19: }

Explanation:


  1. ‘solo.clickOnImageButton()’, is used to click the Image button appearing in the first screen. on click this image button, it will go to second activity that is shown in the above example

  2. Detect the Toast Message: toast Message can be detected using ‘solo.searchText()’ or by using ‘solo.WaitForText()’.

    • ‘solo.searchText()’ is used to detect the Toast Message appearing on the screen, which returns a boolean value true/false.

    • ‘solo.waitForText()’ is used to wait until the message appears on the screen, which is more helpful function while working with Toasts.  If text is found on the screen, returns true otherwise return false.

    • in the above example, there are two Toast messages appear, one is on clicking Image Button and other on selecting the Checkboxes and clicking on ‘Submit’ Button.

  3. Button : ‘solo.searchButton()’, is used to look for a Button with a given text

    • ‘solo.clickOnButton()’, is used to click on the Button appearing with a given text or index.

Friday, December 28, 2012

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’.