UiDeviceExt
public final class UiDeviceExt
Summary
Public methods |
|
|---|---|
static final @NonNull AccessibilityWindowInfo |
activeWindow(@NonNull UiDevice receiver)Returns the active window. |
static final @NonNull UiObject2 |
onElement(Performs a DFS on the accessibility tree starting from the root node in the active window and returns the first node matching the given |
static final UiObject2 |
onElementOrNull(Performs a DFS on the accessibility tree starting from the root node in the active window 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 void |
pressDelete(@NonNull UiDevice receiver, int count)Similar to |
static final void |
Types the given |
static final boolean |
waitForAppToBeVisible(Waits for an application to become visible. |
static final @NonNull AccessibilityNodeInfo |
waitForRootInActiveWindow(Waits for the root node to become available in this window. |
static final @NonNull StableResult |
waitForStableInActiveWindow(Waits for the root node of the active window to become stable. |
static final @NonNull List<@NonNull AccessibilityWindowInfo> |
Returns all the windows on all the displays. |
Public methods
activeWindow
public static final @NonNull AccessibilityWindowInfo activeWindow(@NonNull UiDevice receiver)
Returns the active window.
onElement
public static final @NonNull UiObject2 onElement(
@NonNull UiDevice receiver,
long timeoutMs,
long pollIntervalMs,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
Performs a DFS on the accessibility tree starting from the root node in the active 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 ElementNotFoundException is thrown. Internally it works searching periodically every pollIntervalMs.
Example:
uiDevice.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 UiDevice receiver,
long timeoutMs,
long pollIntervalMs,
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
Performs a DFS on the accessibility tree starting from the root node in the active 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:
uiDevice.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 UiDevice 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. |
pressDelete
public static final void pressDelete(@NonNull UiDevice receiver, int count)
Similar to type but presses the delete key for the given count times.
| Parameters | |
|---|---|
int count |
how many times the press delete key should be pressed. |
type
public static final void type(@NonNull UiDevice receiver, @NonNull String text)
Types the given text string simulating key press through Instrumentation.sendKeySync. This is similar to tapping the keys on a virtual keyboard and will trigger the same listeners in the target app, as opposed to AccessibilityNodeInfo.setText that programmaticaly sets the given text in the target node.
waitForAppToBeVisible
public static final boolean waitForAppToBeVisible(
@NonNull UiDevice receiver,
@NonNull String appPackageName,
long timeoutMs
)
Waits for an application to become visible. Note that internally it checks if an accessibility node with the given appPackageName exists in the accessibility tree.
| Parameters | |
|---|---|
@NonNull String appPackageName |
the package name of the app to wait for. By default is the target app package name. |
long timeoutMs |
a timeout for the app to become visible. |
| Returns | |
|---|---|
boolean |
whether the app became visible in the given timeout. |
waitForRootInActiveWindow
public static final @NonNull AccessibilityNodeInfo waitForRootInActiveWindow(
@NonNull UiDevice 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. |
| Throws | |
|---|---|
kotlin.IllegalStateException |
if the root node in the active window cannot be acquired before the give timeout |
waitForStableInActiveWindow
public static final @NonNull StableResult waitForStableInActiveWindow(
@NonNull UiDevice receiver,
long stableTimeoutMs,
long stableIntervalMs,
long stablePollIntervalMs,
boolean requireStableScreenshot
)
Waits for the root node of the active 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 |
| Returns | |
|---|---|
@NonNull StableResult |
a |