UiAutomatorTestScope


open class UiAutomatorTestScope


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
onView(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 block.

UiObject2?
onViewOrNull(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 block.

List<UiObject2>
onViews(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 block.

Boolean

Press the back key.

Unit
pressDelete(count: Int)

Similar to type but presses the delete key for the given count times.

Boolean

Press the enter key.

Boolean

Press the home key.

Unit
startActivity(clazz: Class<*>)

Starts an activity with the given class.

Unit
startActivity(packageName: String, activityName: String)

Starts an activity with the given packageName and activityName.

Unit

Starts the instrumentation test target app using the target app package name.

Unit
startApp(packageName: String)

Starts the app with the given packageName.

Unit

Starts the given intent.

Unit
type(text: String)

Types the given text string simulating key press through Instrumentation.sendKeySync.

Unit

Unregisters all the watchers previously registered with watchFor.

Boolean
waitForAppToBeVisible(appPackageName: String, timeoutMs: Long)

Waits for an application to become visible.

StableResult
waitForStableInActiveWindow(
    stableTimeoutMs: Long,
    stableIntervalMs: Long,
    stablePollIntervalMs: Long,
    requireStableScreenshot: Boolean
)

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 androidx.test.uiautomator.UiAutomatorTestScope to handle unexpected UI elements.

List<AccessibilityNodeInfo>

Returns all the window roots on all the displays.

List<AccessibilityWindowInfo>

Returns all the windows on all the displays.

Protected constructors

UiAutomatorTestScope

Added in 2.4.0-alpha02
protected UiAutomatorTestScope()

Public functions

activeWindowRoot

Added in 2.4.0-alpha02
fun activeWindowRoot(): AccessibilityNodeInfo

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

Added in 2.4.0-alpha02
fun clearAppData(): Unit

Clears the instrumentation test target app data.

onView

Added in 2.4.0-alpha02
fun onView(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:

onView { textAsString == "Search" }.click()
Parameters
timeoutMs: Long = 10000

a timeout to find the view 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.

Returns
UiObject2

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

onViewOrNull

Added in 2.4.0-alpha02
fun onViewOrNull(
    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:

onView { textAsString == "Search" }.click()
Parameters
timeoutMs: Long = 10000

a timeout to find the view 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.

Returns
UiObject2?

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

onViews

Added in 2.4.0-alpha02
fun onViews(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.onViews { isClass(Button::class.java) }
Parameters
timeoutMs: Long = 10000

a timeout to find the view 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.

Returns
List<UiObject2>

a list of UiObject2 from nodes that matches the given block condition.

pressBack

Added in 2.4.0-alpha02
fun pressBack(): Boolean

Press the back key.

pressDelete

Added in 2.4.0-alpha02
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.

pressEnter

Added in 2.4.0-alpha02
fun pressEnter(): Boolean

Press the enter key.

pressHome

Added in 2.4.0-alpha02
fun pressHome(): Boolean

Press the home key.

startActivity

Added in 2.4.0-alpha02
fun startActivity(clazz: Class<*>): Unit

Starts an activity with the given class.

Parameters
clazz: Class<*>

the class of the activity to start.

startActivity

Added in 2.4.0-alpha02
fun startActivity(packageName: String, activityName: String): Unit

Starts an activity with the given packageName and activityName.

Parameters
packageName: String

the app package name of the activity to start.

activityName: String

the name of the activity to start.

startApp

Added in 2.4.0-alpha02
fun startApp(): Unit

Starts the instrumentation test target app using the target app package name.

startApp

Added in 2.4.0-alpha02
fun startApp(packageName: String): Unit

Starts the app with the given packageName.

Parameters
packageName: String

the package name of the app to start

startIntent

Added in 2.4.0-alpha02
fun startIntent(intent: Intent): Unit

Starts the given intent.

Parameters
intent: Intent

an intent to start

type

Added in 2.4.0-alpha02
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 programmaticaly sets the given text in the target node.

Parameters
text: String

the text to type.

unregisterWatchers

Added in 2.4.0-alpha02
fun unregisterWatchers(): Unit

Unregisters all the watchers previously registered with watchFor.

waitForAppToBeVisible

Added in 2.4.0-alpha02
fun waitForAppToBeVisible(
    appPackageName: String = instrumentation.targetContext.packageName,
    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 = instrumentation.targetContext.packageName

the package name of the app to wait for. By default is the target app package name.

timeoutMs: Long = 10000

a timeout for the app to become visible.

Returns
Boolean

whether the app became visible in the given timeout.

waitForStableInActiveWindow

Added in 2.4.0-alpha02
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 stableIntervalMs. Note that this won't work with views that change constantly, like a video player.

Returns
StableResult

a StableResult containing the latest acquired view hierarchy and screenshot, and a flag indicating if the node was stable before timeout.

watchFor

Added in 2.4.0-alpha02
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 onView 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

Added in 2.4.0-alpha02
fun windowRoots(): List<AccessibilityNodeInfo>

Returns all the window roots on all the displays.

windows

Added in 2.4.0-alpha02
fun windows(): List<AccessibilityWindowInfo>

Returns all the windows on all the displays.

Public properties

device

Added in 2.4.0-alpha02
val deviceUiDevice

instrumentation

Added in 2.4.0-alpha02
val instrumentationInstrumentation

uiAutomation

Added in 2.4.0-alpha02
val uiAutomationUiAutomation