UiObject2
class UiObject2 : Searchable
Represents a UI element, and exposes methods for performing gestures (clicks, swipes) or searching through its children.
Unlike UiObject, UiObject2 is bound to a particular view instance and can become stale if the underlying view object is destroyed. As a result, it may be necessary to call findObject to obtain a new UiObject2 instance if the UI changes significantly.
Summary
Public functions |
|
|---|---|
Unit |
clear()Clears this object's text content if it is an editable field. |
Unit |
click()Clicks on this object's center. |
Unit |
Clicks on this object's center for |
Unit |
Clicks on a |
Unit |
Clicks on a |
U! |
<U> clickAndWait(condition: EventCondition<U!>, timeout: Long)Clicks on this object's center, and waits for a |
U! |
<U> clickAndWait(point: Point, condition: EventCondition<U!>, timeout: Long)Clicks on a |
Unit |
Drags this object to the specified point. |
Unit |
Drags this object to the specified point. |
Boolean |
|
UiObject2! |
findObject(selector: BySelector)Searches all elements under this object and returns the first one to match the |
(Mutable)List<UiObject2!> |
findObjects(selector: BySelector)Searches all elements under this object and returns those that match the |
Boolean |
Performs a fling gesture on this object. |
Boolean |
Performs a fling gesture on this object. |
AccessibilityNodeInfo |
Returns an up-to-date |
String! |
Returns the package name of the app that this object belongs to. |
Int |
Returns the number of child elements directly under this object. |
(Mutable)List<UiObject2!> |
Returns a collection of the child elements directly under this object. |
String! |
Returns the class name of this object's underlying |
String! |
Returns this object's content description. |
Int |
Returns the ID of the display containing this object. |
Int |
@RequiresApi(value = 24)Returns the drawing order (z-index) of this object relative to its siblings. |
String? |
@RequiresApi(value = 26)Returns the hint text of this object, or null if hint text is not preset. |
UiObject2! |
Returns this object's parent, or |
String! |
Returns the fully qualified resource name for this object's ID. |
String! |
getText()Returns this object's text content. |
Rect |
Returns this object's visible bounds. |
Point |
Returns a point in the center of this object's visible bounds. |
Boolean |
hasObject(selector: BySelector)Returns |
Int |
hashCode() |
Boolean |
Returns |
Boolean |
Returns |
Boolean |
Returns |
Boolean |
Returns |
Boolean |
Returns |
Boolean |
Returns |
Boolean |
Returns |
Boolean |
Returns |
Boolean |
Returns |
Unit |
Performs a long click on this object's center. |
Unit |
pinchClose(percent: Float)Performs a pinch close gesture on this object. |
Unit |
pinchClose(percent: Float, speed: Int)Performs a pinch close gesture on this object. |
Unit |
Performs a pinch open gesture on this object. |
Unit |
Performs a pinch open gesture on this object. |
Unit |
recycle()Recycle this object. |
Boolean |
Performs a scroll gesture on this object. |
Boolean |
Performs a scroll gesture on this object. |
U! |
<U> scrollUntil(direction: Direction, condition: Condition<UiObject2!, U!>)Perform scroll actions in certain direction until a |
U! |
<U> scrollUntil(direction: Direction, condition: EventCondition<U!>)Perform scroll actions in certain direction until a |
Unit |
setGestureMargin(margin: Int)Sets the margins used for gestures in pixels. |
Unit |
setGestureMarginPercentage(percent: @FloatRange(from = 0.0, to = 0.5) Float)Sets the percentage of gestures' margins to avoid touching too close to the edges, e.g. when scrolling up, phone open quick settings instead if gesture is close to the top. |
Unit |
setGestureMargins(left: Int, top: Int, right: Int, bottom: Int)Sets the margins used for gestures in pixels. |
Unit |
setGestureMarginsPercentage(Sets the percentage of gestures' margins to avoid touching too close to the edges, e.g. when scrolling up, phone open quick settings instead if gesture is close to the top. |
Unit |
Sets this object's text content if it is an editable field. |
Unit |
Performs a swipe gesture on this object. |
Unit |
Performs a swipe gesture on this object. |
U! |
Waits for a |
U! |
<U> wait(condition: SearchCondition<U!>, timeout: Long)Waits for a |
U! |
<U> wait(condition: UiObject2Condition<U!>, timeout: Long)Waits for a |
Extension functions |
|
|---|---|
UiObject2 |
UiObject2.onElement(Performs a DFS on the accessibility tree starting from the node associated to this |
UiObject2? |
UiObject2.onElementOrNull(Performs a DFS on the accessibility tree starting from the node associated to this |
List<UiObject2> |
UiObject2.onElements(Performs a DFS on the accessibility tree starting from the node associated to this |
UiObject2 |
UiObject2.scrollToElement(Keeps scrolling until the given |
UiObject2? |
UiObject2.scrollToElementOrNull(Keeps scrolling until the given |
Bitmap |
Takes a screenshot of the screen that contains the accessibility node associated to this |
StableResult |
UiObject2.waitForStable(Waits for the node to become stable. |
Public functions
clear
fun clear(): Unit
Clears this object's text content if it is an editable field.
click
fun click(duration: Long): Unit
Clicks on this object's center for duration milliseconds.
click
fun click(point: Point): Unit
Clicks on a point within this object's visible bounds.
| Parameters | |
|---|---|
point: Point |
The point to click (clipped to ensure it is within the visible bounds). |
click
fun click(point: Point, duration: Long): Unit
Clicks on a point within this object's visible bounds.
clickAndWait
fun <U> clickAndWait(condition: EventCondition<U!>, timeout: Long): U!
Clicks on this object's center, and waits for a condition to be met.
| Parameters | |
|---|---|
condition: EventCondition<U!> |
The |
timeout: Long |
The maximum time in milliseconds to wait for. |
clickAndWait
fun <U> clickAndWait(point: Point, condition: EventCondition<U!>, timeout: Long): U!
Clicks on a point within this object's visible bounds, and waits for a
condition to be met.
| Parameters | |
|---|---|
point: Point |
The point to click (clipped to ensure it is within the visible bounds). |
condition: EventCondition<U!> |
The |
timeout: Long |
The maximum time in milliseconds to wait for. |
drag
fun drag(dest: Point): Unit
Drags this object to the specified point.
| Parameters | |
|---|---|
dest: Point |
The end point to drag this object to. |
drag
fun drag(dest: Point, speed: Int): Unit
Drags this object to the specified point.
findObject
fun findObject(selector: BySelector): UiObject2!
Searches all elements under this object and returns the first one to match the
selector, or null if no matching objects are found.
findObjects
fun findObjects(selector: BySelector): (Mutable)List<UiObject2!>
Searches all elements under this object and returns those that match the selector.
fling
fun fling(direction: Direction): Boolean
Performs a fling gesture on this object.
| Parameters | |
|---|---|
direction: Direction |
The direction in which to fling. |
| Returns | |
|---|---|
Boolean |
|
fling
fun fling(direction: Direction, speed: Int): Boolean
Performs a fling gesture on this object.
| Parameters | |
|---|---|
direction: Direction |
The direction in which to fling. |
speed: Int |
The speed at which to perform this gesture in pixels per second. |
| Returns | |
|---|---|
Boolean |
|
getAccessibilityNodeInfo
fun getAccessibilityNodeInfo(): AccessibilityNodeInfo
Returns an up-to-date AccessibilityNodeInfo corresponding to this object's underlying View. Note that this method can be expensive as it wait for the device to be idle and tries multiple time to refresh the AccessibilityNodeInfo.
getApplicationPackage
fun getApplicationPackage(): String!
Returns the package name of the app that this object belongs to.
getChildCount
fun getChildCount(): Int
Returns the number of child elements directly under this object.
getChildren
fun getChildren(): (Mutable)List<UiObject2!>
Returns a collection of the child elements directly under this object.
getClassName
fun getClassName(): String!
Returns the class name of this object's underlying View.
getContentDescription
fun getContentDescription(): String!
Returns this object's content description.
| See also | |
|---|---|
getContentDescription |
getDisplayId
fun getDisplayId(): Int
Returns the ID of the display containing this object.
getDrawingOrder
@RequiresApi(value = 24)
fun getDrawingOrder(): Int
Returns the drawing order (z-index) of this object relative to its siblings. Higher values are drawn last (i.e. above their siblings).
In some cases, the drawing order is essentially simultaneous, so it is possible for two siblings to return the same value. It is also possible that values will be skipped.
| Returns | |
|---|---|
Int |
The drawing order of this object relative to its siblings. |
getHint
@RequiresApi(value = 26)
fun getHint(): String?
Returns the hint text of this object, or null if hint text is not preset.
Hint text is displayed when there's no user input text.
| See also | |
|---|---|
getHint |
getParent
fun getParent(): UiObject2!
Returns this object's parent, or null if it has no parent.
getResourceName
fun getResourceName(): String!
Returns the fully qualified resource name for this object's ID.
getVisibleCenter
fun getVisibleCenter(): Point
Returns a point in the center of this object's visible bounds.
hasObject
fun hasObject(selector: BySelector): Boolean
Returns true if there is a nested element which matches the selector.
isCheckable
fun isCheckable(): Boolean
Returns true if this object is checkable.
| See also | |
|---|---|
Checkable |
isChecked
fun isChecked(): Boolean
Returns true if this object is checked.
| See also | |
|---|---|
isChecked |
isClickable
fun isClickable(): Boolean
Returns true if this object is clickable.
| See also | |
|---|---|
isClickable |
isEnabled
fun isEnabled(): Boolean
Returns true if this object is enabled.
| See also | |
|---|---|
isEnabled |
isFocusable
fun isFocusable(): Boolean
Returns true if this object is focusable.
| See also | |
|---|---|
isFocusable |
isFocused
fun isFocused(): Boolean
Returns true if this object is focused.
| See also | |
|---|---|
isFocused |
isLongClickable
fun isLongClickable(): Boolean
Returns true if this object is long clickable.
| See also | |
|---|---|
isLongClickable |
isSelected
fun isSelected(): Boolean
Returns true if this object is selected.
| See also | |
|---|---|
isSelected |
pinchClose
fun pinchClose(percent: Float): Unit
Performs a pinch close gesture on this object.
| Parameters | |
|---|---|
percent: Float |
The size of the pinch as a percentage of this object's size. |
pinchClose
fun pinchClose(percent: Float, speed: Int): Unit
Performs a pinch close gesture on this object.
pinchOpen
fun pinchOpen(percent: Float): Unit
Performs a pinch open gesture on this object.
| Parameters | |
|---|---|
percent: Float |
The size of the pinch as a percentage of this object's size. |
pinchOpen
fun pinchOpen(percent: Float, speed: Int): Unit
Performs a pinch open gesture on this object.
scroll
fun scroll(direction: Direction, percent: Float): Boolean
Performs a scroll gesture on this object.
| Parameters | |
|---|---|
direction: Direction |
The direction in which to scroll. |
percent: Float |
The distance to scroll as a percentage of this object's visible size. |
| Returns | |
|---|---|
Boolean |
|
scroll
fun scroll(direction: Direction, percent: Float, speed: Int): Boolean
Performs a scroll gesture on this object.
| Parameters | |
|---|---|
direction: Direction |
The direction in which to scroll. |
percent: Float |
The distance to scroll as a percentage of this object's visible size. |
speed: Int |
The speed at which to perform this gesture in pixels per second. |
| Returns | |
|---|---|
Boolean |
|
scrollUntil
fun <U> scrollUntil(direction: Direction, condition: Condition<UiObject2!, U!>): U!
Perform scroll actions in certain direction until a condition is satisfied or scroll has finished, e.g. to scroll until an object contain certain text is found:
mScrollableUiObject2.scrollUntil(Direction.DOWN, Until.findObject(By.textContains ("sometext")));
| Parameters | |
|---|---|
direction: Direction |
The direction in which to scroll. |
condition: Condition<UiObject2!, U!> |
The |
| Returns | |
|---|---|
U! |
If the condition is satisfied. |
scrollUntil
fun <U> scrollUntil(direction: Direction, condition: EventCondition<U!>): U!
Perform scroll actions in certain direction until a condition is satisfied or scroll has finished, e.g. to scroll until a new window has appeared:
mScrollableUiObject2.scrollUntil(Direction.DOWN, Until.newWindow());
| Parameters | |
|---|---|
direction: Direction |
The direction in which to scroll. |
condition: EventCondition<U!> |
The |
| Returns | |
|---|---|
U! |
The value obtained after applying the condition. |
setGestureMargin
fun setGestureMargin(margin: Int): Unit
Sets the margins used for gestures in pixels.
setGestureMarginPercentage
fun setGestureMarginPercentage(percent: @FloatRange(from = 0.0, to = 0.5) Float): Unit
Sets the percentage of gestures' margins to avoid touching too close to the edges, e.g. when scrolling up, phone open quick settings instead if gesture is close to the top. The percentage is based on the object's visible size, e.g. to set 20% margins:
mUiObject2.setGestureMarginPercentage(0.2f);
| Parameters | |
|---|---|
percent: @FloatRange(from = 0.0, to = 0.5) Float |
Float between [0, 0.5] for four margins: left, top, right, and bottom. |
setGestureMargins
fun setGestureMargins(left: Int, top: Int, right: Int, bottom: Int): Unit
Sets the margins used for gestures in pixels.
setGestureMarginsPercentage
fun setGestureMarginsPercentage(
left: @FloatRange(from = 0.0, to = 1.0) Float,
top: @FloatRange(from = 0.0, to = 1.0) Float,
right: @FloatRange(from = 0.0, to = 1.0) Float,
bottom: @FloatRange(from = 0.0, to = 1.0) Float
): Unit
Sets the percentage of gestures' margins to avoid touching too close to the edges, e.g. when scrolling up, phone open quick settings instead if gesture is close to the top. The percentage is based on the object's visible size, e.g. to set 20% bottom margin only:
mUiObject2.setGestureMarginsPercentage(0f, 0f, 0f, 0.2f);
left Float between [0, 1] for left margin
top Float between [0, 1] for top margin
right Float between [0, 1] for right margin
bottom Float between [0, 1] for bottom margin
setText
fun setText(text: String?): Unit
Sets this object's text content if it is an editable field.
swipe
fun swipe(direction: Direction, percent: Float): Unit
Performs a swipe gesture on this object.
swipe
fun swipe(direction: Direction, percent: Float, speed: Int): Unit
Performs a swipe gesture on this object.
wait
fun <U> wait(condition: Condition<UiObject2!, U!>, timeout: Long): U!
Waits for a condition to be met.
| Parameters | |
|---|---|
condition: Condition<UiObject2!, U!> |
The |
timeout: Long |
The maximum time in milliseconds to wait for. |
| Returns | |
|---|---|
U! |
The final result returned by the |
wait
fun <U> wait(condition: SearchCondition<U!>, timeout: Long): U!
Waits for a condition to be met.
| Parameters | |
|---|---|
condition: SearchCondition<U!> |
The |
timeout: Long |
The maximum time in milliseconds to wait for. |
| Returns | |
|---|---|
U! |
The final result returned by the |
wait
fun <U> wait(condition: UiObject2Condition<U!>, timeout: Long): U!
Waits for a condition to be met.
| Parameters | |
|---|---|
condition: UiObject2Condition<U!> |
The |
timeout: Long |
The maximum time in milliseconds to wait for. |
| Returns | |
|---|---|
U! |
The final result returned by the |
Extension functions
UiObject2.onElement
fun UiObject2.onElement(
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 ElementNotFoundException is thrown. 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. |
UiObject2.onElementOrNull
fun UiObject2.onElementOrNull(
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:
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. |
UiObject2.onElements
fun UiObject2.onElements(
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.onElements { 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 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. |
UiObject2.scrollToElement
fun UiObject2.scrollToElement(
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 ElementNotFoundException if the condition is not satisfied at the end of the timeout.
Example:
onElement { isScrollable }.scrollToElement(Direction.DOWN) { id == "button" }.click()
| Parameters | |
|---|---|
direction: Direction |
the scroll |
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. |
UiObject2.scrollToElementOrNull
fun UiObject2.scrollToElementOrNull(
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:
onElement { isScrollable }.scrollToElement(Direction.DOWN) { id == "button" }.click()
| Parameters | |
|---|---|
direction: Direction |
the scroll |
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. |
UiObject2.takeScreenshot
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.
UiObject2.waitForStable
fun UiObject2.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 |
| Returns | |
|---|---|
StableResult |
a |