Monday, December 31, 2012

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.

No comments:

Post a Comment

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