Methods Summary |
---|
public void | clearLastTraversedText()Clears the text from the last UI traversal event.
See {@link #getLastTraversedText()}.
Tracer.trace();
getAutomatorBridge().getQueryController().clearLastTraversedText();
|
public boolean | click(int x, int y)Perform a click at arbitrary coordinates specified by the user
Tracer.trace(x, y);
if (x >= getDisplayWidth() || y >= getDisplayHeight()) {
return (false);
}
return getAutomatorBridge().getInteractionController().clickNoSync(x, y);
|
public boolean | drag(int startX, int startY, int endX, int endY, int steps)Performs a swipe from one coordinate to another coordinate. You can control
the smoothness and speed of the swipe by specifying the number of steps.
Each step execution is throttled to 5 milliseconds per step, so for a 100
steps, the swipe will take around 0.5 seconds to complete.
Tracer.trace(startX, startY, endX, endY, steps);
return getAutomatorBridge().getInteractionController()
.swipe(startX, startY, endX, endY, steps, true);
|
public void | dumpWindowHierarchy(java.lang.String fileName)Helper method used for debugging to dump the current window's layout hierarchy.
The file root location is /data/local/tmp
Tracer.trace(fileName);
AccessibilityNodeInfo root =
getAutomatorBridge().getQueryController().getAccessibilityRootNode();
if(root != null) {
Display display = getAutomatorBridge().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
AccessibilityNodeInfoDumper.dumpWindowToFile(root,
new File(new File(Environment.getDataDirectory(), "local/tmp"), fileName),
display.getRotation(), size.x, size.y);
}
|
public void | freezeRotation()Disables the sensors and freezes the device rotation at its
current rotation state.
Tracer.trace();
getAutomatorBridge().getInteractionController().freezeRotation();
|
UiAutomatorBridge | getAutomatorBridge()Provides access the {@link QueryController} and {@link InteractionController}
if (mUiAutomationBridge == null) {
throw new RuntimeException("UiDevice not initialized");
}
return mUiAutomationBridge;
|
public java.lang.String | getCurrentActivityName()Retrieves the last activity to report accessibility events.
Tracer.trace();
return getAutomatorBridge().getQueryController().getCurrentActivityName();
|
public java.lang.String | getCurrentPackageName()Retrieves the name of the last package to report accessibility events.
Tracer.trace();
return getAutomatorBridge().getQueryController().getCurrentPackageName();
|
public int | getDisplayHeight()Gets the height of the display, in pixels. The size is adjusted based
on the current orientation of the display.
Tracer.trace();
Display display = getAutomatorBridge().getDefaultDisplay();
Point p = new Point();
display.getSize(p);
return p.y;
|
public int | getDisplayRotation()Returns the current rotation of the display, as defined in {@link Surface}
Tracer.trace();
waitForIdle();
return getAutomatorBridge().getRotation();
|
public android.graphics.Point | getDisplaySizeDp()Returns the display size in dp (device-independent pixel)
The returned display size is adjusted per screen rotation. Also this will return the actual
size of the screen, rather than adjusted per system decorations (like status bar).
Tracer.trace();
Display display = getAutomatorBridge().getDefaultDisplay();
Point p = new Point();
display.getRealSize(p);
DisplayMetrics metrics = new DisplayMetrics();
display.getRealMetrics(metrics);
float dpx = p.x / metrics.density;
float dpy = p.y / metrics.density;
p.x = Math.round(dpx);
p.y = Math.round(dpy);
return p;
|
public int | getDisplayWidth()Gets the width of the display, in pixels. The width and height details
are reported based on the current orientation of the display.
Tracer.trace();
Display display = getAutomatorBridge().getDefaultDisplay();
Point p = new Point();
display.getSize(p);
return p.x;
|
public static com.android.uiautomator.core.UiDevice | getInstance()Retrieves a singleton instance of UiDevice
if (sDevice == null) {
sDevice = new UiDevice();
}
return sDevice;
|
public java.lang.String | getLastTraversedText()Retrieves the text from the last UI traversal event received.
You can use this method to read the contents in a WebView container
because the accessibility framework fires events
as each text is highlighted. You can write a test to perform
directional arrow presses to focus on different elements inside a WebView,
and call this method to get the text from each traversed element.
If you are testing a view container that can return a reference to a
Document Object Model (DOM) object, your test should use the view's
DOM instead.
Tracer.trace();
return getAutomatorBridge().getQueryController().getLastTraversedText();
|
public java.lang.String | getProductName()Retrieves the product name of the device.
This method provides information on what type of device the test is running on. This value is
the same as returned by invoking #adb shell getprop ro.product.name.
Tracer.trace();
return Build.PRODUCT;
|
public boolean | hasAnyWatcherTriggered()Checks if any registered {@link UiWatcher} have triggered.
See {@link #registerWatcher(String, UiWatcher)}
See {@link #hasWatcherTriggered(String)}
Tracer.trace();
return mWatchersTriggers.size() > 0;
|
public boolean | hasWatcherTriggered(java.lang.String watcherName)Checks if a specific registered {@link UiWatcher} has triggered.
See {@link #registerWatcher(String, UiWatcher)}. If a UiWatcher runs and its
{@link UiWatcher#checkForCondition()} call returned true , then
the UiWatcher is considered triggered. This is helpful if a watcher is detecting errors
from ANR or crash dialogs and the test needs to know if a UiWatcher has been triggered.
Tracer.trace(watcherName);
return mWatchersTriggers.contains(watcherName);
|
public void | initialize(UiAutomatorBridge uiAutomatorBridge)
mUiAutomationBridge = uiAutomatorBridge;
|
boolean | isInWatcherContext()
return mInWatcherContext;
|
public boolean | isNaturalOrientation()Check if the device is in its natural orientation. This is determined by checking if the
orientation is at 0 or 180 degrees.
Tracer.trace();
waitForIdle();
int ret = getAutomatorBridge().getRotation();
return ret == UiAutomation.ROTATION_FREEZE_0 ||
ret == UiAutomation.ROTATION_FREEZE_180;
|
public boolean | isScreenOn()Checks the power manager if the screen is ON.
Tracer.trace();
return getAutomatorBridge().getInteractionController().isScreenOn();
|
public boolean | openNotification()Opens the notification shade.
Tracer.trace();
waitForIdle();
return getAutomatorBridge().getInteractionController().openNotification();
|
public boolean | openQuickSettings()Opens the Quick Settings shade.
Tracer.trace();
waitForIdle();
return getAutomatorBridge().getInteractionController().openQuickSettings();
|
public boolean | pressBack()Simulates a short press on the BACK button.
Tracer.trace();
waitForIdle();
return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent(
KeyEvent.KEYCODE_BACK, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
KEY_PRESS_EVENT_TIMEOUT);
|
public boolean | pressDPadCenter()Simulates a short press on the CENTER button.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_DPAD_CENTER);
|
public boolean | pressDPadDown()Simulates a short press on the DOWN button.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_DPAD_DOWN);
|
public boolean | pressDPadLeft()Simulates a short press on the LEFT button.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_DPAD_LEFT);
|
public boolean | pressDPadRight()Simulates a short press on the RIGHT button.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_DPAD_RIGHT);
|
public boolean | pressDPadUp()Simulates a short press on the UP button.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_DPAD_UP);
|
public boolean | pressDelete()Simulates a short press on the DELETE key.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_DEL);
|
public boolean | pressEnter()Simulates a short press on the ENTER key.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_ENTER);
|
public boolean | pressHome()Simulates a short press on the HOME button.
Tracer.trace();
waitForIdle();
return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent(
KeyEvent.KEYCODE_HOME, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
KEY_PRESS_EVENT_TIMEOUT);
|
public boolean | pressKeyCode(int keyCode)Simulates a short press using a key code.
See {@link KeyEvent}
Tracer.trace(keyCode);
waitForIdle();
return getAutomatorBridge().getInteractionController().sendKey(keyCode, 0);
|
public boolean | pressKeyCode(int keyCode, int metaState)Simulates a short press using a key code.
See {@link KeyEvent}.
Tracer.trace(keyCode, metaState);
waitForIdle();
return getAutomatorBridge().getInteractionController().sendKey(keyCode, metaState);
|
public boolean | pressMenu()Simulates a short press on the MENU button.
Tracer.trace();
waitForIdle();
return getAutomatorBridge().getInteractionController().sendKeyAndWaitForEvent(
KeyEvent.KEYCODE_MENU, 0, AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
KEY_PRESS_EVENT_TIMEOUT);
|
public boolean | pressRecentApps()Simulates a short press on the Recent Apps button.
Tracer.trace();
waitForIdle();
return getAutomatorBridge().getInteractionController().toggleRecentApps();
|
public boolean | pressSearch()Simulates a short press on the SEARCH button.
Tracer.trace();
return pressKeyCode(KeyEvent.KEYCODE_SEARCH);
|
public void | registerWatcher(java.lang.String name, UiWatcher watcher)Registers a {@link UiWatcher} to run automatically when the testing framework is unable to
find a match using a {@link UiSelector}. See {@link #runWatchers()}
Tracer.trace(name, watcher);
if (mInWatcherContext) {
throw new IllegalStateException("Cannot register new watcher from within another");
}
mWatchers.put(name, watcher);
|
public void | removeWatcher(java.lang.String name)Removes a previously registered {@link UiWatcher}.
See {@link #registerWatcher(String, UiWatcher)}
Tracer.trace(name);
if (mInWatcherContext) {
throw new IllegalStateException("Cannot remove a watcher from within another");
}
mWatchers.remove(name);
|
public void | resetWatcherTriggers()Resets a {@link UiWatcher} that has been triggered.
If a UiWatcher runs and its {@link UiWatcher#checkForCondition()} call
returned true , then the UiWatcher is considered triggered.
See {@link #registerWatcher(String, UiWatcher)}
Tracer.trace();
mWatchersTriggers.clear();
|
public void | runWatchers()This method forces all registered watchers to run.
See {@link #registerWatcher(String, UiWatcher)}
Tracer.trace();
if (mInWatcherContext) {
return;
}
for (String watcherName : mWatchers.keySet()) {
UiWatcher watcher = mWatchers.get(watcherName);
if (watcher != null) {
try {
mInWatcherContext = true;
if (watcher.checkForCondition()) {
setWatcherTriggered(watcherName);
}
} catch (Exception e) {
Log.e(LOG_TAG, "Exceuting watcher: " + watcherName, e);
} finally {
mInWatcherContext = false;
}
}
}
|
public void | setCompressedLayoutHeirarchy(boolean compressed)Enables or disables layout hierarchy compression.
If compression is enabled, the layout hierarchy derived from the Acessibility
framework will only contain nodes that are important for uiautomator
testing. Any unnecessary surrounding layout nodes that make viewing
and searching the hierarchy inefficient are removed.
getAutomatorBridge().setCompressedLayoutHierarchy(compressed);
|
public void | setOrientationLeft()Simulates orienting the device to the left and also freezes rotation
by disabling the sensors.
If you want to un-freeze the rotation and re-enable the sensors
see {@link #unfreezeRotation()}.
Tracer.trace();
getAutomatorBridge().getInteractionController().setRotationLeft();
waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
|
public void | setOrientationNatural()Simulates orienting the device into its natural orientation and also freezes rotation
by disabling the sensors.
If you want to un-freeze the rotation and re-enable the sensors
see {@link #unfreezeRotation()}.
Tracer.trace();
getAutomatorBridge().getInteractionController().setRotationNatural();
waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
|
public void | setOrientationRight()Simulates orienting the device to the right and also freezes rotation
by disabling the sensors.
If you want to un-freeze the rotation and re-enable the sensors
see {@link #unfreezeRotation()}.
Tracer.trace();
getAutomatorBridge().getInteractionController().setRotationRight();
waitForIdle(); // we don't need to check for idle on entry for this. We'll sync on exit
|
private void | setWatcherTriggered(java.lang.String watcherName)Used internally by this class to set a {@link UiWatcher} state as triggered.
Tracer.trace(watcherName);
if (!hasWatcherTriggered(watcherName)) {
mWatchersTriggers.add(watcherName);
}
|
public void | sleep()This method simply presses the power button if the screen is ON else
it does nothing if the screen is already OFF.
Tracer.trace();
getAutomatorBridge().getInteractionController().sleepDevice();
|
public boolean | swipe(int startX, int startY, int endX, int endY, int steps)Performs a swipe from one coordinate to another using the number of steps
to determine smoothness and speed. Each step execution is throttled to 5ms
per step. So for a 100 steps, the swipe will take about 1/2 second to complete.
Tracer.trace(startX, startY, endX, endY, steps);
return getAutomatorBridge().getInteractionController()
.swipe(startX, startY, endX, endY, steps);
|
public boolean | swipe(android.graphics.Point[] segments, int segmentSteps)Performs a swipe between points in the Point array. Each step execution is throttled
to 5ms per step. So for a 100 steps, the swipe will take about 1/2 second to complete
Tracer.trace(segments, segmentSteps);
return getAutomatorBridge().getInteractionController().swipe(segments, segmentSteps);
|
public boolean | takeScreenshot(java.io.File storePath)Take a screenshot of current window and store it as PNG
Default scale of 1.0f (original size) and 90% quality is used
The screenshot is adjusted per screen rotation
Tracer.trace(storePath);
return takeScreenshot(storePath, 1.0f, 90);
|
public boolean | takeScreenshot(java.io.File storePath, float scale, int quality)Take a screenshot of current window and store it as PNG
The screenshot is adjusted per screen rotation
Tracer.trace(storePath, scale, quality);
return getAutomatorBridge().takeScreenshot(storePath, quality);
|
public void | unfreezeRotation()Re-enables the sensors and un-freezes the device rotation allowing its contents
to rotate with the device physical rotation. During a test execution, it is best to
keep the device frozen in a specific orientation until the test case execution has completed.
Tracer.trace();
getAutomatorBridge().getInteractionController().unfreezeRotation();
|
public void | waitForIdle()Waits for the current application to idle.
Default wait timeout is 10 seconds
Tracer.trace();
waitForIdle(Configurator.getInstance().getWaitForIdleTimeout());
|
public void | waitForIdle(long timeout)Waits for the current application to idle.
Tracer.trace(timeout);
getAutomatorBridge().waitForIdle(timeout);
|
public boolean | waitForWindowUpdate(java.lang.String packageName, long timeout)Waits for a window content update event to occur.
If a package name for the window is specified, but the current window
does not have the same package name, the function returns immediately.
Tracer.trace(packageName, timeout);
if (packageName != null) {
if (!packageName.equals(getCurrentPackageName())) {
return false;
}
}
Runnable emptyRunnable = new Runnable() {
@Override
public void run() {
}
};
AccessibilityEventFilter checkWindowUpdate = new AccessibilityEventFilter() {
@Override
public boolean accept(AccessibilityEvent t) {
if (t.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
return packageName == null || packageName.equals(t.getPackageName());
}
return false;
}
};
try {
getAutomatorBridge().executeCommandAndWaitForAccessibilityEvent(
emptyRunnable, checkWindowUpdate, timeout);
} catch (TimeoutException e) {
return false;
} catch (Exception e) {
Log.e(LOG_TAG, "waitForWindowUpdate: general exception from bridge", e);
return false;
}
return true;
|
public void | wakeUp()This method simulates pressing the power button if the screen is OFF else
it does nothing if the screen is already ON.
If the screen was OFF and it just got turned ON, this method will insert a 500ms delay
to allow the device time to wake up and accept input.
Tracer.trace();
if(getAutomatorBridge().getInteractionController().wakeDevice()) {
// sync delay to allow the window manager to start accepting input
// after the device is awakened.
SystemClock.sleep(500);
}
|