Showing posts with label buttons. Show all posts
Showing posts with label buttons. Show all posts

Friday, September 15, 2017

Using Robotium to test Android Toggle Buttons

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.


download file now

Read more »

Tuesday, September 5, 2017

Use Robotium to test Android Image Buttons

Use Robotium to test Android Image Buttons


As it is common that most of the Android developers will use Image Buttons instead Text buttons, it is important criteria to test Image buttons. 

Robotium has support for Image Buttons. The following APIs can be used to test Image Button functionalities,
  • getImageButton(int index)
  • clickOnImageButton(int index)
Let us see the usage of these APIs with and a our second example project shown on the Right side, which is the starting activity containing four Image Buttons at the bottom (Alarm, Gallery, Music and Home)

Test case for Image Buttons :

Steps :
  1. Click on the Image Button �Alarm�
  2. go back
  3. Click on the Image Button �Music�
Test Code :
 1: public void testCheckBoxes() throws Exception {

 2: solo.waitForActivity("AlarmPlusActivity");

 3: 

 4: //click on Alarm Image Button

 5: solo.clickOnImageButton(0);

 6: assertTrue(solo.searchText("Add alarm")); //this text appears in the next activity

 7: //go to previous screen 

 8: solo.goBack();

 9: //click on Gallery button

 10: solo.clickOnImageButton(1);

 11:  

 12: }

Explanation :


  1. Click on Image Button : �solo.clickOnImageButton()�, is used to click on the Image button, which accepts only index number as argument.
  2. Return to Previous screen, �solo.goBack()�, is used to go to Previous screen.
  3. index of the Image buttons increase from Left to Right. Alarm button has 0, Gallery button has 1 and Home button with index


download file now

Read more »