UiObject2Ext

Added in 2.4.0-alpha02

public final class UiObject2Ext


Summary

Public methods

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

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 and returns the first node matching the given block.

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

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 and returns the first node matching the given block.

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

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 and returns all the nodes matching the given block.

static final @NonNull UiObject2
scrollToView(
    @NonNull UiObject2 receiver,
    @NonNull Direction direction,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs.

static final @NonNull UiObject2
scrollToViewOrNull(
    @NonNull UiObject2 receiver,
    @NonNull Direction direction,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs.

static final @NonNull Bitmap

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

Public methods

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

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 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. 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
@NonNull UiObject2

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

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

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 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 UiObject2 receiver,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 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 { isClass(Button::class.java) }

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 UiObject2 scrollToView(
    @NonNull UiObject2 receiver,
    @NonNull Direction direction,
    long timeoutMs,
    long pollIntervalMs,
    @ExtensionFunctionType @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs. Throws a ViewNotFoundException if the condition is not satisfied at the end of the timeout.

Example:

onView { isScrollable }.scrollToView(Direction.DOWN) { id == "button" }.click()
Parameters
@NonNull Direction direction

the scroll Direction.

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 that matches the given block condition.

scrollToViewOrNull

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

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs. Returns null if the condition is not satisfied at the end of the timeout.

Example:

onView { isScrollable }.scrollToView(Direction.DOWN) { id == "button" }.click()
Parameters
@NonNull Direction direction

the scroll Direction.

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 that matches the given block condition.

public static final @NonNull Bitmap takeScreenshot(@NonNull UiObject2 receiver)

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

Returns
@NonNull Bitmap

a bitmap containing the image of the node associated to this UiObject2.