FileDocCategorySizeDatePackage
UiCollection.javaAPI DocAndroid 5.1 API6399Thu Mar 12 22:22:08 GMT 2015com.android.uiautomator.core

UiCollection

public class UiCollection extends UiObject
Used to enumerate a container's UI elements for the purpose of counting, or targeting a sub elements by a child's text or description.
since
API Level 16

Fields Summary
Constructors Summary
public UiCollection(UiSelector selector)
Constructs an instance as described by the selector

param
selector
since
API Level 16

        super(selector);
    
Methods Summary
public UiObjectgetChildByDescription(UiSelector childPattern, java.lang.String text)
Searches for child UI element within the constraints of this UiCollection {@link UiSelector} selector. It looks for any child matching the childPattern argument that has a child UI element anywhere within its sub hierarchy that has content-description text. The returned UiObject will point at the childPattern instance that matched the search and not at the identifying child element that matched the content description.

param
childPattern {@link UiSelector} selector of the child pattern to match and return
param
text String of the identifying child contents of of the childPattern
return
{@link UiObject} pointing at and instance of childPattern
throws
UiObjectNotFoundException
since
API Level 16

        Tracer.trace(childPattern, text);
        if (text != null) {
            int count = getChildCount(childPattern);
            for (int x = 0; x < count; x++) {
                UiObject row = getChildByInstance(childPattern, x);
                String nodeDesc = row.getContentDescription();
                if(nodeDesc != null && nodeDesc.contains(text)) {
                    return row;
                }
                UiObject item = row.getChild(new UiSelector().descriptionContains(text));
                if (item.exists()) {
                    return row;
                }
            }
        }
        throw new UiObjectNotFoundException("for description= \"" + text + "\"");
    
public UiObjectgetChildByInstance(UiSelector childPattern, int instance)
Searches for child UI element within the constraints of this UiCollection {@link UiSelector} selector. It looks for any child matching the childPattern argument that has a child UI element anywhere within its sub hierarchy that is at the instance specified. The operation is performed only on the visible items and no scrolling is performed in this case.

param
childPattern {@link UiSelector} selector of the child pattern to match and return
param
instance int the desired matched instance of this childPattern
return
{@link UiObject} pointing at and instance of childPattern
since
API Level 16

        Tracer.trace(childPattern, instance);
        UiSelector patternSelector = UiSelector.patternBuilder(getSelector(),
                UiSelector.patternBuilder(childPattern).instance(instance));
        return new UiObject(patternSelector);
    
public UiObjectgetChildByText(UiSelector childPattern, java.lang.String text)
Searches for child UI element within the constraints of this UiCollection {@link UiSelector} selector. It looks for any child matching the childPattern argument that has a child UI element anywhere within its sub hierarchy that has text attribute = text. The returned UiObject will point at the childPattern instance that matched the search and not at the identifying child element that matched the text attribute.

param
childPattern {@link UiSelector} selector of the child pattern to match and return
param
text String of the identifying child contents of of the childPattern
return
{@link UiObject} pointing at and instance of childPattern
throws
UiObjectNotFoundException
since
API Level 16

        Tracer.trace(childPattern, text);
        if (text != null) {
            int count = getChildCount(childPattern);
            for (int x = 0; x < count; x++) {
                UiObject row = getChildByInstance(childPattern, x);
                String nodeText = row.getText();
                if(text.equals(nodeText)) {
                    return row;
                }
                UiObject item = row.getChild(new UiSelector().text(text));
                if (item.exists()) {
                    return row;
                }
            }
        }
        throw new UiObjectNotFoundException("for text= \"" + text + "\"");
    
public intgetChildCount(UiSelector childPattern)
Counts child UI element instances matching the childPattern argument. The method returns the number of matching UI elements that are currently visible. The count does not include items of a scrollable list that are off-screen.

param
childPattern a {@link UiSelector} that represents the matching child UI elements to count
return
the number of matched childPattern under the current {@link UiCollection}
since
API Level 16

        Tracer.trace(childPattern);
        UiSelector patternSelector =
                UiSelector.patternBuilder(getSelector(), UiSelector.patternBuilder(childPattern));
        return getQueryController().getPatternCount(patternSelector);