AccessibilityWindowInfoExt

Added in 2.4.0-alpha02

public final class AccessibilityWindowInfoExt


Summary

Public methods

static final @NonNull UiObject2
onView(
    @NonNull AccessibilityWindowInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Performs a DFS on the accessibility tree starting from the root node of this window and returns the first node matching the given block.

static final UiObject2
onViewOrNull(
    @NonNull AccessibilityWindowInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Performs a DFS on the accessibility tree starting from the root node of this window and returns the first node matching the given block.

static final @NonNull List<@NonNull UiObject2>
onViews(
    @NonNull AccessibilityWindowInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Performs a DFS on the accessibility tree starting from the root node of this window and returns all the nodes matching the given block.

static final @NonNull Bitmap

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

static final @NonNull AccessibilityNodeInfo
waitForRoot(
    @NonNull AccessibilityWindowInfo receiver,
    long timeoutMs,
    long sleepIntervalMs,
    boolean clearCache
)

Waits for the root node to become available in this window.

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

Waits for the root node of this window to become stable.

Public methods

public static final @NonNull UiObject2 onView(
    @NonNull AccessibilityWindowInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Performs a DFS on the accessibility tree starting from the root node of this window 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 ViewNotFoundException is thrown.

Example:

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 AccessibilityWindowInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Performs a DFS on the accessibility tree starting from the root node of this window 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:

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 AccessibilityWindowInfo receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Performs a DFS on the accessibility tree starting from the root node of this window 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 Bitmap takeScreenshot(@NonNull AccessibilityWindowInfo receiver)

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

Returns
@NonNull Bitmap

a bitmap containing the image of this node.

public static final @NonNull AccessibilityNodeInfo waitForRoot(
    @NonNull AccessibilityWindowInfo receiver,
    long timeoutMs,
    long sleepIntervalMs,
    boolean clearCache
)

Waits for the root node to become available in this window.

Parameters
long timeoutMs

a timeout for the root node to become available.

long sleepIntervalMs

a interval to wait before retrying checking if the node is available.

boolean clearCache

whether the accessibility nodes cache should be cleared when checking.

Returns
@NonNull AccessibilityNodeInfo

the root node for this window.

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

Waits for the root node of this window 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 StableResult containing the latest acquired view hierarchy and screenshot, and a flag indicating if the node was stable before timeout.