目录
使用UI Automator Viewer 进行元素定位
UiAutomator API
UI Automator 测试框架提供了一个 UiDevice
类,用于在运行目标应用的设备上访问和执行操作。您可以调用其方法来访问设备属性,如当前屏幕方向或显示屏尺寸。UiDevice
类还可让您执行如下操作:
- 改变设备的旋转。
- 按硬件键,如“音量调高按钮”。
- 按返回、主屏幕或菜单按钮。
- 打开通知栏。
- 截取当前窗口的屏幕截图。
例如,要模拟按主屏幕按钮的操作,请调用 UiDevice.pressHome()
方法。
来源: https://developer.android.google.cn/training/testing/ui-automator
UiCollection
:枚举容器的界面元素,目的是为了计数,或者按可见文本或内容说明属性来定位子元素。UiObject
:表示设备上可见的界面元素。UiScrollable
:支持搜索可滚动界面容器中的项目。UiSelector
:表示对设备上的一个或多个目标界面元素的查询。Configurator
:可让您设置用于运行 UI Automator 测试的关键参数。
UiDevice
来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiDevice
Public methods |
|
---|---|
void |
clearLastTraversedText() Clears the text from the last UI traversal event. |
boolean |
click(int x, int y) Perform a click at arbitrary coordinates specified by the user |
boolean |
drag(int startX, int startY, int endX, int endY, int steps) Performs a swipe from one coordinate to another coordinate. |
void |
dumpWindowHierarchy(File dest) Dump the current window hierarchy to a |
void |
dumpWindowHierarchy(OutputStream out) Dump the current window hierarchy to an |
void |
dumpWindowHierarchy(String fileName) This method is deprecated. Use |
UiObject |
findObject(UiSelector selector) Returns a UiObject which represents a view that matches the specified selector criteria. |
UiObject2 |
findObject(BySelector selector) Returns the first object to match the |
List<UiObject2> |
findObjects(BySelector selector) Returns all objects that match the |
void |
freezeRotation() Disables the sensors and freezes the device rotation at its current rotation state. |
String |
getCurrentActivityName() This method is deprecated. The results returned should be considered unreliable |
String |
getCurrentPackageName() Retrieves the name of the last package to report accessibility events. |
Uiselector
来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiSelector.html
UiSelector |
packageName(String name) Set the search criteria to match the package name of the application that contains the widget. |
UiSelector |
packageNameMatches(String regex) Set the search criteria to match the package name of the application that contains the widget. |
UiSelector |
resourceId(String id) Set the search criteria to match the given resource ID. |
UiSelector |
resourceIdMatches(String regex) Set the search criteria to match the resource ID of the widget, using a regular expression. |
UiSelector |
scrollable(boolean val) Set the search criteria to match widgets that are scrollable. |
UiSelector |
selected(boolean val) Set the search criteria to match widgets that are currently selected. |
UiSelector |
text(String text) Set the search criteria to match the visible text displayed in a widget (for example, the text label to launch an app). |
UiSelector |
textContains(String text) Set the search criteria to match the visible text in a widget where the visible text must contain the string in your input argument. |
UiSelector |
textMatches(String regex) Set the search criteria to match the visible text displayed in a layout element, using a regular expression. |
UiSelector |
textStartsWith(String text) Set the search criteria to match visible text in a widget that is prefixed by the text parameter. |
String |
toString() |
UiObject
来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiObject.html
Public methods |
|
---|---|
void |
clearTextField() Clears the existing text contents in an editable field. |
boolean |
click() Performs a click at the center of the visible bounds of the UI element represented by this UiObject. |
boolean |
clickAndWaitForNewWindow() Waits for window transitions that would typically take longer than the usual default timeouts. |
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. |
boolean |
clickBottomRight() Clicks the bottom and right corner of the UI element |
boolean |
clickTopLeft() Clicks the top and left corner of the UI element |
boolean |
dragTo(UiObject destObj, int steps) Drags this object to a destination UiObject. |
boolean |
dragTo(int destX, int destY, int steps) Drags this object to arbitrary coordinates. |
boolean |
exists() Check if view exists. |
Rect |
getBounds() Returns the view's |
UiObject |
getChild(UiSelector selector) Creates a new UiObject for a child view that is under the present UiObject. |
int |
getChildCount() Counts the child views immediately under the present UiObject. |
String |
getClassName() Retrieves the |
String |
getContentDescription() Reads the |
UiObject |
getFromParent(UiSelector selector) Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject. |
String |
getPackageName() Reads the view's |
final UiSelector |
getSelector() Debugging helper. |
String |
getText() Reads the |
UiCollection
boolean |
clickTopLeft() Clicks the top and left corner of the UI element |
boolean |
dragTo(UiObject destObj, int steps) Drags this object to a destination UiObject. |
boolean |
dragTo(int destX, int destY, int steps) Drags this object to arbitrary coordinates. |
boolean |
exists() Check if view exists. |
Rect |
getBounds() Returns the view's |
UiObject |
getChild(UiSelector selector) Creates a new UiObject for a child view that is under the present UiObject. |
int |
getChildCount() Counts the child views immediately under the present UiObject. |
String |
getClassName() Retrieves the |
String |
getContentDescription() Reads the |
UiObject |
getFromParent(UiSelector selector) Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject. |
String |
getPackageName() Reads the view's |
final UiSelector |
getSelector() Debugging helper. |
String |
getText() Reads the |
UiScrollable
来源: https://developer.android.google.cn/reference/androidx/test/uiautomator/UiScrollable.html
boolean |
scrollBackward() Performs a backward scroll with the default number of scroll steps (55). |
boolean |
scrollDescriptionIntoView(String text) Performs a forward scroll action on the scrollable layout element until the content-description is found, or until swipe attempts have been exhausted. |
boolean |
scrollForward(int steps) Performs a forward scroll. |
boolean |
scrollForward() Performs a forward scroll with the default number of scroll steps (55). |
boolean |
scrollIntoView(UiObject obj) Perform a forward scroll action to move through the scrollable layout element until a visible item that matches the |
boolean |
scrollIntoView(UiSelector selector) Perform a scroll forward action to move through the scrollable layout element until a visible item that matches the selector is found. |
boolean |
scrollTextIntoView(String text) Performs a forward scroll action on the scrollable layout element until the text you provided is visible, or until swipe attempts have been exhausted. |
boolean |
scrollToBeginning(int maxSwipes) Scrolls to the beginning of a scrollable layout element. |
boolean |
scrollToBeginning(int maxSwipes, int steps) Scrolls to the beginning of a scrollable layout element. |
boolean |
scrollToEnd(int maxSwipes) Scrolls to the end of a scrollable layout element. |
boolean |
scrollToEnd(int maxSwipes, int steps) Scrolls to the end of a scrollable layout element. |
UiScrollable |
setAsHorizontalList() Set the direction of swipes to be horizontal when performing scroll actions. |
UiScrollable |
setAsVerticalList() Set the direction of swipes to be vertical when performing scroll actions. |