UiAutomatorTestScope
public class UiAutomatorTestScope
A UiAutomator scope that allows to easily access UiAutomator api and utils class.
Summary
Protected constructors |
|---|
Public methods |
|
|---|---|
final @NonNull AccessibilityNodeInfo |
Returns the active window root node. |
final void |
Clears the instrumentation test target app data. |
final @NonNull UiDevice |
|
final @NonNull Instrumentation |
|
final @NonNull UiAutomation |
|
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 |
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 |
final @NonNull List<@NonNull UiObject2> |
onElements(Performs a DFS on the accessibility tree starting from the root node in the active window and returns the first node matching the given |
final boolean |
Press the back key. |
final void |
pressDelete(int count)Similar to |
final boolean |
Press the enter key. |
final boolean |
Press the home key. |
final void |
startActivity(@NonNull Class<@NonNull ?> clazz)Starts an activity with the given class. |
final void |
startActivity(@NonNull String packageName, @NonNull String activityName)Starts an activity with the given |
final void |
startApp()Starts the instrumentation test target app using the target app package name. |
final void |
Starts the app with the given |
final void |
startIntent(@NonNull Intent intent)Starts the given |
final void |
Types the given |
final void |
Unregisters all the watchers previously registered with |
final boolean |
waitForAppToBeVisible(@NonNull String appPackageName, long timeoutMs)Waits for an application to become visible. |
final @NonNull StableResult |
waitForStableInActiveWindow(Waits for the root node of the active window to become stable. |
final @NonNull WatcherRegistration |
<T extends Object> watchFor(Registers a watcher for this |
final @NonNull List<@NonNull AccessibilityNodeInfo> |
Returns all the window roots on all the displays. |
final @NonNull List<@NonNull AccessibilityWindowInfo> |
windows()Returns all the windows on all the displays. |
Protected constructors
Public methods
activeWindowRoot
public final @NonNull AccessibilityNodeInfo activeWindowRoot()
Returns the active window root node. Note that calling this method after startApp, startActivity or startIntent without waiting for the app to be visible, will return the active window root at the time of starting the app, i.e. the root of the launcher if starting from there.
clearAppData
public final void clearAppData()
Clears the instrumentation test target app data.
getInstrumentation
public final @NonNull Instrumentation getInstrumentation()
onElement
public final @NonNull UiObject2 onElement(
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. 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 final UiObject2 onElementOrNull(
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. 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 final @NonNull List<@NonNull UiObject2> onElements(
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. Internally it works searching periodically every pollIntervalMs.
Example:
node.onElements { isClass(Button::class.java) }| 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 final void pressDelete(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. |
startActivity
public final void startActivity(@NonNull Class<@NonNull ?> clazz)
Starts an activity with the given class.
startActivity
public final void startActivity(@NonNull String packageName, @NonNull String activityName)
Starts an activity with the given packageName and activityName.
startApp
public final void startApp()
Starts the instrumentation test target app using the target app package name.
startApp
public final void startApp(@NonNull String packageName)
Starts the app with the given packageName.
startIntent
public final void startIntent(@NonNull Intent intent)
Starts the given intent.
type
public final void type(@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.
unregisterWatchers
public final void unregisterWatchers()
Unregisters all the watchers previously registered with watchFor.
waitForAppToBeVisible
public final boolean waitForAppToBeVisible(@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. |
waitForStableInActiveWindow
public final @NonNull StableResult waitForStableInActiveWindow(
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 |
watchFor
public final @NonNull WatcherRegistration <T extends Object> watchFor(
@NonNull ScopedUiWatcher<@NonNull T> watcher,
@NonNull Function1<@NonNull T, Unit> block
)
Registers a watcher for this androidx.test.uiautomator.UiAutomatorTestScope to handle unexpected UI elements. Internally this method uses the existing UiDevice.registerWatcher api. When the given ScopedUiWatcher.isVisible condition is satisfied, then the given block is executed. scope. This method returns a handler with the WatcherRegistration to unregister it before the block is complete. Note that this api helps with unexpected ui elements, such as system dialogs, and that for expected dialogs the onElement api should be used.
Usage:
@Test fun myTest() = uiAutomator {
// Registers a watcher for a permission dialog.
watchFor(PermissionDialog) { clickAllow() }
// Registers a watcher for a custom dialog and unregisters it.
val registration = watchFor(MyDialog) { clickSomething() }
// Do something...
registration.unregister()
}
| Parameters | |
|---|---|
@NonNull ScopedUiWatcher<@NonNull T> watcher |
the dialog to watch for. |
@NonNull Function1<@NonNull T, Unit> block |
a block to handle. |
| Returns | |
|---|---|
@NonNull WatcherRegistration |
the dialog registration. |
windowRoots
public final @NonNull List<@NonNull AccessibilityNodeInfo> windowRoots()
Returns all the window roots on all the displays.
windows
public final @NonNull List<@NonNull AccessibilityWindowInfo> windows()
Returns all the windows on all the displays.