public final class UiAutomation 
 extends Object 
| java.lang.Object | |
| ↳ | android.app.UiAutomation | 
通过模拟用户操作和反省屏幕内容来与设备的用户界面进行交互的类。 它依靠平台可访问性API来内省屏幕并在远程视图树上执行一些操作。 它还允许注入任意原始输入事件,模拟用户与键盘和触摸设备的交互。 人们可以将UiAutomation视为一种特殊类型的AccessibilityService ,它不提供服务生命周期的钩子,并公开其他对UI测试自动化有用的API。
此类暴露的API在开发UI测试自动化工具和库时是低级别的,以最大限度地提高灵活性。 通常,UiAutomation客户端应使用更高级别的库或实现高级功能。 例如,在屏幕上执行敲击操作需要构建和注入触摸事件,这些事件必须通过致电injectInputEvent(InputEvent, boolean)传送到系统。
此类公开的API可跨应用程序运行,使客户端可以编写涵盖跨多个应用程序的使用案例的测试。 例如,转到设置应用程序以更改设置,然后与行为取决于该设置的其他应用程序进行交互。
| Nested classes | |
|---|---|
|  interface | UiAutomation.AccessibilityEventFilter监听器用于过滤可访问性事件。 | 
|  interface | UiAutomation.OnAccessibilityEventListener听众观察 | 
| Constants | |
|---|---|
| int | FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICESUiAutomation默认禁止无障碍服务。 | 
| int | ROTATION_FREEZE_0旋转常数:将旋转锁定到0度(自然方向) | 
| int | ROTATION_FREEZE_180旋转常数:将旋转冻结至180度。 | 
| int | ROTATION_FREEZE_270旋转常数:将旋转冻结至270度。 | 
| int | ROTATION_FREEZE_90旋转常数:将旋转冻结至90度。 | 
| int | ROTATION_FREEZE_CURRENT旋转常数:将旋转冻结到当前状态。 | 
| int | ROTATION_UNFREEZE旋转常数:解冻旋转(旋转设备改变其旋转状态)。 | 
| Inherited methods | |
|---|---|
|  From class  java.lang.Object  | |
int FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES
UiAutomation默认禁止无障碍服务。 此标志指定现有的无障碍服务应该继续运行,并且可能会开始新的无障碍服务。 从getUiAutomation(int)获取UiAutomation时设置此标志。
常数值:1(0x00000001)
int ROTATION_FREEZE_180
旋转常数:将旋转冻结至180度。
常量值:2(0x00000002)
int ROTATION_FREEZE_270
旋转常数:将旋转冻结至270度。
常量值:3(0x00000003)
int ROTATION_FREEZE_CURRENT
旋转常数:将旋转冻结到当前状态。
常量值:-1(0xffffffff)
int ROTATION_UNFREEZE
旋转常数:解冻旋转(旋转设备改变其旋转状态)。
常量值:-2(0xfffffffe)
void clearWindowAnimationFrameStats ()
清除窗口动画渲染统计信息。 这些统计信息包含有关最近呈现的窗口动画帧的信息,即用于窗口过渡动画。
boolean clearWindowContentFrameStats (int windowId)
清除给定窗口内容的帧统计信息。 这些统计信息包含有关最近呈现的内容框架的信息。
| Parameters | |
|---|---|
| windowId | int: The window id. | 
| Returns | |
|---|---|
| boolean | Whether the window is present and its frame statistics were cleared. | 
AccessibilityEvent executeAndWaitForEvent (Runnable command, UiAutomation.AccessibilityEventFilter filter, long timeoutMillis)
执行命令并等待特定的辅助功能事件达到给定的等待超时。 为了检测事件序列,可以实现一个过滤器,该过滤器跟踪预期序列的可见事件,并在接收到该序列的最后一个事件后返回true。
注意:回收所返回的事件是主叫方的责任。
| Parameters | |
|---|---|
| command | Runnable: The command to execute. | 
| filter | UiAutomation.AccessibilityEventFilter: Filter that recognizes the expected event. | 
| timeoutMillis | long: The wait timeout in milliseconds. | 
| Returns | |
|---|---|
| AccessibilityEvent | |
| Throws | |
|---|---|
| TimeoutException | If the expected event is not received within the timeout. | 
ParcelFileDescriptor executeShellCommand (String command)
执行一个shell命令。 此方法会修改指向标准输出流的文件描述符。 命令执行类似于运行“adb shell” 
注意:一旦完成读取,您有责任关闭重新调优的文件描述符。
| Parameters | |
|---|---|
| command | String: The command to execute. | 
| Returns | |
|---|---|
| ParcelFileDescriptor | A file descriptor to the standard output stream. | 
AccessibilityNodeInfo findFocus (int focus)
找到具有指定焦点类型的视图。 搜索跨所有窗口执行。
注意:为了访问窗口,您必须选择通过设置FLAG_RETRIEVE_INTERACTIVE_WINDOWS标志来检索交互式窗口。 否则,搜索将仅在活动窗口中执行。
| Parameters | |
|---|---|
| focus | int: The focus to find. One ofFOCUS_INPUTorFOCUS_ACCESSIBILITY. | 
| Returns | |
|---|---|
| AccessibilityNodeInfo | The node info of the focused view or null. | 
也可以看看:
AccessibilityNodeInfo getRootInActiveWindow ()
获取活动窗口中的根 AccessibilityNodeInfo 。
| Returns | |
|---|---|
| AccessibilityNodeInfo | The root info. | 
AccessibilityServiceInfo getServiceInfo ()
获取一个AccessibilityServiceInfo描述此UiAutomation。 如果想在运行时更改某些动态可配置属性,此方法非常有用。
| Returns | |
|---|---|
| AccessibilityServiceInfo | The accessibility service info. | 
也可以看看:
WindowAnimationFrameStats getWindowAnimationFrameStats ()
获取窗口动画帧统计信息。 这些统计信息包含有关最近呈现的窗口动画帧的信息,即用于窗口过渡动画。
一个典型的用法需要通过 clearWindowAnimationFrameStats()清除窗口动画帧统计信息,然后进行交互,导致窗口转换使用窗口动画,最后通过调用此方法获取窗口动画帧统计信息。
// Start with a clean slate. uiAutimation.clearWindowAnimationFrameStats(); // Do stuff to trigger a window transition. // Get the frame statistics. WindowAnimationFrameStats stats = uiAutomation.getWindowAnimationFrameStats();
| Returns | |
|---|---|
| WindowAnimationFrameStats | The window animation frame statistics. | 
WindowContentFrameStats getWindowContentFrameStats (int windowId)
获取给定窗口的帧统计信息。 这些统计信息包含有关最近呈现的内容框架的信息。
一个典型的用法需要通过 clearWindowContentFrameStats(int)清除窗口框架统计信息,然后与UI进行交互,最后通过调用此方法获取窗口框架统计信息。
// Assume we have at least one window. final int windowId = getWindows().get(0).getId(); // Start with a clean slate. uiAutimation.clearWindowContentFrameStats(windowId); // Do stuff with the UI. // Get the frame statistics. WindowContentFrameStats stats = uiAutomation.getWindowContentFrameStats(windowId);
| Parameters | |
|---|---|
| windowId | int: The window id. | 
| Returns | |
|---|---|
| WindowContentFrameStats | The window frame statistics, or null if the window is not present. | 
List<AccessibilityWindowInfo> getWindows ()
获取屏幕上的窗口。 该方法仅返回有视力的用户可以与之交互的窗口,而不是所有的窗口。 例如,如果显示的是模态对话框,并且用户无法触摸它后面的任何对话框,则只会报告模态窗口(假设它是最高的窗口)。 为了方便起见,返回的窗口以降序图层顺序排列,这是首先报告Z顺序较高的窗口。
注意:要访问窗口,您必须选择通过设置 FLAG_RETRIEVE_INTERACTIVE_WINDOWS标志来检索交互式窗口。
| Returns | |
|---|---|
| List<AccessibilityWindowInfo> | The windows if there are windows such, otherwise an empty list. | 
boolean injectInputEvent (InputEvent event, boolean sync)
一种注入任意输入事件的方法。
Note: It is caller's responsibility to recycle the event.
| Parameters | |
|---|---|
| event | InputEvent: The event to inject. | 
| sync | boolean: Whether to inject the event synchronously. | 
| Returns | |
|---|---|
| boolean | Whether event injection succeeded. | 
boolean performGlobalAction (int action)
执行全局操作。 无论当前应用程序或用户在该应用程序中的位置如何,都可以随时执行此类操作。 例如回去,回家,打开最近的东西等等。
| Parameters | |
|---|---|
| action | int: The action to perform. | 
| Returns | |
|---|---|
| boolean | Whether the action was successfully performed. | 
void setOnAccessibilityEventListener (UiAutomation.OnAccessibilityEventListener listener)
设置观察 AccessibilityEvent的流的 AccessibilityEvent 。
| Parameters | |
|---|---|
| listener | UiAutomation.OnAccessibilityEventListener: The callback. | 
boolean setRotation (int rotation)
设置设备旋转。 客户端可以冻结所需状态的旋转或将旋转冻结到当前状态或解冻旋转(旋转设备更改其旋转状态)。
| Parameters | |
|---|---|
| rotation | int: The desired rotation. | 
| Returns | |
|---|---|
| boolean | Whether the rotation was set successfully. | 
void setRunAsMonkey (boolean enable)
设置此UiAutomation是否以“猴子”模式运行。 应用程序可以查询它们是否以“猴子”模式执行,即通过测试框架运行,并避免执行可能不需要的操作,例如调用911或在公共论坛上发布等。
| Parameters | |
|---|---|
| enable | boolean: whether to run in a "monkey" mode or not. Default is not. | 
也可以看看:
void setServiceInfo (AccessibilityServiceInfo info)
设置 AccessibilityServiceInfo描述如何UiAutomation将通过该平台可访问层进行处理。
| Parameters | |
|---|---|
| info | AccessibilityServiceInfo: The info. | 
也可以看看:
Bitmap takeScreenshot ()
截图。
| Returns | |
|---|---|
| Bitmap | The screenshot bitmap on success, null otherwise. | 
void waitForIdle (long idleTimeoutMillis, 
                long globalTimeoutMillis) 
     等待可访问性事件idleTimeoutMillis为空闲状态,这不会在idleTimeoutMillis内收到可访问性事件。 等待空闲可访问性事件流花费的总时间由globalTimeoutMillis 。
| Parameters | |
|---|---|
| idleTimeoutMillis | long: The timeout in milliseconds between two events to consider the device idle. | 
| globalTimeoutMillis | long: The maximal global timeout in milliseconds in which to wait for an idle state. | 
| Throws | |
|---|---|
| TimeoutException | If no idle state was detected within globalTimeoutMillis. |