Monday, December 31, 2012

Using Robotium to test Android “Text Fields”

Robotium supports various functions to test Android single and multi line text fields.
The following are few APIs supported by Robotium to test the Text Fields,
  • searchEditText(String text)
  • clickOnEditText(int index)
  • clearEditText(int index)
  • typeText(int index, String text)
  • enterText(int index, String text)
Let us write a test case for the Sample project shown on the right side,

Test case for verifying the Text Fields :

Steps :
  1. Wait for the activity to launch
  2. Enter a text in the Text Field
  3. Verify the text entered in the Text Field
  4. Clear the Text Entered in the Text Field
  5. Enter a new Test again and Verify the text entered.
Test Code:
   1: public void testEditField() throws Exception {

   2:    solo.waitForActivity("MainActivity");

   3:             

   4:    solo.clickOnEditText(0);

   5:    String strInput = "Sample code 1";

   6:    solo.enterText(0, strInput);

   7:    boolean actual = solo.searchEditText(strInput);

   8:    assertEquals("text entered is not matching",true, actual);

   9:  

  10:    solo.clearEditText(0);

  11:    strInput = "Sample code 2";

  12:    solo.typeText(0, strInput);

  13:    actual = solo.searchEditText(strInput);

  14:    assertEquals("text entered is not matching",true, actual);

  15:  }

Explanation:
  1. solo.waitforActivity( ), is to wait for the activity to launch

  2. Enter Text in the Text Field :  to enter a text in the Text filed, both ‘solo.enterText()’ and ‘solo.typeText()’ can be used.

  3. in ‘enterText(int index, String text)’,  index is the index of the Text Field.

    • if multiple Text Fields exists, first one will be assigned an index of 0, second one will get an index 1 and so on..  index increases from Top to Bottom or Left to Right

    • Verify the Text Entered :  to verify the text contained in the Text Filed, ‘solo.searchEditText()’ can be used. it returns True, if pattern matches otherwise return False

  4. Clear the Text in the Text Field : to clear the Text exist in the Text Field, ‘solo.clearEditText()’ can be used.

  5. ‘assertEquals()’, can be used to assert True/False

Using Robotium to test Android Toggle Buttons

Robotium supports various APIs to test Android Toggle Button functionality

the following are some of the APIs supported by Robotium to test the Toggle Buttons

  • searchToggleButton(String text)
  • clickOnToggleButton(String name)
  • isToggleButtonChecked(int index)
  • isToggleButtonChecked(String text)
Let us write a test case for the Example project shown on the Right side, which has one toggle button

Test case for testing Toggle Button :

Steps:
  1. Search for the Toggle Button
  2. Assert True/False based on the search result
  3. Click on the Toggle Button
  4. Check if Toggle Button status is changed or not?
  5. Click on the Toggle Button again
  6. Check whether the Toggle Button status is changed or not?
Test code:

   1: public void testToggleButton() throws Exception {

   2:     solo.waitForActivity("MainActivity");

   3:  

   4:     //search for toggle button

   5:     actual = solo.searchToggleButton("OFF");

   6:     assertEquals("Toggle Button not found",true, actual);

   7:  

   8:     //Turn ON toggle button

   9:     solo.clickOnToggleButton("OFF");

  10:     actual = solo.isToggleButtonChecked(0);

  11:     assertEquals("Toggle Button is OFF",true, actual);

  12:  

  13:     //Turn OFF toggle button

  14:     solo.clickOnToggleButton("ON");

  15:     actual = solo.isToggleButtonChecked(0);

  16:     assertEquals("Toggle Button is ON",false, actual);

  17:              

  18: }
Explanation:
  1. Search for Toggle Button : ‘solo.searchToggleButton()’ is used to search for toggle buttons in the current activity

    • the functions returns ‘true’ if the Toggle Button with the given string is found otherwise return ‘false’

  2. Click on the Toggle Button : ‘solo.clickOnToggleButton()’ is used to click on Toggle Button with the given string

    • on enabling the Toggle Button, it’s text is changed from “OFF” to “ON”. hence, second time we modified the search pattern to “ON”

  3. Check the Status of Toggle Button : ‘isToggleButtonChecked()’ is used to check the status of Toggle Button

    • argument passed can be a String or Index
    • returns true if the toggle button is checked, otherwise, return false.

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.

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