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

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

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 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<*>, intentFlags: List<Int>)

Starts an activity with the given class.

Unit
startActivity(
    packageName: String,
    activityName: String,
    intentFlags: List<Int>
)

Starts an activity with the given packageName and activityName.

Unit

Starts the given intent for an activity.

Unit
startApp(packageName: String, intentFlags: List<Int>)

Starts the app with the given packageName.

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 functions

open Unit

Default implementation of synchronous activity launch.

Protected constructors

UiAutomatorTestScope

Added in 2.4.0-alpha07
protected UiAutomatorTestScope()

Public functions

activeWindowRoot

Added in 2.4.0-alpha07
fun activeWindowRoot(): AccessibilityNodeInfo

Returns the active window root node.

clearAppData

Added in 2.4.0-alpha07
fun clearAppData(): Unit

Clears the instrumentation test target app data.

onElement

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

Returns
UiObject2

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

onElementOrNull

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

Returns
UiObject2?

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

onElements

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

Returns
List<UiObject2>

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

pressBack

Added in 2.4.0-alpha07
fun pressBack(): Boolean

Press the back key.

pressDelete

Added in 2.4.0-alpha07
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-alpha07
fun pressEnter(): Boolean

Press the enter key.

pressHome

Added in 2.4.0-alpha07
fun pressHome(): Boolean

Press the home key.

startActivity

Added in 2.4.0-alpha07
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

Added in 2.4.0-alpha07
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

Added in 2.4.0-alpha07
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

Added in 2.4.0-alpha07
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 Intent.addFlags.

Returns
Unit

whether an accessibility node with the given package name was found before the given timeout.

type

Added in 2.4.0-alpha07
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

Added in 2.4.0-alpha07
fun unregisterWatchers(): Unit

Unregisters all the watchers previously registered with watchFor.

waitForAppToBeVisible

Added in 2.4.0-alpha07
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

Added in 2.4.0-alpha07
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 elements that change constantly, like a video player.

Returns
StableResult

a StableResult containing the latest acquired element hierarchy and screenshot, and a flag indicating if the node was stable before timeout. The flag StableResult.isTimeout is set to false if the node was stable before the timeout expired, true otherwise.

watchFor

Added in 2.4.0-alpha07
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

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

Returns all the window roots on all the displays.

windows

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

Returns all the windows on all the displays.

Protected functions

startIntentAndWait

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

Public properties

device

Added in 2.4.0-alpha07
val deviceUiDevice

instrumentation

Added in 2.4.0-alpha07
val instrumentationInstrumentation

uiAutomation

Added in 2.4.0-alpha07
val uiAutomationUiAutomation