public class UiObject
extends Object
| java.lang.Object | |
| ↳ | android.support.test.uiautomator.UiObject |
| |
| |
UiObject是视图的表示。 它不以任何方式直接绑定到视图作为对象引用。 UiObject包含的信息可帮助它在运行时根据其构造函数中指定的UiSelector属性找到匹配的视图。 一旦你创建了一个UiObject的实例,它可以被重复用于匹配选择器条件的不同视图。
Constants |
|
|---|---|
int |
FINGER_TOUCH_HALF_WIDTH |
int |
SWIPE_MARGIN_LIMIT |
long |
WAIT_FOR_EVENT_TMEOUT 这个常数已被弃用。 使用 |
long |
WAIT_FOR_SELECTOR_POLL |
long |
WAIT_FOR_SELECTOR_TIMEOUT 这个常数已被弃用。 使用 |
long |
WAIT_FOR_WINDOW_TMEOUT |
Public constructors |
|
|---|---|
UiObject(UiSelector selector) 此构造函数已弃用。 改为使用 |
|
Public methods |
|
|---|---|
void |
clearTextField() 清除可编辑字段中的现有文本内容。 |
boolean |
click() 在由此UiObject表示的UI元素的可见边界的中心执行单击。 |
boolean |
clickAndWaitForNewWindow() 等待通常需要比通常默认超时更长的窗口转换。 |
boolean |
clickAndWaitForNewWindow(long timeout) 在由此UiObject表示的UI元素的可见边界的中心执行单击操作,并等待窗口转换。 |
boolean |
clickBottomRight() 点击UI元素的右下角 |
boolean |
clickTopLeft() 点击UI元素的左上角 |
boolean |
dragTo(UiObject destObj, int steps) 将此对象拖到目标UiObject。 |
boolean |
dragTo(int destX, int destY, int steps) 将该对象拖到任意坐标。 |
boolean |
exists() 检查视图是否存在。 |
Rect |
getBounds() 返回视图的 |
UiObject |
getChild(UiSelector selector) 为当前UiObject下的子视图创建一个新的UiObject。 |
int |
getChildCount() 立即计算当前UiObject下的子视图。 |
String |
getClassName() 检索UI元素的 |
String |
getContentDescription() 读取UI元素的 |
UiObject |
getFromParent(UiSelector selector) 相对于当前的UiObject,为同级视图或兄弟视图的子级创建新的UiObject。 |
String |
getPackageName() 读取视图的 |
final UiSelector |
getSelector() 调试助手。 |
String |
getText() 读取UI元素的 |
Rect |
getVisibleBounds() 返回视图的可见边界。 |
boolean |
isCheckable() 检查UI元素的 |
boolean |
isChecked() 检查UI元素的 |
boolean |
isClickable() 检查UI元素的 |
boolean |
isEnabled() 检查UI元素的 |
boolean |
isFocusable() 检查UI元素的 |
boolean |
isFocused() 检查UI元素的 |
boolean |
isLongClickable() 检查视图的 |
boolean |
isScrollable() 检查视图的 |
boolean |
isSelected() 检查UI元素的 |
boolean |
longClick() 长按一下UI元素可见边界的中心 |
boolean |
longClickBottomRight() 长按UI元素的右下角 |
boolean |
longClickTopLeft() 长按一下UI元素的左上角 |
boolean |
performMultiPointerGesture(PointerCoords... touches) 执行多点触控手势。 |
boolean |
performTwoPointerGesture(Point startPoint1, Point startPoint2, Point endPoint1, Point endPoint2, int steps) 生成任意起点和终点的双指手势。 |
boolean |
pinchIn(int percent, int steps) 执行一个双指针手势,每个指针从对角线向另一个对象移动,从边缘到这个UiObject的中心。 |
boolean |
pinchOut(int percent, int steps) 执行一个双指针手势,每个指针在对角线上相对移动,从中心向外朝向此UiObject的边缘。 |
boolean |
setText(String text) 清除字段内容后,在可编辑字段中设置文本。 |
boolean |
swipeDown(int steps) 对UiObject执行滑动操作。 |
boolean |
swipeLeft(int steps) 在UiObject上执行滑动左操作。 |
boolean |
swipeRight(int steps) 对UiObject执行滑动右键操作。 |
boolean |
swipeUp(int steps) 对UiObject执行滑动操作。 |
boolean |
waitForExists(long timeout) 等待指定的时间长度以使视图变为可见。 |
boolean |
waitUntilGone(long timeout) 等待指定的时间长度以使视图变得无法检测。 |
Protected methods |
|
|---|---|
AccessibilityNodeInfo |
findAccessibilityNodeInfo(long timeout) 通过使用此UiObject的选择器在可访问性层次结构中查找匹配的UI元素。 |
Inherited methods |
|
|---|---|
java.lang.Object
|
|
int FINGER_TOUCH_HALF_WIDTH
常量值:20(0x00000014)
int SWIPE_MARGIN_LIMIT
常量值:5(0x00000005)
long WAIT_FOR_EVENT_TMEOUT
这个常数已被弃用。
使用setScrollAcknowledgmentTimeout(long)
常量值:3000(0x0000000000000bb8)
long WAIT_FOR_SELECTOR_POLL
常量值:1000(0x00000000000003e8)
long WAIT_FOR_SELECTOR_TIMEOUT
这个常数已被弃用。
使用setWaitForSelectorTimeout(long)
常量值:10000(0x0000000000002710)
long WAIT_FOR_WINDOW_TMEOUT
常量值:5500(0x000000000000157c)
UiObject (UiSelector selector)
此构造函数已弃用。
改为使用findObject(UiSelector) 。 这个版本隐藏了UiObject对UiDevice的依赖,并且容易被滥用。
构造一个UiObject来表示一个匹配指定选择器条件的视图。
void clearTextField ()
清除可编辑字段中的现有文本内容。 该对象的UiSelector必须引用可编辑的UI元素。 调用此方法时,该方法将焦点置于可编辑字段上,选择其所有现有内容,并通过发送DELETE键来清除它
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean click ()
在由此UiObject表示的UI元素的可见边界的中心执行单击。
| Returns | |
|---|---|
boolean |
true id successful else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean clickAndWaitForNewWindow ()
等待通常需要比通常默认超时更长的窗口转换。 见clickAndWaitForNewWindow(long)
| Returns | |
|---|---|
boolean |
true if the event was triggered, else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean clickAndWaitForNewWindow (long timeout)
在由此UiObject表示的UI元素的可见边界的中心执行单击操作,并等待窗口转换。 该方法与click()不同之处仅在于,该方法由于点击而等待新的窗口转换。 窗口转换的一些示例:
| Parameters | |
|---|---|
timeout |
long: timeout before giving up on waiting for a new window |
| Returns | |
|---|---|
boolean |
true if the event was triggered, else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean clickBottomRight ()
点击UI元素的右下角
| Returns | |
|---|---|
boolean |
true on success |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean clickTopLeft ()
点击UI元素的左上角
| Returns | |
|---|---|
boolean |
true on success |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean dragTo (UiObject destObj, int steps)
将此对象拖到目标UiObject。 输入参数中指定的步数可能会影响拖动速度,而速度的变化可能会影响结果。 考虑在测试中使用此方法时评估不同的速度。
| Parameters | |
|---|---|
destObj |
UiObject: the destination UiObject. |
steps |
int: usually 40 steps. You can increase or decrease the steps to change the speed. |
| Returns | |
|---|---|
boolean |
true if successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean dragTo (int destX,
int destY,
int steps)
将该对象拖到任意坐标。 输入参数中指定的步数可能会影响拖动速度,而速度的变化可能会影响结果。 考虑在测试中使用此方法时评估不同的速度。
| Parameters | |
|---|---|
destX |
int: the X-axis coordinate. |
destY |
int: the Y-axis coordinate. |
steps |
int: usually 40 steps. You can increase or decrease the steps to change the speed. |
| Returns | |
|---|---|
boolean |
true if successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean exists ()
检查视图是否存在。 此方法执行waitForExists(long)零超时。 这基本上会立即返回是否存在由此UiObject表示的视图。 如果您需要等待更长的时间,请参阅waitForExists(long) 。
| Returns | |
|---|---|
boolean |
true if the view represented by this UiObject does exist |
Rect getBounds ()
返回视图的bounds属性。 见getVisibleBounds()
| Returns | |
|---|---|
Rect |
Rect |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
UiObject getChild (UiSelector selector)
为当前UiObject下的子视图创建一个新的UiObject。
| Parameters | |
|---|---|
selector |
UiSelector: for child view to match |
| Returns | |
|---|---|
UiObject |
a new UiObject representing the child view |
| Throws | |
|---|---|
UiObjectNotFoundException |
|
int getChildCount ()
立即计算当前UiObject下的子视图。
| Returns | |
|---|---|
int |
the count of child views. |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
String getClassName ()
检索UI元素的 className属性。
| Returns | |
|---|---|
String |
class name of the current node represented by this UiObject |
| Throws | |
|---|---|
UiObjectNotFoundException |
if no match was found |
String getContentDescription ()
读取UI元素的 content_desc属性
| Returns | |
|---|---|
String |
value of node attribute "content_desc" |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
UiObject getFromParent (UiSelector selector)
相对于当前的UiObject,为同级视图或兄弟视图的子级创建新的UiObject。
| Parameters | |
|---|---|
selector |
UiSelector: for a sibling view or children of the sibling view |
| Returns | |
|---|---|
UiObject |
a new UiObject representing the matched view |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
String getPackageName ()
读取视图的 package属性
| Returns | |
|---|---|
String |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
UiSelector getSelector ()
调试助手。 如果需要,测试可以将选择器的属性作为字符串转储到其日志中。 getSelector().toString();
| Returns | |
|---|---|
UiSelector |
UiSelector |
String getText ()
读取UI元素的 text属性
| Returns | |
|---|---|
String |
text value of the current node represented by this UiObject |
| Throws | |
|---|---|
UiObjectNotFoundException |
if no match could be found |
Rect getVisibleBounds ()
返回视图的可见边界。 如果视图的一部分可见,则仅报告可视部分的边界。
| Returns | |
|---|---|
Rect |
Rect |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
也可以看看:
boolean isCheckable ()
检查UI元素的 checkable属性当前是否为真。
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean isChecked ()
检查UI元素的 checked属性当前是否为真
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
UiObjectNotFoundException |
|
boolean isClickable ()
检查UI元素的 clickable属性当前是否为真。
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean isEnabled ()
检查UI元素的 enabled属性当前是否为真。
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean isFocusable ()
检查UI元素的 focusable属性当前是否为真。
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean isFocused ()
检查UI元素的 focused属性当前是否为真
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean isLongClickable ()
检查视图的 long-clickable属性当前是否为真
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean isScrollable ()
检查视图的 scrollable属性当前是否为真
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean isSelected ()
检查UI元素的 selected属性当前是否为真。
| Returns | |
|---|---|
boolean |
true if it is else false |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean longClick ()
长按一下UI元素可见边界的中心
| Returns | |
|---|---|
boolean |
true if operation was successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean longClickBottomRight ()
长按UI元素的右下角
| Returns | |
|---|---|
boolean |
true if operation was successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean longClickTopLeft ()
长按一下UI元素的左上角
| Returns | |
|---|---|
boolean |
true if operation was successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean performMultiPointerGesture (PointerCoords... touches)
执行多点触控手势。 您必须指定至少2个指针的触摸坐标。 每个指针必须具有在MotionEvent.PointerCoords的数组中定义的所有触摸步骤。 您可以使用此方法指定复杂的手势,如圆圈和不规则形状,其中每个指针可能采用不同的路径。 在指针的触摸路径上创建单个点: PointerCoords p = new PointerCoords(); p.x = stepX; p.y = stepY; p.pressure = 1; p.size = 1;
| Parameters | |
|---|---|
touches |
PointerCoords: represents the pointers' paths. Each MotionEvent.PointerCoords array represents a different pointer. Each MotionEvent.PointerCoords in an array element represents a touch point on a pointer's path. |
| Returns | |
|---|---|
boolean |
true if all touch events for this gesture are injected successfully, false otherwise |
boolean performTwoPointerGesture (Point startPoint1,
Point startPoint2,
Point endPoint1,
Point endPoint2,
int steps)
生成任意起点和终点的双指手势。
| Parameters | |
|---|---|
startPoint1 |
Point: start point of pointer 1 |
startPoint2 |
Point: start point of pointer 2 |
endPoint1 |
Point: end point of pointer 1 |
endPoint2 |
Point: end point of pointer 2 |
steps |
int: the number of steps for the gesture. Steps are injected about 5 milliseconds apart, so 100 steps may take around 0.5 seconds to complete. |
| Returns | |
|---|---|
boolean |
true if all touch events for this gesture are injected successfully, false otherwise |
boolean pinchIn (int percent,
int steps)
执行一个双指针手势,每个指针从对角线向另一个对象移动,从边缘到这个UiObject的中心。
| Parameters | |
|---|---|
percent |
int: percentage of the object's diagonal length for the pinch gesture |
steps |
int: the number of steps for the gesture. Steps are injected about 5 milliseconds apart, so 100 steps may take around 0.5 seconds to complete. |
| Returns | |
|---|---|
boolean |
true if all touch events for this gesture are injected successfully, false otherwise |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean pinchOut (int percent,
int steps)
执行一个双指针手势,每个指针在对角线上相对移动,从中心向外朝向此UiObject的边缘。
| Parameters | |
|---|---|
percent |
int: percentage of the object's diagonal length for the pinch gesture |
steps |
int: the number of steps for the gesture. Steps are injected about 5 milliseconds apart, so 100 steps may take around 0.5 seconds to complete. |
| Returns | |
|---|---|
boolean |
true if all touch events for this gesture are injected successfully, false otherwise |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean setText (String text)
清除字段内容后,在可编辑字段中设置文本。
此对象的 UiSelector选择器必须引用可编辑的UI元素。
调用此方法时,该方法将焦点置于可编辑字段上,清除其现有内容,然后将指定的文本注入字段。
如果您想捕获该字段的原始内容, getText()致电getText() 。 然后,您可以修改文本并使用此方法更新字段。
改进:发布API级别19(KitKat发布),底层实现更新为专用的设置文本可访问性操作,现在它也支持Unicode。
| Parameters | |
|---|---|
text |
String: string to set |
| Returns | |
|---|---|
boolean |
true if operation is successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean swipeDown (int steps)
对UiObject执行滑动操作。 轻扫手势可以在任何表面上执行。 目标UI元素不需要可滚动。 也可以看看:
| Parameters | |
|---|---|
steps |
int: indicates the number of injected move steps into the system. Steps are injected about 5ms apart. So a 100 steps may take about 1/2 second to complete. |
| Returns | |
|---|---|
boolean |
true if successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean swipeLeft (int steps)
在UiObject上执行滑动左操作。 轻扫手势可以在任何表面上执行。 目标UI元素不需要可滚动。 也可以看看:
| Parameters | |
|---|---|
steps |
int: indicates the number of injected move steps into the system. Steps are injected about 5ms apart. So a 100 steps may take about 1/2 second to complete. |
| Returns | |
|---|---|
boolean |
true if successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean swipeRight (int steps)
对UiObject执行滑动右键操作。 轻扫手势可以在任何表面上执行。 目标UI元素不需要可滚动。 也可以看看:
| Parameters | |
|---|---|
steps |
int: indicates the number of injected move steps into the system. Steps are injected about 5ms apart. So a 100 steps may take about 1/2 second to complete. |
| Returns | |
|---|---|
boolean |
true if successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean swipeUp (int steps)
对UiObject执行滑动操作。 也可以看看:
| Parameters | |
|---|---|
steps |
int: indicates the number of injected move steps into the system. Steps are injected about 5ms apart. So a 100 steps may take about 1/2 second to complete. |
| Returns | |
|---|---|
boolean |
true of successful |
| Throws | |
|---|---|
|
UiObjectNotFoundException |
UiObjectNotFoundException |
|
boolean waitForExists (long timeout)
等待指定的时间长度以使视图变为可见。 此方法将一直等待,直到视图在显示屏上可见,或者直到超时。 您可以在您要选择的内容未立即显示的情况下使用此方法。
| Parameters | |
|---|---|
timeout |
long: the amount of time to wait (in milliseconds) |
| Returns | |
|---|---|
boolean |
true if the view is displayed, else false if timeout elapsed while waiting |
boolean waitUntilGone (long timeout)
等待指定的时间长度以使视图变得无法检测。 此方法将一直等待,直到视图不再匹配,或者直到超时过去。 当对象的UiSelector无法找到匹配项时,视图变得无法检测,因为该元素已更改其状态或不再显示。 尝试等待某些长时间操作竞争时可以使用此方法,例如下载大文件或连接到远程服务器。
| Parameters | |
|---|---|
timeout |
long: time to wait (in milliseconds) |
| Returns | |
|---|---|
boolean |
true if the element is gone before timeout elapsed, else false if timeout elapsed but a matching element is still found. |
AccessibilityNodeInfo findAccessibilityNodeInfo (long timeout)
通过使用此UiObject的选择器在可访问性层次结构中查找匹配的UI元素。
| Parameters | |
|---|---|
timeout |
long: in milliseconds |
| Returns | |
|---|---|
AccessibilityNodeInfo |
AccessibilityNodeInfo if found else null |