Skip to content

Most visited

Recently visited

navigation

UiObject2

public class UiObject2
extends Object

java.lang.Object
   ↳ android.support.test.uiautomator.UiObject2


A UiObject2 represents a UI element. Unlike UiObject, it 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(BySelector) to obtain a new UiObject2 instance if the UI changes significantly.

Summary

Public methods

void clear()

Clears the text content if this object is an editable field.

void click()

Clicks on this object.

void click(long duration)

Performs a click on this object that lasts for duration milliseconds.

<R> R clickAndWait(EventCondition<R> condition, long timeout)

Clicks on this object, and waits for the given condition to become true.

void drag(Point dest)

Drags this object to the specified location.

void drag(Point dest, int speed)

Drags this object to the specified location.

boolean equals(Object object)

UiObject2 findObject(BySelector selector)

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

List<UiObject2> findObjects(BySelector selector)

Searches all elements under this object and returns all objects that match the criteria.

boolean fling(Direction direction, int speed)

Performs a fling gesture on this object.

boolean fling(Direction direction)

Performs a fling gesture on this object.

String getApplicationPackage()

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

int getChildCount()

Returns the number of child elements directly under this object.

List<UiObject2> getChildren()

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

String getClassName()

Returns the class name of the underlying View represented by this object.

String getContentDescription()

Returns the content description for this object.

UiObject2 getParent()

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

String getResourceName()

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

String getText()

Returns the text value for this object.

Rect getVisibleBounds()

Returns the visible bounds of this object in screen coordinates.

Point getVisibleCenter()

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

boolean hasObject(BySelector selector)

Returns whether there is a match for the given criteria under this object.

int hashCode()

boolean isCheckable()

Returns whether this object is checkable.

boolean isChecked()

Returns whether this object is checked.

boolean isClickable()

Returns whether this object is clickable.

boolean isEnabled()

Returns whether this object is enabled.

boolean isFocusable()

Returns whether this object is focusable.

boolean isFocused()

Returns whether this object is focused.

boolean isLongClickable()

Returns whether this object is long clickable.

boolean isScrollable()

Returns whether this object is scrollable.

boolean isSelected()

Returns whether this object is selected.

void longClick()

Performs a long click on this object.

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 scroll(Direction direction, float percent, int speed)

Performs a scroll gesture on this object.

boolean scroll(Direction direction, float percent)

Performs a scroll gesture on this object.

void setGestureMargin(int margin)

Sets the margins used for gestures in pixels.

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

Sets the margins used for gestures in pixels.

void setText(String text)

Sets the text content if this object is an editable field.

void swipe(Direction direction, float percent, int speed)

Performs a swipe gesture on this object.

void swipe(Direction direction, float percent)

Performs a swipe gesture on this object.

<R> R wait(UiObject2Condition<R> condition, long timeout)

Waits for given the condition to be met.

<R> R wait(SearchCondition<R> condition, long timeout)

Waits for given the condition to be met.

Inherited methods

From class java.lang.Object

Public methods

clear

void clear ()

Clears the text content if this object is an editable field.

click

void click ()

Clicks on this object.

click

void click (long duration)

Performs a click on this object that lasts for duration milliseconds.

Parameters
duration long

clickAndWait

R clickAndWait (EventCondition<R> condition, 
                long timeout)

Clicks on this object, and waits for the given condition to become true.

Parameters
condition EventCondition

timeout long

Returns
R

drag

void drag (Point dest)

Drags this object to the specified location.

Parameters
dest Point: The end point that this object should be dragged to.

drag

void drag (Point dest, 
                int speed)

Drags this object to the specified location.

Parameters
dest Point: The end point that this object should be dragged to.

speed int: The speed at which to perform this gesture in pixels per second.

equals

boolean equals (Object object)

Parameters
object Object

Returns
boolean

findObject

UiObject2 findObject (BySelector selector)

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

Parameters
selector BySelector

Returns
UiObject2

findObjects

List<UiObject2> findObjects (BySelector selector)

Searches all elements under this object and returns all objects that match the criteria.

Parameters
selector BySelector

Returns
List<UiObject2>

fling

boolean fling (Direction direction, 
                int speed)

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 Whether the object can still scroll in the given direction.

fling

boolean fling (Direction direction)

Performs a fling gesture on this object.

Parameters
direction Direction: The direction in which to fling.

Returns
boolean Whether the object can still scroll in the given direction.

getApplicationPackage

String getApplicationPackage ()

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

Returns
String

getChildCount

int getChildCount ()

Returns the number of child elements directly under this object.

Returns
int

getChildren

List<UiObject2> getChildren ()

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

Returns
List<UiObject2>

getClassName

String getClassName ()

Returns the class name of the underlying View represented by this object.

Returns
String

getContentDescription

String getContentDescription ()

Returns the content description for this object.

Returns
String

getParent

UiObject2 getParent ()

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

Returns
UiObject2

getResourceName

String getResourceName ()

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

Returns
String

getText

String getText ()

Returns the text value for this object.

Returns
String

getVisibleBounds

Rect getVisibleBounds ()

Returns the visible bounds of this object in screen coordinates.

Returns
Rect

getVisibleCenter

Point getVisibleCenter ()

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

Returns
Point

hasObject

boolean hasObject (BySelector selector)

Returns whether there is a match for the given criteria under this object.

Parameters
selector BySelector

Returns
boolean

hashCode

int hashCode ()

Returns
int

isCheckable

boolean isCheckable ()

Returns whether this object is checkable.

Returns
boolean

isChecked

boolean isChecked ()

Returns whether this object is checked.

Returns
boolean

isClickable

boolean isClickable ()

Returns whether this object is clickable.

Returns
boolean

isEnabled

boolean isEnabled ()

Returns whether this object is enabled.

Returns
boolean

isFocusable

boolean isFocusable ()

Returns whether this object is focusable.

Returns
boolean

isFocused

boolean isFocused ()

Returns whether this object is focused.

Returns
boolean

isLongClickable

boolean isLongClickable ()

Returns whether this object is long clickable.

Returns
boolean

isScrollable

boolean isScrollable ()

Returns whether this object is scrollable.

Returns
boolean

isSelected

boolean isSelected ()

Returns whether this object is selected.

Returns
boolean

longClick

void longClick ()

Performs a long click on this object.

pinchClose

void pinchClose (float percent)

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

void pinchClose (float percent, 
                int speed)

Performs a pinch close gesture on this object.

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

speed int: The speed at which to perform this gesture in pixels per second.

pinchOpen

void pinchOpen (float percent)

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

void pinchOpen (float percent, 
                int speed)

Performs a pinch open gesture on this object.

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

speed int: The speed at which to perform this gesture in pixels per second.

recycle

void recycle ()

Recycle this object.

scroll

boolean scroll (Direction direction, 
                float percent, 
                int speed)

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 Whether the object can still scroll in the given direction.

scroll

boolean scroll (Direction direction, 
                float percent)

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 Whether the object can still scroll in the given direction.

setGestureMargin

void setGestureMargin (int margin)

Sets the margins used for gestures in pixels.

Parameters
margin int

setGestureMargins

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

Sets the margins used for gestures in pixels.

Parameters
left int

top int

right int

bottom int

setText

void setText (String text)

Sets the text content if this object is an editable field.

Parameters
text String

swipe

void swipe (Direction direction, 
                float percent, 
                int speed)

Performs a swipe gesture on this object.

Parameters
direction Direction: The direction in which to swipe.

percent float: The length of the swipe as a percentage of this object's size.

speed int: The speed at which to perform this gesture in pixels per second.

swipe

void swipe (Direction direction, 
                float percent)

Performs a swipe gesture on this object.

Parameters
direction Direction: The direction in which to swipe.

percent float: The length of the swipe as a percentage of this object's size.

wait

R wait (UiObject2Condition<R> condition, 
                long timeout)

Waits for given the condition to be met.

Parameters
condition UiObject2Condition: The UiObject2Condition to evaluate.

timeout long: Maximum amount of time to wait in milliseconds.

Returns
R The final result returned by the condition, or null if the condition was not met before the timeout.

wait

R wait (SearchCondition<R> condition, 
                long timeout)

Waits for given the condition to be met.

Parameters
condition SearchCondition: The SearchCondition to evaluate.

timeout long: Maximum amount of time to wait in milliseconds.

Returns
R The final result returned by the condition, or null if the condition was not met before the timeout.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)