androidx.test.uiautomator


Interfaces

Condition

Represents a condition to be satisfied.

IAutomationSupport

Provides auxiliary support for running test cases

Searchable

The Searchable interface represents an object that can be searched for matching UI elements.

UiWatcher

See registerWatcher on how to register a a condition watcher to be called by the automation library.

Classes

By

By is a utility class which enables the creation of BySelectors in a concise manner.

BySelector

A BySelector specifies criteria for matching UI elements during a call to findObject.

Configurator

Allows you to set key parameters for running UiAutomator tests.

EventCondition

An EventCondition is a condition which depends on an event or series of events having occurred.

ResultsReporter

Allows to report test results to Android Studio.

SearchCondition

A SearchCondition is a condition that is satisfied by searching for UI elements.

StableResult

Represents a node that is considered stable and it's returned by androidx.test.uiautomator.waitForStable.

UiAutomatorInstrumentationTestRunner

This class is deprecated.

as it only handles deprecated UiAutomatorTestCases.

UiAutomatorTestCase

This class is deprecated.

It is no longer necessary to extend UiAutomatorTestCase.

UiAutomatorTestScope

A UiAutomator scope that allows to easily access UiAutomator api and utils class.

UiCollection

Used to enumerate a container's UI elements for the purpose of counting, or targeting a sub elements by a child's text or description.

UiDevice

UiDevice provides access to state information about the device.

UiObject

A UiObject is a representation of a view.

UiObject2

Represents a UI element, and exposes methods for performing gestures (clicks, swipes) or searching through its children.

UiObject2Condition

A UiObject2Condition is a condition which is satisfied when a UiObject2 is in a particular state.

UiScrollable

UiScrollable is a UiCollection and provides support for searching for items in scrollable layout elements.

UiSelector

Specifies the elements in the layout hierarchy for tests to target, filtered by properties such as text value, content-description, class name, and state information.

Until

The Until class provides factory methods for constructing common conditions.

Exceptions

StaleObjectException

A StaleObjectException exception is thrown when a UiObject2 is used after the underlying android.view.View has been destroyed.

UiObjectNotFoundException

Generated in test runs when a UiSelector selector could not be matched to any UI element displayed.

ViewNotFoundException

Thrown when a view is not found after invoking androidx.test.uiautomator.onView or androidx.test.uiautomator.onViews.

Enums

Direction

An enumeration used to specify the primary direction of certain gestures.

Top-level functions summary

Unit

Main entry point for ui automator tests.

Extension functions summary

AccessibilityWindowInfo

Returns the active window.

Rect

Returns the bounds of this node in screen coordinates.

List<AccessibilityNodeInfo>

Returns all the immediate children of this node.

List<AccessibilityNodeInfo>

Performs a DFS on the accessibility tree starting from this node and returns all the descendants discovered recursively.

Boolean

Returns whether this node class name is the same of the given class.

UiObject2
AccessibilityNodeInfo.onView(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from this node and returns the first node matching the given block.

UiObject2
AccessibilityWindowInfo.onView(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from the root node of this window and returns the first node matching the given block.

UiObject2
UiDevice.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
UiObject2.onView(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 and returns the first node matching the given block.

UiObject2?
AccessibilityNodeInfo.onViewOrNull(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from this node and returns the first node matching the given block.

UiObject2?
AccessibilityWindowInfo.onViewOrNull(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from the root node of this window and returns the first node matching the given block.

UiObject2?
UiDevice.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.

UiObject2?
UiObject2.onViewOrNull(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 and returns the first node matching the given block.

List<UiObject2>
AccessibilityNodeInfo.onViews(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from this node and returns all the nodes matching the given block.

List<UiObject2>
AccessibilityWindowInfo.onViews(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from the root node of this window and returns all the nodes matching the given block.

List<UiObject2>
UiDevice.onViews(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from this node and returns all the nodes matching the given block.

List<UiObject2>
UiObject2.onViews(
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 and returns all the nodes matching the given block.

Unit

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

Boolean
Bitmap.saveToFile(
    file: File,
    compressFormat: Bitmap.CompressFormat,
    quality: Int
)

Utility function to save a bitmap onto the given file.

Boolean
Bitmap.saveToFile(
    parcelFileDescriptor: ParcelFileDescriptor,
    compressFormat: Bitmap.CompressFormat,
    quality: Int
)

Utility function to save a bitmap onto the given parcelFileDescriptor.

UiObject2
UiObject2.scrollToView(
    direction: Direction,
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs.

UiObject2
UiObject2.scrollToViewOrNull(
    direction: Direction,
    timeoutMs: Long,
    pollIntervalMs: Long,
    block: AccessibilityNodeInfo.() -> Boolean
)

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs.

List<AccessibilityNodeInfo>

Returns all the siblings of the this node.

String?

Returns this node's view id resource name without the resource namespace, i.e. only the portion after the "/" character.

Bitmap

Takes a screenshot of the screen that contains this node and cuts only the area covered by it.

Bitmap

Takes a screenshot of the screen that contains this window and cuts only the area covered by it.

Bitmap

Takes a screenshot of the screen that contains the accessibility node associated to this UiObject2 and cuts only the area covered by it.

String?

Returns this node's text as a string.

Unit

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

Boolean
UiDevice.waitForAppToBeVisible(appPackageName: String, timeoutMs: Long)

Waits for an application to become visible.

AccessibilityNodeInfo
AccessibilityWindowInfo.waitForRoot(
    timeoutMs: Long,
    sleepIntervalMs: Long,
    clearCache: Boolean
)

Waits for the root node to become available in this window.

AccessibilityNodeInfo
UiDevice.waitForRootInActiveWindow(
    timeoutMs: Long,
    sleepIntervalMs: Long,
    clearCache: Boolean
)

Waits for the root node to become available in this window.

StableResult
AccessibilityNodeInfo.waitForStable(
    stableTimeoutMs: Long,
    stableIntervalMs: Long,
    stablePollIntervalMs: Long,
    requireStableScreenshot: Boolean
)

Waits for the node to become stable.

StableResult
AccessibilityWindowInfo.waitForStable(
    stableTimeoutMs: Long,
    stableIntervalMs: Long,
    stablePollIntervalMs: Long,
    requireStableScreenshot: Boolean
)

Waits for the root node of this window to become stable.

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

Waits for the root node of the active window to become stable.

List<AccessibilityWindowInfo>

Returns all the windows on all the displays.

Top-level functions

fun uiAutomator(block: UiAutomatorTestScope.() -> Unit): Unit

Main entry point for ui automator tests. It creates a UiAutomatorTestScope in which a test can be defined.

Example:

@Test
fun
myTest() = uiAutomator {

startActivity(MyActivity::class.java)

onView { id == "button" }.click()

onView { id == "nested_elements" }
.apply {
onView { text == "First Level" }
onView { text == "Second Level" }
onView { text == "Third Level" }
}
}
Parameters
block: UiAutomatorTestScope.() -> Unit

A block containing the test to run within the UiAutomatorTestScope.

Extension functions

fun UiDevice.activeWindow(): AccessibilityWindowInfo

Returns the active window.

fun AccessibilityNodeInfo.boundsInScreen(): Rect

Returns the bounds of this node in screen coordinates. This method is a shortcut for AccessibilityNodeInfo.getBoundsInScreen.

Returns
Rect

a Rect with the bounds of this node within the screen limits.

fun AccessibilityNodeInfo.children(
    block: (AccessibilityNodeInfo) -> Boolean = { true }
): List<AccessibilityNodeInfo>

Returns all the immediate children of this node. To return all the descendants (including children of children , children of children of children and so on use[ AccessibilityNodeInfo.descendants.

Parameters
block: (AccessibilityNodeInfo) -> Boolean = { true }

an optional predicate to filter the node children

Returns
List<AccessibilityNodeInfo>

the list of children of this node, with the given filter if specified.

fun AccessibilityNodeInfo.descendants(
    filterBlock: (AccessibilityNodeInfo) -> Boolean = { true }
): List<AccessibilityNodeInfo>

Performs a DFS on the accessibility tree starting from this node and returns all the descendants discovered recursively. Optionally a filter can be specified. Note that this differs from AccessibilityNodeInfo.children() because this will return also children of children, children of children of children and so on.

Parameters
filterBlock: (AccessibilityNodeInfo) -> Boolean = { true }

an optional predicate to filter the node children

Returns
List<AccessibilityNodeInfo>

the list of all the descendants of this node, with the given filter if specified.

fun AccessibilityNodeInfo.isClass(cls: Class<*>): Boolean

Returns whether this node class name is the same of the given class.

Usage:

onView { isClass(Button::class.java) }.click()
fun AccessibilityNodeInfo.onView(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2

Performs a DFS on the accessibility tree starting from this node 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 androidx.test.uiautomator.ViewNotFoundException is thrown. Internally it works searching periodically every pollIntervalMs.

Example:

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

fun AccessibilityWindowInfo.onView(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2

Performs a DFS on the accessibility tree starting from the root node of this 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 ViewNotFoundException is thrown.

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.

fun UiDevice.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. If the requested node doesn't exist, a ViewNotFoundException is thrown. Internally it works searching periodically every pollIntervalMs.

Example:

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

fun UiObject2.onView(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 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 ViewNotFoundException is thrown. 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.

fun AccessibilityNodeInfo.onViewOrNull(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2?

Performs a DFS on the accessibility tree starting from this node 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:

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

fun AccessibilityWindowInfo.onViewOrNull(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2?

Performs a DFS on the accessibility tree starting from the root node of this 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:

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.

fun UiDevice.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. If the requested node doesn't exist, null is returned. Internally it works searching periodically every pollIntervalMs.

Example:

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

fun UiObject2.onViewOrNull(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2?

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 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:

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.

fun AccessibilityNodeInfo.onViews(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): List<UiObject2>

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.onViews { 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
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.

fun AccessibilityWindowInfo.onViews(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): List<UiObject2>

Performs a DFS on the accessibility tree starting from the root node of this window 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.onViews { 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
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.

fun UiDevice.onViews(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): List<UiObject2>

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.onViews { 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
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.

fun UiObject2.onViews(
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): List<UiObject2>

Performs a DFS on the accessibility tree starting from the node associated to this UiObject2 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.onViews { isClass(Button::class.java) }

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

fun UiDevice.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.

fun Bitmap.saveToFile(
    file: File,
    compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
    quality: Int = 100
): Boolean

Utility function to save a bitmap onto the given file.

Parameters
file: File

the file to store the bitmap.

compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG

the Bitmap.CompressFormat to use when storing the bitmap. Default is jpg.

quality: Int = 100

the quality for the compression. This is always 100 for png.

Returns
Boolean

whether the bitmap was correctly saved onto a file.

fun Bitmap.saveToFile(
    parcelFileDescriptor: ParcelFileDescriptor,
    compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG,
    quality: Int = 100
): Boolean

Utility function to save a bitmap onto the given parcelFileDescriptor.

Parameters
parcelFileDescriptor: ParcelFileDescriptor

the file descriptor pointing to a file to store the bitmap.

compressFormat: Bitmap.CompressFormat = Bitmap.CompressFormat.JPEG

the Bitmap.CompressFormat to use when storing the bitmap. Default is jpg.

quality: Int = 100

the quality for the compression. This is always 100 for png.

Returns
Boolean

whether the bitmap was correctly saved onto a file.

fun UiObject2.scrollToView(
    direction: Direction,
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs. Throws a ViewNotFoundException if the condition is not satisfied at the end of the timeout.

Example:

onView { isScrollable }.scrollToView(Direction.DOWN) { id == "button" }.click()
Parameters
direction: Direction

the scroll Direction.

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 that matches the given block condition.

scrollToViewOrNull

fun UiObject2.scrollToViewOrNull(
    direction: Direction,
    timeoutMs: Long = 10000,
    pollIntervalMs: Long = 100,
    block: AccessibilityNodeInfo.() -> Boolean
): UiObject2

Keeps scrolling until the given block condition is satisfied or until the given timeoutMs. Returns null if the condition is not satisfied at the end of the timeout.

Example:

onView { isScrollable }.scrollToView(Direction.DOWN) { id == "button" }.click()
Parameters
direction: Direction

the scroll Direction.

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 that matches the given block condition.

fun AccessibilityNodeInfo.siblings(
    filterBlock: (AccessibilityNodeInfo) -> Boolean = { true }
): List<AccessibilityNodeInfo>

Returns all the siblings of the this node.

Parameters
filterBlock: (AccessibilityNodeInfo) -> Boolean = { true }

an optional predicate to filter the node siblings

Returns
List<AccessibilityNodeInfo>

the list of the siblings of this node, with the given filter if specified.

simpleViewResourceName

fun AccessibilityNodeInfo.simpleViewResourceName(): String?

Returns this node's view id resource name without the resource namespace, i.e. only the portion after the "/" character. If the view id is not specified, then it returns null.

fun AccessibilityNodeInfo.takeScreenshot(): Bitmap

Takes a screenshot of the screen that contains this node and cuts only the area covered by it.

Returns
Bitmap

a bitmap containing the image of this node.

fun AccessibilityWindowInfo.takeScreenshot(): Bitmap

Takes a screenshot of the screen that contains this window and cuts only the area covered by it.

Returns
Bitmap

a bitmap containing the image of this node.

fun UiObject2.takeScreenshot(): Bitmap

Takes a screenshot of the screen that contains the accessibility node associated to this UiObject2 and cuts only the area covered by it.

Returns
Bitmap

a bitmap containing the image of the node associated to this UiObject2.

fun AccessibilityNodeInfo.textAsString(): String?

Returns this node's text as a string. This should always be preferred to AccessibilityNodeInfo.text that instead returns a CharSequence, that might be either a String or a android.text.SpannableString. If a text is not specified, then it returns null.

Usage:

onView { textAsString() == "Some text" }.click()
fun UiDevice.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.

waitForAppToBeVisible

fun UiDevice.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. 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.

fun AccessibilityWindowInfo.waitForRoot(
    timeoutMs: Long = 10000,
    sleepIntervalMs: Long = 100,
    clearCache: Boolean = true
): AccessibilityNodeInfo

Waits for the root node to become available in this window.

Parameters
timeoutMs: Long = 10000

a timeout for the root node to become available.

sleepIntervalMs: Long = 100

a interval to wait before retrying checking if the node is available.

clearCache: Boolean = true

whether the accessibility nodes cache should be cleared when checking.

Returns
AccessibilityNodeInfo

the root node for this window.

waitForRootInActiveWindow

fun UiDevice.waitForRootInActiveWindow(
    timeoutMs: Long = 10000,
    sleepIntervalMs: Long = 100,
    clearCache: Boolean = true
): AccessibilityNodeInfo

Waits for the root node to become available in this window.

Parameters
timeoutMs: Long = 10000

a timeout for the root node to become available.

sleepIntervalMs: Long = 100

a interval to wait before retrying checking if the node is available.

clearCache: Boolean = true

whether the accessibility nodes cache should be cleared when checking.

Returns
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 timeoutMs.

fun AccessibilityNodeInfo.waitForStable(
    stableTimeoutMs: Long = 3000,
    stableIntervalMs: Long = 500,
    stablePollIntervalMs: Long = 50,
    requireStableScreenshot: Boolean = true
): StableResult

Waits for the node 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 androidx.test.uiautomator.StableResult containing the latest acquired view hierarchy and screenshot, and a flag indicating if the node was stable before timeout.

fun AccessibilityWindowInfo.waitForStable(
    stableTimeoutMs: Long = 3000,
    stableIntervalMs: Long = 500,
    stablePollIntervalMs: Long = 50,
    requireStableScreenshot: Boolean = true
): StableResult

Waits for the root node of this 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.

waitForStableInActiveWindow

fun UiDevice.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.

fun UiDevice.windows(): List<AccessibilityWindowInfo>

Returns all the windows on all the displays.