UiObject2
public class UiObject2 implements 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 methods |
|
|---|---|
void |
clear()Clears this object's text content if it is an editable field. |
void |
click()Clicks on this object's center. |
void |
click(long duration)Clicks on this object's center for |
void |
Clicks on a |
void |
Clicks on a |
U |
<U> clickAndWait(@NonNull EventCondition<U> condition, long timeout)Clicks on this object's center, and waits for a |
U |
<U> clickAndWait(Clicks on a |
void |
Drags this object to the specified point. |
void |
Drags this object to the specified point. |
boolean |
|
UiObject2 |
findObject(@NonNull BySelector selector)Searches all elements under this object and returns the first one to match the |
@NonNull List<UiObject2> |
findObjects(@NonNull BySelector selector)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. |
@NonNull 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. |
@NonNull 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. |
@Nullable 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. |
@NonNull Rect |
Returns this object's visible bounds. |
@NonNull Point |
Returns a point in the center of this object's visible bounds. |
boolean |
hasObject(@NonNull BySelector selector)Returns |
int |
hashCode() |
boolean |
Returns |
boolean |
Returns |
boolean |
Returns |
boolean |
Returns |
boolean |
Returns |
boolean |
Returns |
boolean |
Returns |
boolean |
Returns |
boolean |
Returns |
void |
Performs a long click on this object's center. |
void |
pinchClose(float percent)Performs a pinch close gesture on this object. |
void |
pinchClose(float percent, int speed)Performs a pinch close gesture on this object. |
void |
pinchOpen(float percent)Performs a pinch open gesture on this object. |
void |
pinchOpen(float percent, int speed)Performs a pinch open gesture on this object. |
void |
recycle()Recycle this object. |
boolean |
Performs a scroll gesture on this object. |
boolean |
Performs a scroll gesture on this object. |
U |
Perform scroll actions in certain direction until a |
U |
<U> scrollUntil(Perform scroll actions in certain direction until a |
void |
setGestureMargin(int margin)Sets the margins used for gestures in pixels. |
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. |
void |
setGestureMargins(int left, int top, int right, int bottom)Sets the margins used for gestures in pixels. |
void |
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. |
void |
Sets this object's text content if it is an editable field. |
void |
Performs a swipe gesture on this object. |
void |
Performs a swipe gesture on this object. |
U |
Waits for a |
U |
<U> wait(@NonNull SearchCondition<U> condition, long timeout)Waits for a |
U |
<U> wait(@NonNull UiObject2Condition<U> condition, long timeout)Waits for a |
Extension functions |
|
|---|---|
final @NonNull UiObject2 |
UiObject2Ext.onElement(Performs a DFS on the accessibility tree starting from the node associated to this |
final UiObject2 |
UiObject2Ext.onElementOrNull(Performs a DFS on the accessibility tree starting from the node associated to this |
final @NonNull List<@NonNull UiObject2> |
UiObject2Ext.onElements(Performs a DFS on the accessibility tree starting from the node associated to this |
final @NonNull UiObject2 |
UiObject2Ext.scrollToElement(Keeps scrolling until the given |
final UiObject2 |
UiObject2Ext.scrollToElementOrNull(Keeps scrolling until the given |
final @NonNull Bitmap |
UiObject2Ext.takeScreenshot(@NonNull UiObject2 receiver)Takes a screenshot of the screen that contains the accessibility node associated to this |
final @NonNull StableResult |
UiObject2Ext.waitForStable(Waits for the node to become stable. |
Public methods
clear
public void clear()
Clears this object's text content if it is an editable field.
click
public void click(long duration)
Clicks on this object's center for duration milliseconds.
click
public void click(@NonNull Point point)
Clicks on a point within this object's visible bounds.
click
public void click(@NonNull Point point, long duration)
Clicks on a point within this object's visible bounds.
clickAndWait
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 | |
|---|---|
@NonNull EventCondition<U> condition |
The |
long timeout |
The maximum time in milliseconds to wait for. |
clickAndWait
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 | |
|---|---|
@NonNull Point point |
The point to click (clipped to ensure it is within the visible bounds). |
@NonNull EventCondition<U> condition |
The |
long timeout |
The maximum time in milliseconds to wait for. |
drag
public void drag(@NonNull Point dest, int speed)
Drags this object to the specified point.
findObject
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
public @NonNull List<UiObject2> findObjects(@NonNull BySelector selector)
Searches all elements under this object and returns those that match the selector.
fling
public boolean fling(@NonNull Direction direction)
Performs a fling gesture on this object.
| Returns | |
|---|---|
boolean |
|
fling
public boolean fling(@NonNull Direction direction, int speed)
Performs a fling gesture on this object.
| Parameters | |
|---|---|
@NonNull Direction direction |
The direction in which to fling. |
int speed |
The speed at which to perform this gesture in pixels per second. |
| Returns | |
|---|---|
boolean |
|
getAccessibilityNodeInfo
public @NonNull AccessibilityNodeInfo getAccessibilityNodeInfo()
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
public String getApplicationPackage()
Returns the package name of the app that this object belongs to.
getChildCount
public int getChildCount()
Returns the number of child elements directly under this object.
getChildren
public @NonNull List<UiObject2> getChildren()
Returns a collection of the child elements directly under this object.
getClassName
public String getClassName()
Returns the class name of this object's underlying View.
getContentDescription
public String getContentDescription()
Returns this object's content description.
| See also | |
|---|---|
getContentDescription |
getDisplayId
public int getDisplayId()
Returns the ID of the display containing this object.
getDrawingOrder
@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
@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 | |
|---|---|
getHint |
getParent
public UiObject2 getParent()
Returns this object's parent, or null if it has no parent.
getResourceName
public String getResourceName()
Returns the fully qualified resource name for this object's ID.
getVisibleBounds
public @NonNull Rect getVisibleBounds()
Returns this object's visible bounds.
getVisibleCenter
public @NonNull Point getVisibleCenter()
Returns a point in the center of this object's visible bounds.
hasObject
public boolean hasObject(@NonNull BySelector selector)
Returns true if there is a nested element which matches the selector.
isCheckable
public boolean isCheckable()
Returns true if this object is checkable.
| See also | |
|---|---|
Checkable |
isChecked
public boolean isChecked()
Returns true if this object is checked.
| See also | |
|---|---|
isChecked |
isClickable
public boolean isClickable()
Returns true if this object is clickable.
| See also | |
|---|---|
isClickable |
isEnabled
public boolean isEnabled()
Returns true if this object is enabled.
| See also | |
|---|---|
isEnabled |
isFocusable
public boolean isFocusable()
Returns true if this object is focusable.
| See also | |
|---|---|
isFocusable |
isFocused
public boolean isFocused()
Returns true if this object is focused.
| See also | |
|---|---|
isFocused |
isLongClickable
public boolean isLongClickable()
Returns true if this object is long clickable.
| See also | |
|---|---|
isLongClickable |
isScrollable
public boolean isScrollable()
Returns true if this object is scrollable.
isSelected
public boolean isSelected()
Returns true if this object is selected.
| See also | |
|---|---|
isSelected |
pinchClose
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
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
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
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. |
scroll
public boolean scroll(@NonNull Direction direction, float percent)
Performs a scroll gesture on this object.
| Parameters | |
|---|---|
@NonNull 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 |
|
scroll
public boolean scroll(@NonNull Direction direction, float percent, int speed)
Performs a scroll gesture on this object.
| Parameters | |
|---|---|
@NonNull 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 |
|
scrollUntil
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 | |
|---|---|
@NonNull Direction direction |
The direction in which to scroll. |
@NonNull Condition<UiObject2, U> condition |
The |
| Returns | |
|---|---|
U |
If the condition is satisfied. |
scrollUntil
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 | |
|---|---|
@NonNull Direction direction |
The direction in which to scroll. |
@NonNull EventCondition<U> condition |
The |
| Returns | |
|---|---|
U |
The value obtained after applying the condition. |
setGestureMargin
public void setGestureMargin(int margin)
Sets the margins used for gestures in pixels.
setGestureMarginPercentage
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 | |
|---|---|
@FloatRange(from = 0.0, to = 0.5) float percent |
Float between [0, 0.5] for four margins: left, top, right, and bottom. |
setGestureMargins
public void setGestureMargins(int left, int top, int right, int bottom)
Sets the margins used for gestures in pixels.
setGestureMarginsPercentage
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
public void setText(@Nullable String text)
Sets this object's text content if it is an editable field.
swipe
public void swipe(@NonNull Direction direction, float percent)
Performs a swipe gesture on this object.
swipe
public void swipe(@NonNull Direction direction, float percent, int speed)
Performs a swipe gesture on this object.
wait
public U <U> wait(@NonNull Condition<UiObject2, U> condition, long timeout)
Waits for a condition to be met.
| Parameters | |
|---|---|
@NonNull Condition<UiObject2, U> condition |
The |
long timeout |
The maximum time in milliseconds to wait for. |
| Returns | |
|---|---|
U |
The final result returned by the |
wait
public U <U> wait(@NonNull SearchCondition<U> condition, long timeout)
Waits for a condition to be met.
| Parameters | |
|---|---|
@NonNull SearchCondition<U> condition |
The |
long timeout |
The maximum time in milliseconds to wait for. |
| Returns | |
|---|---|
U |
The final result returned by the |
wait
public U <U> wait(@NonNull UiObject2Condition<U> condition, long timeout)
Waits for a condition to be met.
| Parameters | |
|---|---|
@NonNull UiObject2Condition<U> condition |
The |
long timeout |
The maximum time in milliseconds to wait for. |
| Returns | |
|---|---|
U |
The final result returned by the |
Extension functions
UiObject2Ext.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 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 | |
|---|---|
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. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
UiObject2Ext.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 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 | |
|---|---|
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. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
UiObject2Ext.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 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 | |
|---|---|
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. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
UiObject2Ext.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 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 | |
|---|---|
@NonNull Direction direction |
the scroll |
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. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
UiObject2Ext.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 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 | |
|---|---|
@NonNull Direction direction |
the scroll |
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. |
@NonNull Function1<@NonNull AccessibilityNodeInfo, @NonNull Boolean> block |
a block that specifies a condition on the node to find. |
UiObject2Ext.takeScreenshot
public final @NonNull Bitmap UiObject2Ext.takeScreenshot(@NonNull UiObject2 receiver)
Takes a screenshot of the screen that contains the accessibility node associated to this UiObject2 and cuts only the area covered by it.
UiObject2Ext.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 |
| Returns | |
|---|---|
@NonNull StableResult |
a |