Friday, December 28, 2012

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

No comments:

Post a Comment

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