UiAutomatorTestScope
open class UiAutomatorTestScope
MacrobenchmarkScope |
Provides access to common operations in app automation, such as killing the app, or navigating home. |
A UiAutomator scope that allows to easily access UiAutomator api and utils class.
Summary
Protected constructors |
|---|
Public functions |
|
|---|---|
AccessibilityNodeInfo |
Returns the active window root node. |
Unit |
Clears the instrumentation test target app data. |
UiObject2 |
onElement(timeoutMs: Long, pollIntervalMs: Long, block: AccessibilityNodeInfo.() -> Boolean)Performs a DFS on the accessibility tree starting from the root node in the active window and returns the first node matching the given |
UiObject2? |
onElementOrNull(timeoutMs: Long, pollIntervalMs: Long, block: AccessibilityNodeInfo.() -> Boolean)Performs a DFS on the accessibility tree starting from the root node in the active window and returns the first node matching the given |
List<UiObject2> |
onElements(timeoutMs: Long, pollIntervalMs: Long, block: AccessibilityNodeInfo.() -> Boolean)Performs a DFS on the accessibility tree starting from the root node in the active window and returns the first node matching the given |
Boolean |
Press the back key. |
Unit |
pressDelete(count: Int)Similar to |
Boolean |
Press the enter key. |
Boolean |
Press the home key. |
Unit |
startActivity(clazz: Class<*>, intentFlags: List<Int>)Starts an activity with the given class. |
Unit |
startActivity(Starts an activity with the given |
Unit |
startActivityIntent(intent: Intent)Starts the given |
Unit |
Starts the app with the given |
Unit |
Types the given |
Unit |
Unregisters all the watchers previously registered with |
Boolean |
waitForAppToBeVisible(appPackageName: String, timeoutMs: Long)Waits for an application to become visible. |
StableResult |
waitForStableInActiveWindow(Waits for the root node of the active window to become stable. |
WatcherRegistration |
<T : Any?> watchFor(watcher: ScopedUiWatcher<T>, block: T.() -> Unit)Registers a watcher for this |
List<AccessibilityNodeInfo> |
Returns all the window roots on all the displays. |
List<AccessibilityWindowInfo> |
windows()Returns all the windows on all the displays. |
Protected functions |
|
|---|---|
open Unit |
startIntentAndWait(intent: Intent)Default implementation of synchronous activity launch. |
Public properties |
|
|---|---|
UiDevice |
|
Instrumentation |
|
UiAutomation |
Protected constructors
Public functions
activeWindowRoot
fun activeWindowRoot(): AccessibilityNodeInfo
Returns the active window root node.
clearAppData
fun clearAppData(): Unit
Clears the instrumentation test target app data.
onElement
fun onElement(
timeoutMs: Long = 10000,
pollIntervalMs: Long = 100,
block: AccessibilityNodeInfo.() -> Boolean
): UiObject2
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 | |
|---|---|
timeoutMs: Long = 10000 |
a timeout to find the element that satisfies the given condition. |
pollIntervalMs: Long = 100 |
an interval to wait before rechecking the accessibility tree for updates. |
block: AccessibilityNodeInfo.() -> Boolean |
a block that specifies a condition on the node to find. |
onElementOrNull
fun onElementOrNull(
timeoutMs: Long = 10000,
pollIntervalMs: Long = 100,
block: AccessibilityNodeInfo.() -> Boolean
): UiObject2?
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 | |
|---|---|
timeoutMs: Long = 10000 |
a timeout to find the element that satisfies the given condition. |
pollIntervalMs: Long = 100 |
an interval to wait before rechecking the accessibility tree for updates. |
block: AccessibilityNodeInfo.() -> Boolean |
a block that specifies a condition on the node to find. |
onElements
fun onElements(
timeoutMs: Long = 10000,
pollIntervalMs: Long = 100,
block: AccessibilityNodeInfo.() -> Boolean
): List<UiObject2>
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 | |
|---|---|
timeoutMs: Long = 10000 |
a timeout to find the element that satisfies the given condition. |
pollIntervalMs: Long = 100 |
an interval to wait before rechecking the accessibility tree for updates. |
block: AccessibilityNodeInfo.() -> Boolean |
a block that specifies a condition on the node to find. |
pressDelete
fun pressDelete(count: Int): Unit
Similar to type but presses the delete key for the given count times.
| Parameters | |
|---|---|
count: Int |
how many times the press delete key should be pressed. |
startActivity
fun startActivity(clazz: Class<*>, intentFlags: List<Int> = listOf()): Unit
Starts an activity with the given class. This method is blocking and awaits for a new window to be displayed.
| Parameters | |
|---|---|
clazz: Class<*> |
the class of the activity to start. |
intentFlags: List<Int> = listOf() |
a list of flags to add to the intent before launching it. |
| Returns | |
|---|---|
Unit |
whether an accessibility a new window was detected before the given timeout. |
startActivity
fun startActivity(
packageName: String,
activityName: String,
intentFlags: List<Int> = listOf()
): Unit
Starts an activity with the given packageName and activityName. This method is blocking and awaits for a new window to be displayed.
| Parameters | |
|---|---|
packageName: String |
the app package name of the activity to start. |
activityName: String |
the name of the activity to start. |
intentFlags: List<Int> = listOf() |
a list of flags to add to the intent before launching it. |
| Returns | |
|---|---|
Unit |
whether an accessibility a new window was detected before the given timeout. |
startActivityIntent
fun startActivityIntent(intent: Intent): Unit
Starts the given intent for an activity. This method is blocking and awaits for a new window to be displayed.
| Parameters | |
|---|---|
intent: Intent |
an intent to start |
| Returns | |
|---|---|
Unit |
whether an accessibility a new window was detected before the given timeout. |
startApp
fun startApp(packageName: String, intentFlags: List<Int> = listOf()): Unit
Starts the app with the given packageName. This method is blocking and awaits for a view with the given package name to be visible.
| Parameters | |
|---|---|
packageName: String |
the package name of the app to start |
intentFlags: List<Int> = listOf() |
a list of flags to add to the intent before launching it, see |
| Returns | |
|---|---|
Unit |
whether an accessibility node with the given package name was found before the given timeout. |
type
fun type(text: String): Unit
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 programmatically sets the given text in the target node.
| Parameters | |
|---|---|
text: String |
the text to type. |
unregisterWatchers
fun unregisterWatchers(): Unit
Unregisters all the watchers previously registered with watchFor.
waitForAppToBeVisible
fun waitForAppToBeVisible(appPackageName: String, timeoutMs: Long = 10000): Boolean
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 | |
|---|---|
appPackageName: String |
the package name of the app to wait for. |
timeoutMs: Long = 10000 |
a timeout for the app to become visible. |
| Returns | |
|---|---|
Boolean |
whether the app became visible in the given timeout. |
waitForStableInActiveWindow
fun waitForStableInActiveWindow(
stableTimeoutMs: Long = 3000,
stableIntervalMs: Long = 500,
stablePollIntervalMs: Long = 50,
requireStableScreenshot: Boolean = true
): StableResult
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 | |
|---|---|
stableTimeoutMs: Long = 3000 |
a timeout for the wait operation, to ensure not waiting forever for stability. |
stableIntervalMs: Long = 500 |
the interval during which the node should not be changing, in order to be considered stable. |
stablePollIntervalMs: Long = 50 |
specifies how often the ui should be checked for changes. |
requireStableScreenshot: Boolean = true |
specifies if also the bitmap of the node should not change over the specified |
| Returns | |
|---|---|
StableResult |
a |
watchFor
fun <T : Any?> watchFor(watcher: ScopedUiWatcher<T>, block: T.() -> Unit): WatcherRegistration
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 | |
|---|---|
watcher: ScopedUiWatcher<T> |
the dialog to watch for. |
block: T.() -> Unit |
a block to handle. |
| Returns | |
|---|---|
WatcherRegistration |
the dialog registration. |
windowRoots
fun windowRoots(): List<AccessibilityNodeInfo>
Returns all the window roots on all the displays.
windows
fun windows(): List<AccessibilityWindowInfo>
Returns all the windows on all the displays.
Protected functions
startIntentAndWait
protected open fun startIntentAndWait(intent: Intent): Unit
Default implementation of synchronous activity launch.
This isn't exposed to callers, just to subclasses for overriding to enable e.g. Macrobenchmark to verify synchronous launches more aggressively.