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.

2 comments:

  1. Hi Srikanth,

    I also wrote some test case for toast message recently, but I think this way is wrong, cause I used 'Android' instead of 'Android and Windows are Selected' and the test is still pass. And now I am confused. Do you have noticed this problem.

    ReplyDelete
    Replies
    1. @John Zhang: I think in your case the test still passes because it has found the string 'Android' in the toast message. I think it does not check for the correctness for the entirety of the string 'Android and Windows are Selected'.

      Try changing your test string to 'Andus' and it should fail.

      Delete

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