Showing posts with label Toast Message. Show all posts
Showing posts with label Toast Message. Show all posts

Monday, December 31, 2012

Robotium to test Android List views

Lists in Android present multiple line items in a vertical arrangement. They can be used for data selection as well as drill down navigation.

Robotium provides various functions to test Android Lists. the following are some of them,

  • clickInList(int line)
  • clickLongInList(int line)
  • scrollUpList(int index)
  • scrollDownList(int index)
  • scrollListToTop(int index)
  • scrollListToBottom(int index)
  • scrollListToLine(int index, int line)
Let us see the Usage of the functions with the following example project, which contains a listview with the items from 'Product1' to 'Product19'. 


let us see the usage of the APIs with a Test case

Test case for Android List views:

Steps:
  1. Click on the products list, select 'Product 1', it will open a blank activity. click on Back button to back to list view.
  2. select 'Product 7' from the list and go back to list view
  3. scroll down the list and select 'Product 8' from the list and go back to list view
  4. select 'Product 11' from the list and go back to list view
  5. scroll up the list and select 'product 6' from the list and go back to list view
Test code :
public void testNewProducts() throws Exception {
    //select 'Product 1'
    solo.clickInList(1);
    assertTrue(solo.waitForText("Product 1 selected"));
    solo.goBack();
    
    //select 'Product 6'
    solo.clickInList(1);
    assertTrue(solo.waitForText("Product 6 selected"));
    solo.goBack();
    
    //scroll list to line 7
    solo.scrollListToLine(0,7);
 
    //click on 'Product 8'
    solo.clickInList(1);
    assertTrue(solo.waitForText("Product 8 selected"));
    solo.goBack();
    
    //scroll list to line 10
    solo.scrollListToLine(0, 10);
    
    //select 'Product 11'
    solo.clickInList(1);
    assertTrue(solo.waitForText("Product 11 selected"));
    solo.goBack();
    
    //select 'Product 7'
    solo.scrollListToLine(0, 6);
    solo.clickInList(1);
    assertTrue(solo.waitForText("Product 7 selected"));
    solo.goBack();
    
    //select 'Product 2'
    solo.scrollListToLine(0, 1);
    solo.clickInList(1);
    assertTrue(solo.waitForText("Product 2 selected"));
    solo.goBack();
}


Explanation:


  1. Click on Products list : 'solo.clickInList()', is used to select the list item with a given index (index starts with 1)

    • If you see the first image shown above, the list contains seven items from 'Product1' to  'Product 7' having index from 1 to 7. To select 'Product 7' in the list, use 'solo.clickInList(7)'
    • please note, to select 'Product 8', you can not use index 8.

      • If you see the first image shown above, the list contains seven items from 'Product1' to  'Product 7' having index from 1 to 7.
      • Since 'Product 8' is not listed in the screen, you have to scroll the list to down, to do this use 'solo.scrollDownList()' or use 'solo.scrollListToLine()' to scroll to a give line.
      • After scrolling down, the first item in the list will be assigned index1 and so on..
      • See the second picture shown above, which shows list after scrolling down the list, contains items from 'Product 8' to 'Product 14'. Now, to select 'Product 8', use index of 1 and to select 'Product 14', use index 7.
      • in our example project, it will display a Toast message on selecting a list item, which can be used to assert true/false.

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.