Showing posts with label Image Button. Show all posts
Showing posts with label Image Button. 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 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