public class

UiCollection

extends UiObject

 java.lang.Object

androidx.test.uiautomator.UiObject

↳androidx.test.uiautomator.UiCollection

Subclasses:

UiScrollable

Gradle dependencies

compile group: 'androidx.test.uiautomator', name: 'uiautomator', version: '2.4.0-alpha01'

  • groupId: androidx.test.uiautomator
  • artifactId: uiautomator
  • version: 2.4.0-alpha01

Artifact androidx.test.uiautomator:uiautomator:2.4.0-alpha01 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.uiautomator:uiautomator com.android.support.test.uiautomator:uiautomator

Androidx class mapping:

androidx.test.uiautomator.UiCollection android.support.test.uiautomator.UiCollection

Overview

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.

Summary

Fields
from UiObjectFINGER_TOUCH_HALF_WIDTH, SWIPE_MARGIN_LIMIT, WAIT_FOR_EVENT_TMEOUT, WAIT_FOR_SELECTOR_POLL, WAIT_FOR_SELECTOR_TIMEOUT, WAIT_FOR_WINDOW_TMEOUT
Constructors
publicUiCollection(UiSelector selector)

Constructs an instance as described by the selector

Methods
public UiObjectgetChildByDescription(UiSelector childPattern, java.lang.String text)

Searches for child UI element within the constraints of this UiCollection UiSelector selector.

public UiObjectgetChildByInstance(UiSelector childPattern, int instance)

Searches for child UI element within the constraints of this UiCollection UiSelector selector.

public UiObjectgetChildByText(UiSelector childPattern, java.lang.String text)

Searches for child UI element within the constraints of this UiCollection UiSelector selector.

public intgetChildCount(UiSelector childPattern)

Counts child UI element instances matching the childPattern argument.

from UiObjectclearTextField, click, clickAndWaitForNewWindow, clickAndWaitForNewWindow, clickBottomRight, clickTopLeft, dragTo, dragTo, exists, findAccessibilityNodeInfo, getBounds, getChild, getChildCount, getClassName, getContentDescription, getFromParent, getPackageName, getSelector, getText, getVisibleBounds, isCheckable, isChecked, isClickable, isEnabled, isFocusable, isFocused, isLongClickable, isScrollable, isSelected, longClick, longClickBottomRight, longClickTopLeft, performMultiPointerGesture, performTwoPointerGesture, pinchIn, pinchOut, setText, swipeDown, swipeLeft, swipeRight, swipeUp, waitForExists, waitUntilGone
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public UiCollection(UiSelector selector)

Constructs an instance as described by the selector

Parameters:

selector:

Methods

public UiObject getChildByDescription(UiSelector childPattern, java.lang.String text)

Searches for child UI element within the constraints of this UiCollection 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.

Parameters:

childPattern: UiSelector selector of the child pattern to match and return
text: String of the identifying child contents of of the childPattern

Returns:

UiObject pointing at and instance of childPattern

public UiObject getChildByInstance(UiSelector childPattern, int instance)

Searches for child UI element within the constraints of this UiCollection 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.

Parameters:

childPattern: UiSelector selector of the child pattern to match and return
instance: int the desired matched instance of this childPattern

Returns:

UiObject pointing at and instance of childPattern

public UiObject getChildByText(UiSelector childPattern, java.lang.String text)

Searches for child UI element within the constraints of this UiCollection 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.

Parameters:

childPattern: UiSelector selector of the child pattern to match and return
text: String of the identifying child contents of of the childPattern

Returns:

UiObject pointing at and instance of childPattern

public int getChildCount(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.

Parameters:

childPattern: a UiSelector that represents the matching child UI elements to count

Returns:

the number of matched childPattern under the current UiCollection

Source

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.uiautomator;

import androidx.annotation.NonNull;

/**
 * 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.
 */
public class UiCollection extends UiObject {

    /**
     * Constructs an instance as described by the selector
     *
     * @param selector
     */
    public UiCollection(@NonNull UiSelector selector) {
        super(selector);
    }

    /**
     * Searches for child UI element within the constraints of this UiCollection {@link UiSelector}
     * selector.
     *
     * It looks for any child matching the <code>childPattern</code> argument that has
     * a child UI element anywhere within its sub hierarchy that has content-description text.
     * The returned UiObject will point at the <code>childPattern</code> instance that matched the
     * search and not at the identifying child element that matched the content description.</p>
     *
     * @param childPattern {@link UiSelector} selector of the child pattern to match and return
     * @param text String of the identifying child contents of of the <code>childPattern</code>
     * @return {@link UiObject} pointing at and instance of <code>childPattern</code>
     * @throws UiObjectNotFoundException
     */
    @NonNull
    public UiObject getChildByDescription(@NonNull UiSelector childPattern, @NonNull String text)
            throws UiObjectNotFoundException {
        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 + "\"");
    }

    /**
     * Searches for child UI element within the constraints of this UiCollection {@link UiSelector}
     * selector.
     *
     * It looks for any child matching the <code>childPattern</code> argument that has
     * a child UI element anywhere within its sub hierarchy that is at the <code>instance</code>
     * 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 <code>childPattern</code>
     * @return {@link UiObject} pointing at and instance of <code>childPattern</code>
     */
    @NonNull
    public UiObject getChildByInstance(@NonNull UiSelector childPattern, int instance)
            throws UiObjectNotFoundException {
        UiSelector patternSelector = UiSelector.patternBuilder(getSelector(),
                UiSelector.patternBuilder(childPattern).instance(instance));
        return new UiObject(patternSelector);
    }

    /**
     * Searches for child UI element within the constraints of this UiCollection {@link UiSelector}
     * selector.
     *
     * It looks for any child matching the <code>childPattern</code> argument that has
     * a child UI element anywhere within its sub hierarchy that has text attribute =
     * <code>text</code>. The returned UiObject will point at the <code>childPattern</code>
     * instance that matched the search and not at the identifying child element that matched the
     * text attribute.</p>
     *
     * @param childPattern {@link UiSelector} selector of the child pattern to match and return
     * @param text String of the identifying child contents of of the <code>childPattern</code>
     * @return {@link UiObject} pointing at and instance of <code>childPattern</code>
     * @throws UiObjectNotFoundException
     */
    @NonNull
    public UiObject getChildByText(@NonNull UiSelector childPattern, @NonNull String text)
            throws UiObjectNotFoundException {
        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 + "\"");
    }

    /**
     * Counts child UI element instances matching the <code>childPattern</code>
     * 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}
     */
    public int getChildCount(@NonNull UiSelector childPattern) {
        UiSelector patternSelector =
                UiSelector.patternBuilder(getSelector(), UiSelector.patternBuilder(childPattern));
        return getQueryController().getPatternCount(patternSelector);
    }
}