AccessibilityNodeInfoExt

Added in 2.4.0-alpha02

public final class AccessibilityNodeInfoExt


Summary

Public methods

static final @NonNull Rect

Returns the bounds of this node in screen coordinates.

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.

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.

static final boolean
isClass(
    @NonNull AccessibilityNodeInfo receiver,
    @NonNull Class<@NonNull ?> cls
)

Returns whether this node class name is the same of the given class.

static final @NonNull UiObject2
onView(
    @NonNull AccessibilityNodeInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @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.

static final UiObject2
onViewOrNull(
    @NonNull AccessibilityNodeInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @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.

static final @NonNull List<@NonNull UiObject2>
onViews(
    @NonNull AccessibilityNodeInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @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.

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.

static final String

Returns this node's view id resource name without the resource namespace, i.e. only the portion after the "/" character.

static final @NonNull Bitmap

Takes a screenshot of the screen that contains this node and cuts only the area covered by it.

static final String

Returns this node's text as a string.

static final @NonNull StableResult
waitForStable(
    @NonNull AccessibilityNodeInfo receiver,
    long stableTimeoutMs,
    long stableIntervalMs,
    long stablePollIntervalMs,
    boolean requireStableScreenshot
)

Waits for the node to become stable.

Public methods

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.

Returns
@NonNull Rect

a Rect with the bounds of this node within the screen limits.

public static final @NonNull List<@NonNull AccessibilityNodeInfochildren(
    @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.

public static final @NonNull List<@NonNull AccessibilityNodeInfodescendants(
    @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.

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:

onView { isClass(Button::class.java) }.click()
public static final @NonNull UiObject2 onView(
    @NonNull AccessibilityNodeInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @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.ViewNotFoundException is thrown. Internally it works searching periodically every pollIntervalMs.

Example:

node.onView { textAsString == "Search" }.click()
Parameters
long timeoutMs

a timeout to find the view that satisfies the given condition.

long pollIntervalMs

an interval to wait before rechecking the accessibility tree for updates.

@ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block

a block that specifies a condition on the node to find.

Returns
@NonNull UiObject2

a UiObject2 from a node that matches the given block condition.

public static final UiObject2 onViewOrNull(
    @NonNull AccessibilityNodeInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @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.onView { textAsString == "Search" }.click()
Parameters
long timeoutMs

a timeout to find the view that satisfies the given condition.

long pollIntervalMs

an interval to wait before rechecking the accessibility tree for updates.

@ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block

a block that specifies a condition on the node to find.

Returns
UiObject2

a UiObject2 from a node that matches the given block condition or null.

public static final @NonNull List<@NonNull UiObject2onViews(
    @NonNull AccessibilityNodeInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @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.onViews { 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 view that satisfies the given condition.

long pollIntervalMs

an interval to wait before rechecking the accessibility tree for updates.

@ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block

a block that specifies a condition on the node to find.

Returns
@NonNull List<@NonNull UiObject2>

a list of UiObject2 from nodes that matches the given block condition.

public static final @NonNull List<@NonNull AccessibilityNodeInfosiblings(
    @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 view id resource name without the resource namespace, i.e. only the portion after the "/" character. If the view id is not specified, then it returns null.

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.

Returns
@NonNull Bitmap

a bitmap containing the image of this node.

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:

onView { textAsString() == "Some text" }.click()
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 stableIntervalMs. Note that this won't work with views that change constantly, like a video player.

Returns
@NonNull StableResult

a androidx.test.uiautomator.StableResult containing the latest acquired view hierarchy and screenshot, and a flag indicating if the node was stable before timeout.