AccessibilityNodeInfoExt
public final class AccessibilityNodeInfoExt
Summary
Public methods |
|
|---|---|
static final @NonNull Rect |
boundsInScreen(@NonNull AccessibilityNodeInfo receiver)Returns the bounds of this node in screen coordinates. |
static final @NonNull List<@NonNull AccessibilityNodeInfo> |
children(Returns all the immediate children of this node. |
static final @NonNull List<@NonNull AccessibilityNodeInfo> |
descendants(Performs a DFS on the accessibility tree starting from this node and returns all the descendants discovered recursively. |
static final boolean |
Returns whether this node class name is the same of the given class. |
static final @NonNull UiObject2 |
onElement(Performs a DFS on the accessibility tree starting from this node and returns the first node matching the given |
static final UiObject2 |
onElementOrNull(Performs a DFS on the accessibility tree starting from this node and returns the first node matching the given |
static final @NonNull List<@NonNull UiObject2> |
onElements(Performs a DFS on the accessibility tree starting from this node and returns all the nodes matching the given |
static final @NonNull List<@NonNull AccessibilityNodeInfo> |
siblings(Returns all the siblings of the this node. |
static final String |
simpleViewResourceName(@NonNull AccessibilityNodeInfo receiver)Returns this node's element id resource name without the resource namespace, i.e. only the portion after the "/" character. |
static final @NonNull Bitmap |
takeScreenshot(@NonNull AccessibilityNodeInfo receiver)Takes a screenshot of the screen that contains this node and cuts only the area covered by it. |
static final String |
textAsString(@NonNull AccessibilityNodeInfo receiver)Returns this node's text as a string. |
static final @NonNull StableResult |
waitForStable(Waits for the node to become stable. |
Public methods
boundsInScreen
public static final @NonNull Rect boundsInScreen(@NonNull AccessibilityNodeInfo receiver)
Returns the bounds of this node in screen coordinates. This method is a shortcut for AccessibilityNodeInfo.getBoundsInScreen.
children
public static final @NonNull List<@NonNull AccessibilityNodeInfo> children(
@NonNull AccessibilityNodeInfo receiver,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
Returns all the immediate children of this node. To return all the descendants (including children of children , children of children of children and so on use[ AccessibilityNodeInfo.descendants.
| Parameters | |
|---|---|
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
an optional predicate to filter the node children |
| Returns | |
|---|---|
@NonNull List<@NonNull AccessibilityNodeInfo> |
the list of children of this node, with the given filter if specified. |
descendants
public static final @NonNull List<@NonNull AccessibilityNodeInfo> descendants(
@NonNull AccessibilityNodeInfo receiver,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> filterBlock
)
Performs a DFS on the accessibility tree starting from this node and returns all the descendants discovered recursively. Optionally a filter can be specified. Note that this differs from AccessibilityNodeInfo.children() because this will return also children of children, children of children of children and so on.
| Parameters | |
|---|---|
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> filterBlock |
an optional predicate to filter the node children |
| Returns | |
|---|---|
@NonNull List<@NonNull AccessibilityNodeInfo> |
the list of all the descendants of this node, with the given filter if specified. |
isClass
public static final boolean isClass(
@NonNull AccessibilityNodeInfo receiver,
@NonNull Class<@NonNull ?> cls
)
Returns whether this node class name is the same of the given class.
Usage:
onElement { isClass(Button::class.java) }.click()
onElement
public static final @NonNull UiObject2 onElement(
@NonNull AccessibilityNodeInfo receiver,
long timeoutMs,
long pollIntervalMs,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
Performs a DFS on the accessibility tree starting from this node and returns the first node matching the given block. The node is returned as an UiObject2 that allows interacting with it. If the requested node doesn't exist, a androidx.test.uiautomator.ElementNotFoundException is thrown. Internally it works searching periodically every pollIntervalMs.
Example:
node.onElement { textAsString == "Search" }.click()
| Parameters | |
|---|---|
long timeoutMs |
a timeout to find the element that satisfies the given condition. |
long pollIntervalMs |
an interval to wait before rechecking the accessibility tree for updates. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
onElementOrNull
public static final UiObject2 onElementOrNull(
@NonNull AccessibilityNodeInfo receiver,
long timeoutMs,
long pollIntervalMs,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
Performs a DFS on the accessibility tree starting from this node and returns the first node matching the given block. The node is returned as an UiObject2 that allows interacting with it. If the requested node doesn't exist, null is returned. Internally it works searching periodically every pollIntervalMs.
Example:
node.onElement { textAsString == "Search" }.click()
| Parameters | |
|---|---|
long timeoutMs |
a timeout to find the element that satisfies the given condition. |
long pollIntervalMs |
an interval to wait before rechecking the accessibility tree for updates. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
onElements
public static final @NonNull List<@NonNull UiObject2> onElements(
@NonNull AccessibilityNodeInfo receiver,
long timeoutMs,
long pollIntervalMs,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
Performs a DFS on the accessibility tree starting from this node and returns all the nodes matching the given block. This method stops waiting as soon as a single node with the given condition is returned. The nodes returned are UiObject2 that allow interacting with them. Internally it works searching periodically every pollIntervalMs.
Example:
node.onElements { className == Button::class.java.name }
If multiple nodes are expected but they appear at different times, it's recommended to call androidx.test.uiautomator.waitForStable before, to ensure any operation is complete.
| Parameters | |
|---|---|
long timeoutMs |
a timeout to find the element that satisfies the given condition. |
long pollIntervalMs |
an interval to wait before rechecking the accessibility tree for updates. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
siblings
public static final @NonNull List<@NonNull AccessibilityNodeInfo> siblings(
@NonNull AccessibilityNodeInfo receiver,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> filterBlock
)
Returns all the siblings of the this node.
| Parameters | |
|---|---|
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> filterBlock |
an optional predicate to filter the node siblings |
| Returns | |
|---|---|
@NonNull List<@NonNull AccessibilityNodeInfo> |
the list of the siblings of this node, with the given filter if specified. |
simpleViewResourceName
public static final String simpleViewResourceName(@NonNull AccessibilityNodeInfo receiver)
Returns this node's element id resource name without the resource namespace, i.e. only the portion after the "/" character. If the element id is not specified, then it returns null.
takeScreenshot
public static final @NonNull Bitmap takeScreenshot(@NonNull AccessibilityNodeInfo receiver)
Takes a screenshot of the screen that contains this node and cuts only the area covered by it.
textAsString
public static final String textAsString(@NonNull AccessibilityNodeInfo receiver)
Returns this node's text as a string. This should always be preferred to AccessibilityNodeInfo.text that instead returns a CharSequence, that might be either a String or a android.text.SpannableString. If a text is not specified, then it returns null.
Usage:
onElement { textAsString() == "Some text" }.click()
waitForStable
public static final @NonNull StableResult waitForStable(
@NonNull AccessibilityNodeInfo receiver,
long stableTimeoutMs,
long stableIntervalMs,
long stablePollIntervalMs,
boolean requireStableScreenshot
)
Waits for the node to become stable. A node is considered stable when it and its descendants have not changed over an interval of time. Optionally also the node image can be checked. Internally it works checking periodically that the internal properties of the node have not changed.
| Parameters | |
|---|---|
long stableTimeoutMs |
a timeout for the wait operation, to ensure not waiting forever for stability. |
long stableIntervalMs |
the interval during which the node should not be changing, in order to be considered stable. |
long stablePollIntervalMs |
specifies how often the ui should be checked for changes. |
boolean requireStableScreenshot |
specifies if also the bitmap of the node should not change over the specified |
| Returns | |
|---|---|
@NonNull StableResult |
a |