Methods Summary |
---|
public void | clearTextField()Clears the existing text contents in an editable field.
The {@link UiSelector} of this object must reference a UI element that is editable.
When you call this method, the method first sets focus at the start edge of the field.
The method then simulates a long-press to select the existing text, and deletes the
selected text.
If a "Select-All" option is displayed, the method will automatically attempt to use it
to ensure full text selection.
Note that it is possible that not all the text in the field is selected; for example,
if the text contains separators such as spaces, slashes, at symbol etc.
Also, not all editable fields support the long-press functionality.
Tracer.trace();
// long click left + center
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
getInteractionController().longTapNoSync(rect.left + 20, rect.centerY());
// check if the edit menu is open
UiObject selectAll = new UiObject(new UiSelector().descriptionContains("Select all"));
if(selectAll.waitForExists(50))
selectAll.click();
// wait for the selection
SystemClock.sleep(250);
// delete it
getInteractionController().sendKey(KeyEvent.KEYCODE_DEL, 0);
|
public boolean | click()Performs a click at the center of the visible bounds of the UI element represented
by this UiObject.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().clickAndSync(rect.centerX(), rect.centerY(),
mConfig.getActionAcknowledgmentTimeout());
|
public boolean | clickAndWaitForNewWindow()Waits for window transitions that would typically take longer than the
usual default timeouts.
See {@link #clickAndWaitForNewWindow(long)}
Tracer.trace();
return clickAndWaitForNewWindow(WAIT_FOR_WINDOW_TMEOUT);
|
public boolean | clickAndWaitForNewWindow(long timeout)Performs a click at the center of the visible bounds of the UI element represented
by this UiObject and waits for window transitions.
This method differ from {@link UiObject#click()} only in that this method waits for a
a new window transition as a result of the click. Some examples of a window transition:
launching a new activity
bringing up a pop-up menu
bringing up a dialog
Tracer.trace(timeout);
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().clickAndWaitForNewWindow(rect.centerX(), rect.centerY(),
mConfig.getActionAcknowledgmentTimeout());
|
public boolean | clickBottomRight()Clicks the bottom and right corner of the UI element
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().clickNoSync(rect.right - 5, rect.bottom - 5);
|
public boolean | clickTopLeft()Clicks the top and left corner of the UI element
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().clickNoSync(rect.left + 5, rect.top + 5);
|
public boolean | dragTo(int destX, int destY, int steps)Drags this object to arbitrary coordinates.
The number of steps specified in your input parameter can influence the
drag speed, and varying speeds may impact the results. Consider
evaluating different speeds when using this method in your tests.
Rect srcRect = getVisibleBounds();
return getInteractionController().swipe(srcRect.centerX(), srcRect.centerY(), destX, destY,
steps, true);
|
public boolean | dragTo(com.android.uiautomator.core.UiObject destObj, int steps)Drags this object to a destination UiObject.
The number of steps specified in your input parameter can influence the
drag speed, and varying speeds may impact the results. Consider
evaluating different speeds when using this method in your tests.
Rect srcRect = getVisibleBounds();
Rect dstRect = destObj.getVisibleBounds();
return getInteractionController().swipe(srcRect.centerX(), srcRect.centerY(),
dstRect.centerX(), dstRect.centerY(), steps, true);
|
public boolean | exists()Check if view exists.
This methods performs a {@link #waitForExists(long)} with zero timeout. This
basically returns immediately whether the view represented by this UiObject
exists or not. If you need to wait longer for this view, then see
{@link #waitForExists(long)}.
Tracer.trace();
return waitForExists(0);
|
protected android.view.accessibility.AccessibilityNodeInfo | findAccessibilityNodeInfo(long timeout)Finds a matching UI element in the accessibility hierarchy, by
using the selector for this UiObject.
AccessibilityNodeInfo node = null;
long startMills = SystemClock.uptimeMillis();
long currentMills = 0;
while (currentMills <= timeout) {
node = getQueryController().findAccessibilityNodeInfo(getSelector());
if (node != null) {
break;
} else {
// does nothing if we're reentering another runWatchers()
UiDevice.getInstance().runWatchers();
}
currentMills = SystemClock.uptimeMillis() - startMills;
if(timeout > 0) {
SystemClock.sleep(WAIT_FOR_SELECTOR_POLL);
}
}
return node;
|
public android.graphics.Rect | getBounds()Returns the view's bounds property. See {@link #getVisibleBounds()}
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect nodeRect = new Rect();
node.getBoundsInScreen(nodeRect);
return nodeRect;
|
public com.android.uiautomator.core.UiObject | getChild(UiSelector selector)Creates a new UiObject for a child view that is under the present UiObject.
Tracer.trace(selector);
return new UiObject(getSelector().childSelector(selector));
|
public int | getChildCount()Counts the child views immediately under the present UiObject.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.getChildCount();
|
public java.lang.String | getClassName()Retrieves the className property of the UI element.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
String retVal = safeStringReturn(node.getClassName());
Log.d(LOG_TAG, String.format("getClassName() = %s", retVal));
return retVal;
|
public java.lang.String | getContentDescription()Reads the content_desc property of the UI element
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return safeStringReturn(node.getContentDescription());
|
public com.android.uiautomator.core.UiObject | getFromParent(UiSelector selector)Creates a new UiObject for a sibling view or a child of the sibling view,
relative to the present UiObject.
Tracer.trace(selector);
return new UiObject(getSelector().fromParent(selector));
|
InteractionController | getInteractionController()Retrieves the {@link InteractionController} to perform finger actions such as tapping,
swiping, or entering text.
return UiDevice.getInstance().getAutomatorBridge().getInteractionController();
|
public java.lang.String | getPackageName()Reads the view's package property
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return safeStringReturn(node.getPackageName());
|
QueryController | getQueryController()Retrieves the {@link QueryController} to translate a {@link UiSelector} selector
into an {@link AccessibilityNodeInfo}.
return UiDevice.getInstance().getAutomatorBridge().getQueryController();
|
private android.view.accessibility.AccessibilityNodeInfo | getScrollableParent(android.view.accessibility.AccessibilityNodeInfo node)Walks up the layout hierarchy to find a scrollable parent. A scrollable parent
indicates that this node might be in a container where it is partially
visible due to scrolling. In this case, its clickable center might not be visible and
the click coordinates should be adjusted.
AccessibilityNodeInfo parent = node;
while(parent != null) {
parent = parent.getParent();
if (parent != null && parent.isScrollable()) {
return parent;
}
}
return null;
|
public final UiSelector | getSelector()Debugging helper. A test can dump the properties of a selector as a string
to its logs if needed. getSelector().toString();
Tracer.trace();
return new UiSelector(mSelector);
|
public java.lang.String | getText()Reads the text property of the UI element
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
String retVal = safeStringReturn(node.getText());
Log.d(LOG_TAG, String.format("getText() = %s", retVal));
return retVal;
|
private android.graphics.Rect | getVisibleBounds(android.view.accessibility.AccessibilityNodeInfo node)Finds the visible bounds of a partially visible UI element
if (node == null) {
return null;
}
// targeted node's bounds
int w = UiDevice.getInstance().getDisplayWidth();
int h = UiDevice.getInstance().getDisplayHeight();
Rect nodeRect = AccessibilityNodeInfoHelper.getVisibleBoundsInScreen(node, w, h);
// is the targeted node within a scrollable container?
AccessibilityNodeInfo scrollableParentNode = getScrollableParent(node);
if(scrollableParentNode == null) {
// nothing to adjust for so return the node's Rect as is
return nodeRect;
}
// Scrollable parent's visible bounds
Rect parentRect = AccessibilityNodeInfoHelper
.getVisibleBoundsInScreen(scrollableParentNode, w, h);
// adjust for partial clipping of targeted by parent node if required
nodeRect.intersect(parentRect);
return nodeRect;
|
public android.graphics.Rect | getVisibleBounds()Returns the visible bounds of the view.
If a portion of the view is visible, only the bounds of the visible portion are
reported.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return getVisibleBounds(node);
|
public boolean | isCheckable()Checks if the UI element's checkable property is currently true.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isCheckable();
|
public boolean | isChecked()Check if the UI element's checked property is currently true
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isChecked();
|
public boolean | isClickable()Checks if the UI element's clickable property is currently true.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isClickable();
|
public boolean | isEnabled()Checks if the UI element's enabled property is currently true.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isEnabled();
|
public boolean | isFocusable()Check if the UI element's focusable property is currently true.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isFocusable();
|
public boolean | isFocused()Check if the UI element's focused property is currently true
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isFocused();
|
public boolean | isLongClickable()Check if the view's long-clickable property is currently true
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isLongClickable();
|
public boolean | isScrollable()Check if the view's scrollable property is currently true
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isScrollable();
|
public boolean | isSelected()Checks if the UI element's selected property is currently true.
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
return node.isSelected();
|
public boolean | longClick()Long clicks the center of the visible bounds of the UI element
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().longTapNoSync(rect.centerX(), rect.centerY());
|
public boolean | longClickBottomRight()Long clicks bottom and right corner of the UI element
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().longTapNoSync(rect.right - 5, rect.bottom - 5);
|
public boolean | longClickTopLeft()Long clicks on the top and left corner of the UI element
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().longTapNoSync(rect.left + 5, rect.top + 5);
|
public boolean | performMultiPointerGesture(android.view.MotionEvent.PointerCoords[] touches)Performs a multi-touch gesture. You must specify touch coordinates for
at least 2 pointers. Each pointer must have all of its touch steps
defined in an array of {@link PointerCoords}. You can use this method to
specify complex gestures, like circles and irregular shapes, where each
pointer may take a different path.
To create a single point on a pointer's touch path:
PointerCoords p = new PointerCoords();
p.x = stepX;
p.y = stepY;
p.pressure = 1;
p.size = 1;
return getInteractionController().performMultiPointerGesture(touches);
|
public boolean | performTwoPointerGesture(android.graphics.Point startPoint1, android.graphics.Point startPoint2, android.graphics.Point endPoint1, android.graphics.Point endPoint2, int steps)Generates a two-pointer gesture with arbitrary starting and ending points.
// avoid a divide by zero
if(steps == 0)
steps = 1;
final float stepX1 = (endPoint1.x - startPoint1.x) / steps;
final float stepY1 = (endPoint1.y - startPoint1.y) / steps;
final float stepX2 = (endPoint2.x - startPoint2.x) / steps;
final float stepY2 = (endPoint2.y - startPoint2.y) / steps;
int eventX1, eventY1, eventX2, eventY2;
eventX1 = startPoint1.x;
eventY1 = startPoint1.y;
eventX2 = startPoint2.x;
eventY2 = startPoint2.y;
// allocate for steps plus first down and last up
PointerCoords[] points1 = new PointerCoords[steps + 2];
PointerCoords[] points2 = new PointerCoords[steps + 2];
// Include the first and last touch downs in the arrays of steps
for (int i = 0; i < steps + 1; i++) {
PointerCoords p1 = new PointerCoords();
p1.x = eventX1;
p1.y = eventY1;
p1.pressure = 1;
p1.size = 1;
points1[i] = p1;
PointerCoords p2 = new PointerCoords();
p2.x = eventX2;
p2.y = eventY2;
p2.pressure = 1;
p2.size = 1;
points2[i] = p2;
eventX1 += stepX1;
eventY1 += stepY1;
eventX2 += stepX2;
eventY2 += stepY2;
}
// ending pointers coordinates
PointerCoords p1 = new PointerCoords();
p1.x = endPoint1.x;
p1.y = endPoint1.y;
p1.pressure = 1;
p1.size = 1;
points1[steps + 1] = p1;
PointerCoords p2 = new PointerCoords();
p2.x = endPoint2.x;
p2.y = endPoint2.y;
p2.pressure = 1;
p2.size = 1;
points2[steps + 1] = p2;
return performMultiPointerGesture(points1, points2);
|
public boolean | pinchIn(int percent, int steps)Performs a two-pointer gesture, where each pointer moves diagonally
toward the other, from the edges to the center of this UiObject .
// make value between 1 and 100
percent = (percent < 0) ? 0 : (percent > 100) ? 100 : percent;
float percentage = percent / 100f;
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if (node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2)
throw new IllegalStateException("Object width is too small for operation");
Point startPoint1 = new Point(rect.centerX() - (int)((rect.width()/2) * percentage),
rect.centerY());
Point startPoint2 = new Point(rect.centerX() + (int)((rect.width()/2) * percentage),
rect.centerY());
Point endPoint1 = new Point(rect.centerX() - FINGER_TOUCH_HALF_WIDTH, rect.centerY());
Point endPoint2 = new Point(rect.centerX() + FINGER_TOUCH_HALF_WIDTH, rect.centerY());
return performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, steps);
|
public boolean | pinchOut(int percent, int steps)Performs a two-pointer gesture, where each pointer moves diagonally
opposite across the other, from the center out towards the edges of the
this UiObject.
// make value between 1 and 100
percent = (percent < 0) ? 1 : (percent > 100) ? 100 : percent;
float percentage = percent / 100f;
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if (node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
if (rect.width() <= FINGER_TOUCH_HALF_WIDTH * 2)
throw new IllegalStateException("Object width is too small for operation");
// start from the same point at the center of the control
Point startPoint1 = new Point(rect.centerX() - FINGER_TOUCH_HALF_WIDTH, rect.centerY());
Point startPoint2 = new Point(rect.centerX() + FINGER_TOUCH_HALF_WIDTH, rect.centerY());
// End at the top-left and bottom-right corners of the control
Point endPoint1 = new Point(rect.centerX() - (int)((rect.width()/2) * percentage),
rect.centerY());
Point endPoint2 = new Point(rect.centerX() + (int)((rect.width()/2) * percentage),
rect.centerY());
return performTwoPointerGesture(startPoint1, startPoint2, endPoint1, endPoint2, steps);
|
private java.lang.String | safeStringReturn(java.lang.CharSequence cs)
if(cs == null)
return "";
return cs.toString();
|
public boolean | setText(java.lang.String text)Sets the text in an editable field, after clearing the field's content.
The {@link UiSelector} selector of this object must reference a UI element that is editable.
When you call this method, the method first simulates a {@link #click()} on
editable field to set focus. The method then clears the field's contents
and injects your specified text into the field.
If you want to capture the original contents of the field, call {@link #getText()} first.
You can then modify the text and use this method to update the field.
Tracer.trace(text);
clearTextField();
return getInteractionController().sendText(text);
|
public boolean | swipeDown(int steps)Performs the swipe down action on the UiObject.
The swipe gesture can be performed over any surface. The targeted
UI element does not need to be scrollable.
See also:
- {@link UiScrollable#scrollToBeginning(int)}
- {@link UiScrollable#scrollToEnd(int)}
- {@link UiScrollable#scrollBackward()}
- {@link UiScrollable#scrollForward()}
Tracer.trace(steps);
Rect rect = getVisibleBounds();
if(rect.height() <= SWIPE_MARGIN_LIMIT * 2)
return false; // too small to swipe
return getInteractionController().swipe(rect.centerX(),
rect.top + SWIPE_MARGIN_LIMIT, rect.centerX(),
rect.bottom - SWIPE_MARGIN_LIMIT, steps);
|
public boolean | swipeLeft(int steps)Performs the swipe left action on the UiObject.
The swipe gesture can be performed over any surface. The targeted
UI element does not need to be scrollable.
See also:
- {@link UiScrollable#scrollToBeginning(int)}
- {@link UiScrollable#scrollToEnd(int)}
- {@link UiScrollable#scrollBackward()}
- {@link UiScrollable#scrollForward()}
Tracer.trace(steps);
Rect rect = getVisibleBounds();
if(rect.width() <= SWIPE_MARGIN_LIMIT * 2)
return false; // too small to swipe
return getInteractionController().swipe(rect.right - SWIPE_MARGIN_LIMIT,
rect.centerY(), rect.left + SWIPE_MARGIN_LIMIT, rect.centerY(), steps);
|
public boolean | swipeRight(int steps)Performs the swipe right action on the UiObject.
The swipe gesture can be performed over any surface. The targeted
UI element does not need to be scrollable.
See also:
- {@link UiScrollable#scrollToBeginning(int)}
- {@link UiScrollable#scrollToEnd(int)}
- {@link UiScrollable#scrollBackward()}
- {@link UiScrollable#scrollForward()}
Tracer.trace(steps);
Rect rect = getVisibleBounds();
if(rect.width() <= SWIPE_MARGIN_LIMIT * 2)
return false; // too small to swipe
return getInteractionController().swipe(rect.left + SWIPE_MARGIN_LIMIT,
rect.centerY(), rect.right - SWIPE_MARGIN_LIMIT, rect.centerY(), steps);
|
public boolean | swipeUp(int steps)Performs the swipe up action on the UiObject.
See also:
- {@link UiScrollable#scrollToBeginning(int)}
- {@link UiScrollable#scrollToEnd(int)}
- {@link UiScrollable#scrollBackward()}
- {@link UiScrollable#scrollForward()}
Tracer.trace(steps);
Rect rect = getVisibleBounds();
if(rect.height() <= SWIPE_MARGIN_LIMIT * 2)
return false; // too small to swipe
return getInteractionController().swipe(rect.centerX(),
rect.bottom - SWIPE_MARGIN_LIMIT, rect.centerX(), rect.top + SWIPE_MARGIN_LIMIT,
steps);
|
public boolean | waitForExists(long timeout)Waits a specified length of time for a view to become visible.
This method waits until the view becomes visible on the display, or
until the timeout has elapsed. You can use this method in situations where
the content that you want to select is not immediately displayed.
Tracer.trace(timeout);
if(findAccessibilityNodeInfo(timeout) != null) {
return true;
}
return false;
|
public boolean | waitUntilGone(long timeout)Waits a specified length of time for a view to become undetectable.
This method waits until a view is no longer matchable, or until the
timeout has elapsed.
A view becomes undetectable when the {@link UiSelector} of the object is
unable to find a match because the element has either changed its state or is no
longer displayed.
You can use this method when attempting to wait for some long operation
to compete, such as downloading a large file or connecting to a remote server.
Tracer.trace(timeout);
long startMills = SystemClock.uptimeMillis();
long currentMills = 0;
while (currentMills <= timeout) {
if(findAccessibilityNodeInfo(0) == null)
return true;
currentMills = SystemClock.uptimeMillis() - startMills;
if(timeout > 0)
SystemClock.sleep(WAIT_FOR_SELECTOR_POLL);
}
return false;
|