java.lang.Object | |
↳ | android.app.UiAutomation |
Class for interacting with the device's UI by simulation user actions and
introspection of the screen content. It relies on the platform accessibility
APIs to introspect the screen and to perform some actions on the remote view
tree. It also allows injecting of arbitrary raw input events simulating user
interaction with keyboards and touch devices. One can think of a UiAutomation
as a special type of AccessibilityService
which does not provide hooks for the service life cycle and exposes other
APIs that are useful for UI test automation.
The APIs exposed by this class are low-level to maximize flexibility when
developing UI test automation tools and libraries. Generally, a UiAutomation
client should be using a higher-level library or implement high-level functions.
For example, performing a tap on the screen requires construction and injecting
of a touch down and up events which have to be delivered to the system by a
call to injectInputEvent(InputEvent, boolean)
.
The APIs exposed by this class operate across applications enabling a client to write tests that cover use cases spanning over multiple applications. For example, going to the settings application to change a setting and then interacting with another application whose behavior depends on that setting.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
UiAutomation.AccessibilityEventFilter | Listener for filtering accessibility events. | ||||||||||
UiAutomation.OnAccessibilityEventListener |
Listener for observing the AccessibilityEvent stream.
|
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | ROTATION_FREEZE_0 | Rotation constant: Freeze rotation to 0 degrees (natural orientation) | |||||||||
int | ROTATION_FREEZE_180 | Rotation constant: Freeze rotation to 180 degrees . | |||||||||
int | ROTATION_FREEZE_270 | Rotation constant: Freeze rotation to 270 degrees . | |||||||||
int | ROTATION_FREEZE_90 | Rotation constant: Freeze rotation to 90 degrees . | |||||||||
int | ROTATION_FREEZE_CURRENT | Rotation constant: Freeze rotation to its current state. | |||||||||
int | ROTATION_UNFREEZE | Rotation constant: Unfreeze rotation (rotating the device changes its rotation state). |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Clears the window animation rendering statistics.
| |||||||||||
Clears the frame statistics for the content of a given window.
| |||||||||||
Executes a command and waits for a specific accessibility event up to a
given wait timeout.
| |||||||||||
Executes a shell command.
| |||||||||||
Find the view that has the specified focus type.
| |||||||||||
Gets the root
AccessibilityNodeInfo in the active window.
| |||||||||||
Gets the an
AccessibilityServiceInfo describing this UiAutomation.
| |||||||||||
Gets the window animation frame statistics.
| |||||||||||
Gets the frame statistics for a given window.
| |||||||||||
Gets the windows on the screen.
| |||||||||||
A method for injecting an arbitrary input event.
| |||||||||||
Performs a global action.
| |||||||||||
Sets a callback for observing the stream of
AccessibilityEvent s.
| |||||||||||
Sets the device rotation.
| |||||||||||
Sets whether this UiAutomation to run in a "monkey" mode.
| |||||||||||
Sets the
AccessibilityServiceInfo that describes how this
UiAutomation will be handled by the platform accessibility layer.
| |||||||||||
Takes a screenshot.
| |||||||||||
Waits for the accessibility event stream to become idle, which is not to
have received an accessibility event within
idleTimeoutMillis .
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Rotation constant: Freeze rotation to 0 degrees (natural orientation)
Rotation constant: Freeze rotation to 180 degrees .
Rotation constant: Freeze rotation to 270 degrees .
Rotation constant: Freeze rotation to 90 degrees .
Rotation constant: Freeze rotation to its current state.
Rotation constant: Unfreeze rotation (rotating the device changes its rotation state).
Clears the window animation rendering statistics. These statistics contain information about the most recently rendered window animation frames, i.e. for window transition animations.
Clears the frame statistics for the content of a given window. These statistics contain information about the most recently rendered content frames.
windowId | The window id. |
---|
Executes a command and waits for a specific accessibility event up to a given wait timeout. To detect a sequence of events one can implement a filter that keeps track of seen events of the expected sequence and returns true after the last event of that sequence is received.
Note: It is caller's responsibility to recycle the returned event.
command | The command to execute. |
---|---|
filter | Filter that recognizes the expected event. |
timeoutMillis | The wait timeout in milliseconds. |
TimeoutException | If the expected event is not received within the timeout. |
---|
Executes a shell command. This method returs a file descriptor that points
to the standard output stream. The command execution is similar to running
"adb shell
Note: It is your responsibility to close the retunred file
descriptor once you are done reading.
command | The command to execute. |
---|
Find the view that has the specified focus type. The search is performed across all windows.
Note: In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
FLAG_RETRIEVE_INTERACTIVE_WINDOWS
flag.
Otherwise, the search will be performed only in the active window.
focus | The focus to find. One of FOCUS_INPUT or
FOCUS_ACCESSIBILITY . |
---|
Gets the root AccessibilityNodeInfo
in the active window.
Gets the an AccessibilityServiceInfo
describing this UiAutomation.
This method is useful if one wants to change some of the dynamically
configurable properties at runtime.
Gets the window animation frame statistics. These statistics contain information about the most recently rendered window animation frames, i.e. for window transition animations.
A typical usage requires clearing the window animation frame statistics via
clearWindowAnimationFrameStats()
followed by an interaction that causes
a window transition which uses a window animation and finally getting the window
animation frame statistics by calling this method.
// Start with a clean slate. uiAutimation.clearWindowAnimationFrameStats(); // Do stuff to trigger a window transition. // Get the frame statistics. WindowAnimationFrameStats stats = uiAutomation.getWindowAnimationFrameStats();
Gets the frame statistics for a given window. These statistics contain information about the most recently rendered content frames.
A typical usage requires clearing the window frame statistics via clearWindowContentFrameStats(int)
followed by an interaction with the UI and
finally getting the window frame statistics via calling this method.
// 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);
windowId | The window id. |
---|
Gets the windows on the screen. This method returns only the windows that a sighted user can interact with, as opposed to all windows. For example, if there is a modal dialog shown and the user cannot touch anything behind it, then only the modal window will be reported (assuming it is the top one). For convenience the returned windows are ordered in a descending layer order, which is the windows that are higher in the Z-order are reported first.
Note: In order to access the windows you have to opt-in
to retrieve the interactive windows by setting the
FLAG_RETRIEVE_INTERACTIVE_WINDOWS
flag.
A method for injecting an arbitrary input event.
Note: It is caller's responsibility to recycle the event.
event | The event to inject. |
---|---|
sync | Whether to inject the event synchronously. |
Performs a global action. Such an action can be performed at any moment regardless of the current application or user location in that application. For example going back, going home, opening recents, etc.
action | The action to perform. |
---|
Sets a callback for observing the stream of AccessibilityEvent
s.
listener | The callback. |
---|
Sets the device rotation. A client can freeze the rotation in desired state or freeze the rotation to its current state or unfreeze the rotation (rotating the device changes its rotation state).
rotation | The desired rotation. |
---|
Sets whether this UiAutomation to run in a "monkey" mode. Applications can query whether they are executed in a "monkey" mode, i.e. run by a test framework, and avoid doing potentially undesirable actions such as calling 911 or posting on public forums etc.
enable | whether to run in a "monkey" mode or not. Default is not. |
---|
Sets the AccessibilityServiceInfo
that describes how this
UiAutomation will be handled by the platform accessibility layer.
info | The info. |
---|
Takes a screenshot.
Waits for the accessibility event stream to become idle, which is not to
have received an accessibility event within idleTimeoutMillis
.
The total time spent to wait for an idle accessibility event stream is bounded
by the globalTimeoutMillis
.
idleTimeoutMillis | The timeout in milliseconds between two events to consider the device idle. |
---|---|
globalTimeoutMillis | The maximal global timeout in milliseconds in which to wait for an idle state. |
TimeoutException | If no idle state was detected within
globalTimeoutMillis.
|
---|