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.

No comments:

Post a Comment

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