Testing UIAutomator for Android: the number of all items in the list

When testing mobile UI Automation using UIAutomator for Android, I need to find out all the elements that are present in the list view.

Using the getChildCount () method, as shown below, I get a count of currently visible items only , but more items are present in the list view, but they are invisible.

Here is a sample code:

    //Created UI Object for list view
UiObject listview_elements = new UiObject(new UiSelector().className("android.widget.ListView"));


//Printing the numbmer of child ements present in the List View by using getchildCount() method
System.out.println("List view elements : "+listview_elements.getChildCount());*

Can anyone help get the number of all list items , including invisible items (not currently displayed on screen).

Note: Please note that here I am not implementing the Android user interface, rather I am just testing the third-party Android user interface for Android using UIAutomator for Android.

+4
source share
3 answers

A bit late, but here are some tips.

Remember that you can use the UI Automator Viewer to identify resource identifiers, etc. AndroidSDKPath \ tools \ uiautomatorviewer

A few considerations that change the approach to this.

  • Are the items available in the list?
  • Are the items in the list the same size?
  • Do elements contain separate text fields?

Assuming the items in the list can be clicked, you can use the selector as follows:

UiSelector selector = new UiSelector().clickable(true);

, Java , .

, . , , , , ( ). , , .

, , , 0.

:

//UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());;
UiObject list = device.findObject(new UiSelector().resourceId(PACKAGE + ":id/" + resourceId));

int index = 0;
int count = 0;

while (index < list.getChildCount()) {
    UiObject listItem = list.getChild(selector.index(index));

    Set<String> examinedItems = new LinkedHashSet<>();

    //if first item is a bit out of view (under toolbar) then skip it
    if (listItem.getBounds().top < TOOLBAR_BOTTOM_Y) {
        index++;
        continue;
        //this will only ever happen once = reached end of list
    }

    //get unique details from each item
    //for example, you might get a text field for that list item list this
    //UiObject textField = listItem.getChild(new UiSelector().resourceId(PACKAGE + ":id/" + childResourceId));
    String itemDetails = ...;

    //this would be relevent if the list was perfectly scrolled to the top and we don't know we are at the end of the list
    if (examinedItems.contains(itemDetails)) {
        index++;
        continue;
    }

    //do any actual testing on the item here..
    count++;

    //if index > 0 we have reached the end of the list
    if (index == 0) {
        //you'll need to inherit from InstrumentationTestCase so you can pass an instance to this method
        TouchUtils.drag(this, CENTER_X, CENTER_X, START_SCROLL_Y, START_SCROLL_Y - ITEM_HEIGHT, 150);
    }

    examinedItems.add(itemDetails);
}

//maybe return count here
+1

( , ), :

ListView UiScrollable.

scrollToBeginning() ListView.

getChildCount(),

scrollForward(), .

getChildCount() , .

, , , , , scrollToEnd() false ( ). scrollToEnd() true, , , . , ( ), getChildBy...().

0

listView.getCount() - listView.

-4

Source: https://habr.com/ru/post/1533079/


All Articles