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,
let us see the usage of the APIs with a Test case
Test case for Android List views:
Steps:
Explanation:
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 APIs with a Test case
Test case for Android List views:
Steps:
- Click on the products list, select 'Product 1', it will open a blank activity. click on Back button to back to list view.
- select 'Product 7' from the list and go back to list view
- scroll down the list and select 'Product 8' from the list and go back to list view
- select 'Product 11' from the list and go back to list view
- scroll up the list and select 'product 6' from the list and go back to list view
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:
- 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.
- If you see the first image shown above, the list contains seven items from 'Product1' to 'Product 7' having index from 1 to 7.
- 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)'