# UiObject2

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2.java+class:androidx.test.uiautomator.UiObject2) Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

*** ** * ** ***


```
public class UiObject2 implements Searchable
```

<br />

*** ** * ** ***

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

Unlike `https://developer.android.com/reference/androidx/test/uiautomator/UiObject`, `https://developer.android.com/reference/androidx/test/uiautomator/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 `https://developer.android.com/reference/androidx/test/uiautomator/UiDevice#findObject(androidx.test.uiautomator.BySelector)` to obtain a new `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` instance if the UI changes significantly.

## Summary

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#clear()()` Clears this object's text content if it is an editable field. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#click()()` Clicks on this object's center. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#click(long)(long duration)` Clicks on this object's center for `duration` milliseconds. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#click(android.graphics.Point)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html point)` Clicks on a `point` within this object's visible bounds. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#click(android.graphics.Point,long)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html point, long duration)` Clicks on a `point` within this object's visible bounds. |
| `U` | `<U> https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#clickAndWait(androidx.test.uiautomator.EventCondition<U>,long)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/EventCondition<U> condition, long timeout)` Clicks on this object's center, and waits for a `condition` to be met. |
| `U` | `<U> https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#clickAndWait(android.graphics.Point,androidx.test.uiautomator.EventCondition<U>,long)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html point, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/EventCondition<U> condition, long timeout )` Clicks on a `point` within this object's visible bounds, and waits for a ` condition` to be met. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#drag(android.graphics.Point)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html dest)` Drags this object to the specified point. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#drag(android.graphics.Point,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html dest, int speed)` Drags this object to the specified point. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object.html object)` |
| `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#findObject(androidx.test.uiautomator.BySelector)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/BySelector selector)` Searches all elements under this object and returns the first one to match the ` selector`, or `null` if no matching objects are found. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/test/uiautomator/UiObject2>` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#findObjects(androidx.test.uiautomator.BySelector)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/BySelector selector)` Searches all elements under this object and returns those that match the `selector`. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#fling(androidx.test.uiautomator.Direction)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction)` Performs a fling gesture on this object. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#fling(androidx.test.uiautomator.Direction,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, int speed)` Performs a fling gesture on this object. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getAccessibilityNodeInfo()()` Returns an up-to-date `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html` corresponding to this object's underlying `https://developer.android.com/reference/android/view/View.html`. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getApplicationPackage()()` Returns the package name of the app that this object belongs to. |
| `int` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getChildCount()()` Returns the number of child elements directly under this object. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/test/uiautomator/UiObject2>` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getChildren()()` Returns a collection of the child elements directly under this object. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getClassName()()` Returns the class name of this object's underlying `https://developer.android.com/reference/android/view/View.html`. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getContentDescription()()` Returns this object's content description. |
| `int` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getDisplayId()()` Returns the ID of the display containing this object. |
| `int` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 24) https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getDrawingOrder()()` Returns the drawing order (z-index) of this object relative to its siblings. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 26) https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getHint()()` Returns the hint text of this object, or null if hint text is not preset. |
| `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getParent()()` Returns this object's parent, or `null` if it has no parent. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getResourceName()()` Returns the fully qualified resource name for this object's ID. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getText()()` Returns this object's text content. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getVisibleBounds()()` Returns this object's visible bounds. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#getVisibleCenter()()` Returns a point in the center of this object's visible bounds. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#hasObject(androidx.test.uiautomator.BySelector)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/BySelector selector)` Returns `true` if there is a nested element which matches the `selector`. |
| `int` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#hashCode()()` |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isCheckable()()` Returns `true` if this object is checkable. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isChecked()()` Returns `true` if this object is checked. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isClickable()()` Returns `true` if this object is clickable. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isEnabled()()` Returns `true` if this object is enabled. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isFocusable()()` Returns `true` if this object is focusable. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isFocused()()` Returns `true` if this object is focused. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isLongClickable()()` Returns `true` if this object is long clickable. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isScrollable()()` Returns `true` if this object is scrollable. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#isSelected()()` Returns `true` if this object is selected. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#longClick()()` Performs a long click on this object's center. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#pinchClose(float)(float percent)` Performs a pinch close gesture on this object. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#pinchClose(float,int)(float percent, int speed)` Performs a pinch close gesture on this object. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#pinchOpen(float)(float percent)` Performs a pinch open gesture on this object. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#pinchOpen(float,int)(float percent, int speed)` Performs a pinch open gesture on this object. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#recycle()()` Recycle this object. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#scroll(androidx.test.uiautomator.Direction,float)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, float percent)` Performs a scroll gesture on this object. |
| `boolean` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#scroll(androidx.test.uiautomator.Direction,float,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, float percent, int speed)` Performs a scroll gesture on this object. |
| `U` | `<U> https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#scrollUntil(androidx.test.uiautomator.Direction,androidx.test.uiautomator.Condition<? super androidx.test.uiautomator.UiObject2,U>)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Condition<https://developer.android.com/reference/androidx/test/uiautomator/UiObject2, U> condition )` 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: |
| `U` | `<U> https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#scrollUntil(androidx.test.uiautomator.Direction,androidx.test.uiautomator.EventCondition<U>)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/EventCondition<U> condition )` 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: |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#setGestureMargin(int)(int margin)` Sets the margins used for gestures in pixels. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#setGestureMarginPercentage(float)(@https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, to = 0.5) float percent)` 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. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#setGestureMargins(int,int,int,int)(int left, int top, int right, int bottom)` Sets the margins used for gestures in pixels. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#setGestureMarginsPercentage(float,float,float,float)( @https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, to = 1.0) float left, @https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, to = 1.0) float top, @https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, to = 1.0) float right, @https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, to = 1.0) float bottom )` 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. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#setText(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html text)` Sets this object's text content if it is an editable field. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#swipe(androidx.test.uiautomator.Direction,float)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, float percent)` Performs a swipe gesture on this object. |
| `void` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#swipe(androidx.test.uiautomator.Direction,float,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, float percent, int speed)` Performs a swipe gesture on this object. |
| `U` | `<U> https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#wait(androidx.test.uiautomator.Condition<? super androidx.test.uiautomator.UiObject2,U>,long)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Condition<https://developer.android.com/reference/androidx/test/uiautomator/UiObject2, U> condition, long timeout)` Waits for a `condition` to be met. |
| `U` | `<U> https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#wait(androidx.test.uiautomator.SearchCondition<U>,long)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/SearchCondition<U> condition, long timeout)` Waits for a `condition` to be met. |
| `U` | `<U> https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#wait(androidx.test.uiautomator.UiObject2Condition<U>,long)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Condition<U> condition, long timeout)` Waits for a `condition` to be met. |

| ### Extension functions |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Ext.https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#(androidx.test.uiautomator.UiObject2).onElement(kotlin.Long,kotlin.Long,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2 receiver, long timeoutMs, long pollIntervalMs, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block )` Performs a DFS on the accessibility tree starting from the node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and returns the first node matching the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElement(kotlin.Long,kotlin.Long,kotlin.Function1)`. |
| `final https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Ext.https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#(androidx.test.uiautomator.UiObject2).onElementOrNull(kotlin.Long,kotlin.Long,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2 receiver, long timeoutMs, long pollIntervalMs, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block )` Performs a DFS on the accessibility tree starting from the node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and returns the first node matching the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElementOrNull(kotlin.Long,kotlin.Long,kotlin.Function1)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2>` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Ext.https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#(androidx.test.uiautomator.UiObject2).onElements(kotlin.Long,kotlin.Long,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2 receiver, long timeoutMs, long pollIntervalMs, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block )` Performs a DFS on the accessibility tree starting from the node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and returns all the nodes matching the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElements(kotlin.Long,kotlin.Long,kotlin.Function1)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Ext.https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#(androidx.test.uiautomator.UiObject2).scrollToElement(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2 receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, long timeoutMs, long pollIntervalMs, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block )` Keeps scrolling until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElement(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)` condition is satisfied or until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElement(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)`. |
| `final https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Ext.https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#(androidx.test.uiautomator.UiObject2).scrollToElementOrNull(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2 receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/Direction direction, long timeoutMs, long pollIntervalMs, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block )` Keeps scrolling until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElementOrNull(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)` condition is satisfied or until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElementOrNull(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Bitmap.html` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Ext.https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#(androidx.test.uiautomator.UiObject2).takeScreenshot()(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2 receiver)` Takes a screenshot of the screen that contains the accessibility node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and cuts only the area covered by it. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/StableResult` | `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Ext.https://developer.android.com/reference/androidx/test/uiautomator/UiObject2#(androidx.test.uiautomator.UiObject2).waitForStable(kotlin.Long,kotlin.Long,kotlin.Long,kotlin.Boolean)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2 receiver, long stableTimeoutMs, long stableIntervalMs, long stablePollIntervalMs, boolean requireStableScreenshot )` Waits for the node to become stable. |

## Public methods

### clear

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void clear()
```

Clears this object's text content if it is an editable field.

### click

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void click()
```

Clicks on this object's center.

### click

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void click(long duration)
```

Clicks on this object's center for `duration` milliseconds.

### click

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public void click(@NonNull Point point)
```

Clicks on a `point` within this object's visible bounds.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html point` | The point to click (clipped to ensure it is within the visible bounds). |

### click

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public void click(@NonNull Point point, long duration)
```

Clicks on a `point` within this object's visible bounds.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html point` | The point to click (clipped to ensure it is within the visible bounds). |
| `long duration` | The click duration in milliseconds. |

### clickAndWait

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public U <U> clickAndWait(@NonNull EventCondition<U> condition, long timeout)
```

Clicks on this object's center, and waits for a `condition` to be met.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/EventCondition<U> condition` | The `https://developer.android.com/reference/androidx/test/uiautomator/EventCondition` to wait for. |
| `long timeout` | The maximum time in milliseconds to wait for. |

### clickAndWait

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public U <U> clickAndWait(
    @NonNull Point point,
    @NonNull EventCondition<U> condition,
    long timeout
)
```

Clicks on a `point` within this object's visible bounds, and waits for a `
condition` to be met.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html point` | The point to click (clipped to ensure it is within the visible bounds). |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/EventCondition<U> condition` | The `https://developer.android.com/reference/androidx/test/uiautomator/EventCondition` to wait for. |
| `long timeout` | The maximum time in milliseconds to wait for. |

### drag

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void drag(@NonNull Point dest)
```

Drags this object to the specified point.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html dest` | The end point to drag this object to. |

### drag

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void drag(@NonNull Point dest, int speed)
```

Drags this object to the specified point.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Point.html dest` | The end point to drag this object to. |
| `int speed` | The speed at which to perform this gesture in pixels per second. |

### equals

```
public boolean equals(Object object)
```

### findObject

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public UiObject2 findObject(@NonNull BySelector selector)
```

Searches all elements under this object and returns the first one to match the `
selector`, or `null` if no matching objects are found.

### findObjects

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public @NonNull List<UiObject2> findObjects(@NonNull BySelector selector)
```

Searches all elements under this object and returns those that match the `selector`.

### fling

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean fling(@NonNull Direction direction)
```

Performs a fling gesture on this object.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to fling. |

| Returns |
|---|---|
| `boolean` | `true` if the object can still scroll in the given direction. |

### fling

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean fling(@NonNull Direction direction, int speed)
```

Performs a fling gesture on this object.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to fling. |
| `int speed` | The speed at which to perform this gesture in pixels per second. |

| Returns |
|---|---|
| `boolean` | `true` if the object can still scroll in the given direction. |

### getAccessibilityNodeInfo

Added in [2.4.1](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.4.1)

```
public @NonNull AccessibilityNodeInfo getAccessibilityNodeInfo()
```

Returns an up-to-date `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html` corresponding to this object's underlying `https://developer.android.com/reference/android/view/View.html`. Note that this method can be expensive as it wait for the device to be idle and tries multiple time to refresh the `https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html`.

### getApplicationPackage

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public String getApplicationPackage()
```

Returns the package name of the app that this object belongs to.

### getChildCount

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public int getChildCount()
```

Returns the number of child elements directly under this object.

### getChildren

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public @NonNull List<UiObject2> getChildren()
```

Returns a collection of the child elements directly under this object.

### getClassName

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public String getClassName()
```

Returns the class name of this object's underlying `https://developer.android.com/reference/android/view/View.html`.

### getContentDescription

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public String getContentDescription()
```

Returns this object's content description.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/View.html#getContentDescription()` |   |

### getDisplayId

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public int getDisplayId()
```

Returns the ID of the display containing this object.

### getDrawingOrder

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
@RequiresApi(value = 24)
public int getDrawingOrder()
```

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

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
@RequiresApi(value = 26)
public @Nullable String getHint()
```

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 |
|---|---|
| `https://developer.android.com/reference/android/widget/TextView.html#getHint()` |   |

### getParent

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public UiObject2 getParent()
```

Returns this object's parent, or `null` if it has no parent.

### getResourceName

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public String getResourceName()
```

Returns the fully qualified resource name for this object's ID.

### getText

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public String getText()
```

Returns this object's text content.

| See also |
|---|---|
| `https://developer.android.com/reference/android/widget/TextView.html#getText()` |   |

### getVisibleBounds

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public @NonNull Rect getVisibleBounds()
```

Returns this object's visible bounds.

### getVisibleCenter

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public @NonNull Point getVisibleCenter()
```

Returns a point in the center of this object's visible bounds.

### hasObject

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean hasObject(@NonNull BySelector selector)
```

Returns `true` if there is a nested element which matches the `selector`.

### hashCode

```
public int hashCode()
```

### isCheckable

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isCheckable()
```

Returns `true` if this object is checkable.

| See also |
|---|---|
| `https://developer.android.com/reference/android/widget/Checkable.html` |   |

### isChecked

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isChecked()
```

Returns `true` if this object is checked.

| See also |
|---|---|
| `https://developer.android.com/reference/android/widget/Checkable.html#isChecked()` |   |

### isClickable

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isClickable()
```

Returns `true` if this object is clickable.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/View.html#isClickable()` |   |

### isEnabled

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isEnabled()
```

Returns `true` if this object is enabled.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/View.html#isEnabled()` |   |

### isFocusable

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isFocusable()
```

Returns `true` if this object is focusable.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/View.html#isFocusable()` |   |

### isFocused

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isFocused()
```

Returns `true` if this object is focused.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/View.html#isFocused()` |   |

### isLongClickable

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isLongClickable()
```

Returns `true` if this object is long clickable.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/View.html#isLongClickable()` |   |

### isScrollable

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isScrollable()
```

Returns `true` if this object is scrollable.

### isSelected

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean isSelected()
```

Returns `true` if this object is selected.

| See also |
|---|---|
| `https://developer.android.com/reference/android/view/View.html#isSelected()` |   |

### longClick

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void longClick()
```

Performs a long click on this object's center.

### pinchClose

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void pinchClose(float percent)
```

Performs a pinch close gesture on this object.

| Parameters |
|---|---|
| `float percent` | The size of the pinch as a percentage of this object's size. |

### pinchClose

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void pinchClose(float percent, int speed)
```

Performs a pinch close gesture on this object.

| Parameters |
|---|---|
| `float percent` | The size of the pinch as a percentage of this object's size. |
| `int speed` | The speed at which to perform this gesture in pixels per second. |

### pinchOpen

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void pinchOpen(float percent)
```

Performs a pinch open gesture on this object.

| Parameters |
|---|---|
| `float percent` | The size of the pinch as a percentage of this object's size. |

### pinchOpen

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void pinchOpen(float percent, int speed)
```

Performs a pinch open gesture on this object.

| Parameters |
|---|---|
| `float percent` | The size of the pinch as a percentage of this object's size. |
| `int speed` | The speed at which to perform this gesture in pixels per second. |

### recycle

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void recycle()
```

Recycle this object.

### scroll

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean scroll(@NonNull Direction direction, float percent)
```

Performs a scroll gesture on this object.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to scroll. |
| `float percent` | The distance to scroll as a percentage of this object's visible size. |

| Returns |
|---|---|
| `boolean` | `true` if the object can still scroll in the given direction. |

### scroll

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public boolean scroll(@NonNull Direction direction, float percent, int speed)
```

Performs a scroll gesture on this object.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to scroll. |
| `float percent` | The distance to scroll as a percentage of this object's visible size. |
| `int speed` | The speed at which to perform this gesture in pixels per second. |

| Returns |
|---|---|
| `boolean` | `true` if the object can still scroll in the given direction. |

### scrollUntil

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public U <U> scrollUntil(
    @NonNull Direction direction,
    @NonNull Condition<UiObject2, U> condition
)
```

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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to scroll. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Condition<https://developer.android.com/reference/androidx/test/uiautomator/UiObject2, U> condition` | The `https://developer.android.com/reference/androidx/test/uiautomator/Condition` to evaluate. |

| Returns |
|---|---|
| `U` | If the condition is satisfied. |

### scrollUntil

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public U <U> scrollUntil(
    @NonNull Direction direction,
    @NonNull EventCondition<U> condition
)
```

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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to scroll. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/EventCondition<U> condition` | The `https://developer.android.com/reference/androidx/test/uiautomator/EventCondition` to wait for. |

| Returns |
|---|---|
| `U` | The value obtained after applying the condition. |

### setGestureMargin

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void setGestureMargin(int margin)
```

Sets the margins used for gestures in pixels.

### setGestureMarginPercentage

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public void setGestureMarginPercentage(@FloatRange(from = 0.0, to = 0.5) float percent)
```

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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/FloatRange(from = 0.0, to = 0.5) float percent` | Float between \[0, 0.5\] for four margins: left, top, right, and bottom. |

### setGestureMargins

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void setGestureMargins(int left, int top, int right, int bottom)
```

Sets the margins used for gestures in pixels.

### setGestureMarginsPercentage

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public void setGestureMarginsPercentage(
    @FloatRange(from = 0.0, to = 1.0) float 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
)
```

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

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void setText(@Nullable String text)
```

Sets this object's text content if it is an editable field.

### swipe

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void swipe(@NonNull Direction direction, float percent)
```

Performs a swipe gesture on this object.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to swipe. |
| `float percent` | The length of the swipe as a percentage of this object's size. |

### swipe

Added in [2.2.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.2.0)

```
public void swipe(@NonNull Direction direction, float percent, int speed)
```

Performs a swipe gesture on this object.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | The direction in which to swipe. |
| `float percent` | The length of the swipe as a percentage of this object's size. |
| `int speed` | The speed at which to perform this gesture in pixels per second. |

### wait

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public U <U> wait(@NonNull Condition<UiObject2, U> condition, long timeout)
```

Waits for a `condition` to be met.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/Condition<https://developer.android.com/reference/androidx/test/uiautomator/UiObject2, U> condition` | The `https://developer.android.com/reference/androidx/test/uiautomator/Condition` to evaluate. |
| `long timeout` | The maximum time in milliseconds to wait for. |

| Returns |
|---|---|
| `U` | The final result returned by the `condition`, or `null` if the ` condition` was not met before the `timeout`. |

### wait

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public U <U> wait(@NonNull SearchCondition<U> condition, long timeout)
```

Waits for a `condition` to be met.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/SearchCondition<U> condition` | The `https://developer.android.com/reference/androidx/test/uiautomator/SearchCondition` to evaluate. |
| `long timeout` | The maximum time in milliseconds to wait for. |

| Returns |
|---|---|
| `U` | The final result returned by the `condition`, or `null` if the ` condition` was not met before the `timeout`. |

### wait

Added in [2.3.0](https://developer.android.com/jetpack/androidx/releases/test-uiautomator#2.3.0)

```
public U <U> wait(@NonNull UiObject2Condition<U> condition, long timeout)
```

Waits for a `condition` to be met.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Condition<U> condition` | The `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2Condition` to wait for. |
| `long timeout` | The maximum time in milliseconds to wait for. |

| Returns |
|---|---|
| `U` | The final result returned by the `condition`, or `null` if the ` condition` was not met before the `timeout`. |

## Extension functions

### UiObject2Ext.onElement

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2Ext.kt+function:onElement)

```
public final @NonNull UiObject2 UiObject2Ext.onElement(
    @NonNull UiObject2 receiver,
    long timeoutMs,
    long pollIntervalMs,
    @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
```

Performs a DFS on the accessibility tree starting from the node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and returns the first node matching the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElement(kotlin.Long,kotlin.Long,kotlin.Function1)`. The node is returned as an `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` that allows interacting with it. If the requested node doesn't exist, a `https://developer.android.com/reference/androidx/test/uiautomator/ElementNotFoundException` is thrown. Internally it works searching periodically every `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElement(kotlin.Long,kotlin.Long,kotlin.Function1)`.

Example:

```
onElement { textAsString == "Search" }.click()
```

| Parameters |
|---|---|
| `long timeoutMs` | a timeout to find the element that satisfies the given condition. |
| `long pollIntervalMs` | an interval to wait before rechecking the accessibility tree for updates. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block` | a block that specifies a condition on the node to find. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | a `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` from a node that matches the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElement(kotlin.Long,kotlin.Long,kotlin.Function1)` condition. |

### UiObject2Ext.onElementOrNull

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2Ext.kt+function:onElementOrNull)

```
public final UiObject2 UiObject2Ext.onElementOrNull(
    @NonNull UiObject2 receiver,
    long timeoutMs,
    long pollIntervalMs,
    @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
```

Performs a DFS on the accessibility tree starting from the node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and returns the first node matching the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElementOrNull(kotlin.Long,kotlin.Long,kotlin.Function1)`. The node is returned as an `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` that allows interacting with it. If the requested node doesn't exist, null is returned. Internally it works searching periodically every `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElementOrNull(kotlin.Long,kotlin.Long,kotlin.Function1)`.

Example:

```
onElement { textAsString == "Search" }.click()
```

| Parameters |
|---|---|
| `long timeoutMs` | a timeout to find the element that satisfies the given condition. |
| `long pollIntervalMs` | an interval to wait before rechecking the accessibility tree for updates. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block` | a block that specifies a condition on the node to find. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | a `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` from a node that matches the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElementOrNull(kotlin.Long,kotlin.Long,kotlin.Function1)` condition or null. |

### UiObject2Ext.onElements

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2Ext.kt+function:onElements)

```
public final @NonNull List<@NonNull UiObject2> UiObject2Ext.onElements(
    @NonNull UiObject2 receiver,
    long timeoutMs,
    long pollIntervalMs,
    @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
```

Performs a DFS on the accessibility tree starting from the node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and returns all the nodes matching the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElements(kotlin.Long,kotlin.Long,kotlin.Function1)`. This method stops waiting as soon as a single node with the given condition is returned. The nodes returned are `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` that allow interacting with them. Internally it works searching periodically every `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElements(kotlin.Long,kotlin.Long,kotlin.Function1)`.

Example:

```
node.onElements { isClass(Button::class.java) }
```

If multiple nodes are expected but they appear at different times, it's recommended to call `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(android.view.accessibility.AccessibilityNodeInfo).waitForStable(kotlin.Long,kotlin.Long,kotlin.Long,kotlin.Boolean)` before, to ensure any operation is complete.

| Parameters |
|---|---|
| `long timeoutMs` | a timeout to find the element that satisfies the given condition. |
| `long pollIntervalMs` | an interval to wait before rechecking the accessibility tree for updates. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block` | a block that specifies a condition on the node to find. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2>` | a list of `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` from nodes that matches the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).onElements(kotlin.Long,kotlin.Long,kotlin.Function1)` condition. |

### UiObject2Ext.scrollToElement

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2Ext.kt+function:scrollToElement)

```
public final @NonNull UiObject2 UiObject2Ext.scrollToElement(
    @NonNull UiObject2 receiver,
    @NonNull Direction direction,
    long timeoutMs,
    long pollIntervalMs,
    @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
```

Keeps scrolling until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElement(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)` condition is satisfied or until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElement(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)`. Throws a `https://developer.android.com/reference/androidx/test/uiautomator/ElementNotFoundException` if the condition is not satisfied at the end of the timeout.

Example:

```
onElement { isScrollable }.scrollToElement(Direction.DOWN) { id == "button" }.click()
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | the scroll `https://developer.android.com/reference/androidx/test/uiautomator/Direction`. |
| `long timeoutMs` | a timeout to find the element that satisfies the given condition. |
| `long pollIntervalMs` | an interval to wait before rechecking the accessibility tree for updates. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block` | a block that specifies a condition on the node to find. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | a `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` that matches the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElement(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)` condition. |

### UiObject2Ext.scrollToElementOrNull

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2Ext.kt+function:scrollToElementOrNull)

```
public final UiObject2 UiObject2Ext.scrollToElementOrNull(
    @NonNull UiObject2 receiver,
    @NonNull Direction direction,
    long timeoutMs,
    long pollIntervalMs,
    @NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block
)
```

Keeps scrolling until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElementOrNull(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)` condition is satisfied or until the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElementOrNull(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)`. Returns null if the condition is not satisfied at the end of the timeout.

Example:

```
onElement { isScrollable }.scrollToElement(Direction.DOWN) { id == "button" }.click()
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/Direction direction` | the scroll `https://developer.android.com/reference/androidx/test/uiautomator/Direction`. |
| `long timeoutMs` | a timeout to find the element that satisfies the given condition. |
| `long pollIntervalMs` | an interval to wait before rechecking the accessibility tree for updates. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Boolean.html> block` | a block that specifies a condition on the node to find. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` | a `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` that matches the given `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).scrollToElementOrNull(androidx.test.uiautomator.Direction,kotlin.Long,kotlin.Long,kotlin.Function1)` condition. |

### UiObject2Ext.takeScreenshot

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2Ext.kt+function:takeScreenshot)

```
public final @NonNull Bitmap UiObject2Ext.takeScreenshot(@NonNull UiObject2 receiver)
```

Takes a screenshot of the screen that contains the accessibility node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2` and cuts only the area covered by it.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Bitmap.html` | a bitmap containing the image of the node associated to this `https://developer.android.com/reference/androidx/test/uiautomator/UiObject2`. |

### UiObject2Ext.waitForStable

Artifact: [androidx.test.uiautomator:uiautomator](https://developer.android.com/jetpack/androidx/releases/test-uiautomator) [View Source](https://cs.android.com/search?q=file:androidx/test/uiautomator/UiObject2Ext.kt+function:waitForStable)

```
public final @NonNull StableResult UiObject2Ext.waitForStable(
    @NonNull UiObject2 receiver,
    long stableTimeoutMs,
    long stableIntervalMs,
    long stablePollIntervalMs,
    boolean requireStableScreenshot
)
```

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 |
|---|---|
| `long stableTimeoutMs` | a timeout for the wait operation, to ensure not waiting forever for stability. |
| `long stableIntervalMs` | the interval during which the node should not be changing, in order to be considered stable. |
| `long stablePollIntervalMs` | specifies how often the ui should be checked for changes. |
| `boolean requireStableScreenshot` | specifies if also the bitmap of the node should not change over the specified `https://developer.android.com/reference/androidx/test/uiautomator/package-summary#(androidx.test.uiautomator.UiObject2).waitForStable(kotlin.Long,kotlin.Long,kotlin.Long,kotlin.Boolean)`. Note that this won't work with elements that change constantly, like a video player. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/test/uiautomator/StableResult` | a `https://developer.android.com/reference/androidx/test/uiautomator/StableResult` containing the latest acquired element hierarchy and screenshot, and a flag indicating if the node was stable before timeout. The flag `https://developer.android.com/reference/androidx/test/uiautomator/StableResult#isTimeout()` is set to false if the node was stable before the timeout expired, true otherwise. |