UiObject2Ext
public final class UiObject2Ext
Summary
Public methods |
|
|---|---|
static final @NonNull UiObject2 |
onElement(Performs a DFS on the accessibility tree starting from the node associated to this |
static final UiObject2 |
onElementOrNull(Performs a DFS on the accessibility tree starting from the node associated to this |
static final @NonNull List<@NonNull UiObject2> |
onElements(Performs a DFS on the accessibility tree starting from the node associated to this |
static final @NonNull UiObject2 |
scrollToElement(Keeps scrolling until the given |
static final @NonNull UiObject2 |
scrollToElementOrNull(Keeps scrolling until the given |
static final @NonNull Bitmap |
takeScreenshot(@NonNull UiObject2 receiver)Takes a screenshot of the screen that contains the accessibility node associated to this |
static final @NonNull StableResult |
waitForStable(Waits for the node to become stable. |
Public methods
onElement
public static final @NonNull UiObject2 onElement(
@NonNull UiObject2 receiver,
long timeoutMs,
long pollIntervalMs,
@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 ElementNotFoundException is thrown. Internally it works searching periodically every pollIntervalMs.
Example:
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 UiObject2 receiver,
long timeoutMs,
long pollIntervalMs,
@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:
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 UiObject2 receiver,
long timeoutMs,
long pollIntervalMs,
@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.onElements { 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 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. |
scrollToElement
public static final @NonNull UiObject2 scrollToElement(
@NonNull UiObject2 receiver,
@NonNull Direction direction,
long timeoutMs,
long pollIntervalMs,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
Keeps scrolling until the given block condition is satisfied or until the given timeoutMs. Throws a ElementNotFoundException if the condition is not satisfied at the end of the timeout.
Example:
onElement { isScrollable }.scrollToElement(Direction.DOWN) { id == "button" }.click()| Parameters | |
|---|---|
@NonNull Direction direction |
the scroll |
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. |
scrollToElementOrNull
public static final @NonNull UiObject2 scrollToElementOrNull(
@NonNull UiObject2 receiver,
@NonNull Direction direction,
long timeoutMs,
long pollIntervalMs,
@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:
onElement { isScrollable }.scrollToElement(Direction.DOWN) { id == "button" }.click()| Parameters | |
|---|---|
@NonNull Direction direction |
the scroll |
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. |
takeScreenshot
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.
waitForStable
public static final @NonNull StableResult waitForStable(
@NonNull UiObject2 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 |