public class

UiObject2

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

Class Overview

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.
<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, int speed)
Drags this object to the specified location.
void drag(Point dest)
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.
List<UiObject2> findObjects(BySelector selector)
Searches all elements under this object and returns all objects that match the criteria.
boolean fling(Direction direction)
Performs a fling gesture on this object.
boolean fling(Direction direction, int speed)
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.
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, int speed)
Performs a pinch close gesture on this object.
void pinchClose(float percent)
Performs a pinch close gesture on this object.
void pinchOpen(float percent, int speed)
Performs a pinch open gesture on this object.
void pinchOpen(float percent)
Performs a pinch open gesture on this object.
void recycle()
Recycle this object.
boolean scroll(Direction direction, float percent)
Performs a scroll gesture on this object.
boolean scroll(Direction direction, float percent, int speed)
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.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void clear ()

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

public void click ()

Clicks on this object.

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

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

public void drag (Point dest, int speed)

Drags this object to the specified location.

Parameters
dest The end point that this object should be dragged to.
speed The speed at which to perform this gesture in pixels per second.

public void drag (Point dest)

Drags this object to the specified location.

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

public boolean equals (Object object)

public UiObject2 findObject (BySelector selector)

Searches all elements under this object and returns the first object to match the criteria.

public List<UiObject2> findObjects (BySelector selector)

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

public boolean fling (Direction direction)

Performs a fling gesture on this object.

Parameters
direction The direction in which to fling.
Returns
  • Whether the object can still scroll in the given direction.

public boolean fling (Direction direction, int speed)

Performs a fling gesture on this object.

Parameters
direction The direction in which to fling.
speed The speed at which to perform this gesture in pixels per second.
Returns
  • Whether the object can still scroll in the given direction.

public String getApplicationPackage ()

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

public int getChildCount ()

Returns the number of child elements directly under this object.

public List<UiObject2> getChildren ()

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

public String getClassName ()

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

public String getContentDescription ()

Returns the content description for this object.

public UiObject2 getParent ()

Returns this object's parent.

public String getResourceName ()

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

public String getText ()

Returns the text value for this object.

public Rect getVisibleBounds ()

Returns the visible bounds of this object in screen coordinates.

public Point getVisibleCenter ()

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

public boolean hasObject (BySelector selector)

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

public int hashCode ()

public boolean isCheckable ()

Returns whether this object is checkable.

public boolean isChecked ()

Returns whether this object is checked.

public boolean isClickable ()

Returns whether this object is clickable.

public boolean isEnabled ()

Returns whether this object is enabled.

public boolean isFocusable ()

Returns whether this object is focusable.

public boolean isFocused ()

Returns whether this object is focused.

public boolean isLongClickable ()

Returns whether this object is long clickable.

public boolean isScrollable ()

Returns whether this object is scrollable.

public boolean isSelected ()

Returns whether this object is selected.

public void longClick ()

Performs a long click on this object.

public void pinchClose (float percent, int speed)

Performs a pinch close gesture on this object.

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

public void pinchClose (float percent)

Performs a pinch close gesture on this object.

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

public void pinchOpen (float percent, int speed)

Performs a pinch open gesture on this object.

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

public void pinchOpen (float percent)

Performs a pinch open gesture on this object.

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

public void recycle ()

Recycle this object.

public boolean scroll (Direction direction, float percent)

Performs a scroll gesture on this object.

Parameters
direction The direction in which to scroll.
percent The distance to scroll as a percentage of this object's visible size.
Returns
  • Whether the object can still scroll in the given direction.

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

Performs a scroll gesture on this object.

Parameters
direction The direction in which to scroll.
percent The distance to scroll as a percentage of this object's visible size.
speed The speed at which to perform this gesture in pixels per second.
Returns
  • Whether the object can still scroll in the given direction.

public void setGestureMargin (int margin)

Sets the margins used for gestures in pixels.

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

Sets the margins used for gestures in pixels.

public void setText (String text)

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

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

Performs a swipe gesture on this object.

Parameters
direction The direction in which to swipe.
percent The length of the swipe as a percentage of this object's size.
speed The speed at which to perform this gesture in pixels per second.

public void swipe (Direction direction, float percent)

Performs a swipe gesture on this object.

Parameters
direction The direction in which to swipe.
percent The length of the swipe as a percentage of this object's size.

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

Waits for given the condition to be met.

Parameters
condition The UiObject2Condition to evaluate.
timeout Maximum amount of time to wait in milliseconds.
Returns
  • The final result returned by the condition.

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

Waits for given the condition to be met.

Parameters
condition The SearchCondition to evaluate.
timeout Maximum amount of time to wait in milliseconds.
Returns
  • The final result returned by the condition.