From 94b3f8eee62f8d2b1076654072d61e67654ed977 Mon Sep 17 00:00:00 2001
From: Quddus Chong Provides auxiliary support for running test cases
+
+ Allows the running test cases to send out interim status UI automation tests should extend this class. This class provides access
+ to the following:
+andr
-
+ create-test-project
create test-project
@@ -306,7 +306,7 @@ and may be out of date. For the most current list of options, execute -n <name>
andr
-
+ update-test-project
update test-project
@@ -324,7 +324,7 @@ and may be out of date. For the most current list of options, execute -p <path>
andr
-
+ create-lib-project
create lib-project
@@ -358,7 +358,7 @@ and may be out of date. For the most current list of options, execute -k <packageName>
andr
-
+
+
+ update-lib-project
update lib-project
@@ -382,6 +382,30 @@ and may be out of date. For the most current list of options, execute -p <path>
andr
+
+
+
+ create uitest-project
+ -n <name>
The name of the UI test project
+
+
+
+
+
+
+ -t <name>
Target ID of the UI test project
+
+ Required
+
+
+
+
+ -p <path>
Location path of the UI test project
+
+ Required
+ Update actions
diff --git a/docs/html/tools/help/uiautomator/IAutomationSupport.jd b/docs/html/tools/help/uiautomator/IAutomationSupport.jd
new file mode 100644
index 0000000..4120f2b
--- /dev/null
+++ b/docs/html/tools/help/uiautomator/IAutomationSupport.jd
@@ -0,0 +1,168 @@
+page.title=IAutomationSupport
+parent.title=uiautomator
+parent.link=index.html
+@jd:body
+
+
+Class Overview
+Summary
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Public Methods
+
+
+
+
+
+ abstract
+
+
+
+
+ void
+
+
+ sendStatus(int resultCode, Bundle status)
+
+ Public Methods
+
+
+
+
+
+
+
+ public
+
+
+ abstract
+
+ void
+
+ sendStatus
+ (int resultCode, Bundle status)
+
+ Class Overview
+
+
+UiDevice
instance
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + + | ++ UiAutomatorTestCase() + + |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + IAutomationSupport + | +
+ getAutomationSupport()
+
+ Provides support for running tests to report interim status
+
+ | ||||||||||
+ + + + + + Bundle + | +
+ getParams()
+
+ Get command line parameters.
+
+ | ||||||||||
+ + + + + + UiDevice + | +
+ getUiDevice()
+
+ Get current instance of
+
+ UiDevice . | ||||||||||
+ + + + + + void + | +
+ sleep(long ms)
+
+ Calls
+
+ sleep(long) to sleep |
+ [Expand]
+ Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+
+From class
+
+ junit.framework.TestCase
+
+
+
+
+
+
+ | |||||||||||
+
+From class
+
+ junit.framework.Assert
+
+
+
+
+
+
+ | |||||||||||
+
+From class
+
+ java.lang.Object
+
+
+
+
+
+
+ | |||||||||||
+
+From interface
+
+ junit.framework.Test
+
+
+
+
+
+
+ |
Provides support for running tests to report interim status
Get command line parameters. On the command line when passing -e key value
+ pairs, the Bundle will have the key value pairs conveniently available to the
+ tests.
+
Get current instance of UiDevice
. Works similar to calling the static
+ getInstance()
from anywhere in the test classes.
+
Calls sleep(long)
to sleep
ms | +is in milliseconds. + | +
---|
Used to enumerate a container's user interface (UI) elements for the purpose of counting, + or targeting a sub elements by a child's text or description. +
+ +Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + + | ++ UiCollection(UiSelector selector) + + |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + UiObject + | +
+ getChildByDescription(UiSelector childPattern, String text)
+
+ Searches for child UI element within the constraints of this
+
+ UiSelector
+ selector. | ||||||||||
+ + + + + + UiObject + | +
+ getChildByInstance(UiSelector childPattern, int instance)
+
+ Searches for child UI element within the constraints of this
+
+ UiSelector . | ||||||||||
+ + + + + + UiObject + | +
+ getChildByText(UiSelector childPattern, String text)
+
+ Searches for child UI element within the constraints of this
+
+ UiSelector . | ||||||||||
+ + + + + + int + | +
+ getChildCount(UiSelector childPattern)
+
+ Counts child UI element instances matching the
+
+ childPattern
+ argument. |
+ [Expand]
+ Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+
+From class
+
+ com.android.uiautomator.core.UiObject
+
+
+
+
+
+
+ | |||||||||||
+
+From class
+
+ java.lang.Object
+
+
+
+
+
+
+ |
Searches for child UI element within the constraints of this UiSelector
+ selector.
+
+ It looks for any child matching the childPattern
argument that has
+ a child UI element anywhere within its sub hierarchy that has content-description text.
+ The returned UiObject will point at the childPattern
instance that matched the
+ search and not at the identifying child element that matched the content description.
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
text | +String of the identifying child contents of of the childPattern |
+
UiObject
pointing at and instance of childPattern
UiObjectNotFoundException + | + |
---|
Searches for child UI element within the constraints of this UiSelector
.
+
+ It looks for any child matching the childPattern
argument that has
+ a child UI element anywhere within its sub hierarchy that is at the instance
+ specified. The operation is performed only on the visible items and no scrolling is performed
+ in this case.
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
instance | +int the desired matched instance of this childPattern |
+
UiObject
pointing at and instance of childPattern
+UiObjectNotFoundException + | + |
---|
Searches for child UI element within the constraints of this UiSelector
+ selector.
+
+ It looks for any child matching the childPattern
argument that has
+ a child UI element anywhere within its sub hierarchy that has a text attribute equal to
+ text
. The returned UiObject will point at the childPattern
+ instance that matched the search and not at the identifying child element that matched the
+ text attribute.
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
text | +String of the identifying child contents of of the childPattern |
+
UiObject
pointing at and instance of childPattern
UiObjectNotFoundException + | + |
---|
Counts child UI element instances matching the childPattern
+ argument. The method returns the number of matching UI elements that are
+ currently visible. The count does not include items of a scrollable list
+ that are off-screen.
childPattern | +a UiSelector that represents the matching child UI
+ elements to count |
+
---|
UiCollection
+UiDevice provides access to state information about the device. + You can also use this class to simulate user actions on the device, + such as pressing the d-pad or pressing the Home and Menu buttons.
+ + + + + + + + + + + + + + + + + + + + + + +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
+
+ | ||||||||||
+ + + + + + void + | +
+ dumpWindowHierarchy(String fileName)
+
+ Helper method used for debugging to dump the current window's layout hierarchy.
+
+ | ||||||||||
+ + + + + + 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.
+
+ | ||||||||||
+ + + + + + int + | +
+ getDisplayHeight()
+
+ Gets the height of the display, in pixels.
+
+ | ||||||||||
+ + + + + + int + | +
+ getDisplayRotation()
+
+ Returns the current rotation of the display, as defined in Surface@return
+
+ | ||||||||||
+ + + + + + int + | +
+ getDisplayWidth()
+
+ Gets the width of the display, in pixels.
+
+ | ||||||||||
+ + + + static + + UiDevice + | +
+ getInstance()
+
+ Retrieves a singleton instance of UiDevice
+
+ | ||||||||||
+ + + + + + String + | +
+ getLastTraversedText()
+
+ Retrieves the text from the last UI traversal event received.
+
+ | ||||||||||
+ + + + + + String + | +
+ getProductName()
+
+ Retrieves the product name of the device.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ hasAnyWatcherTriggered()
+
+ Checks if any registered
+
+ UiWatcher have triggered. | ||||||||||
+ + + + + + boolean + | +
+ hasWatcherTriggered(String watcherName)
+
+ Checks if a specific registered
+
+ UiWatcher has triggered. | ||||||||||
+ + + + + + boolean + | +
+ isNaturalOrientation()
+
+ Check if the device is in its natural orientation.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ isScreenOn()
+
+ Checks the power manager if the screen is ON.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressBack()
+
+ Simulates a short press on the BACK button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressDPadCenter()
+
+ Simulates a short press on the CENTER button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressDPadDown()
+
+ Simulates a short press on the DOWN button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressDPadLeft()
+
+ Simulates a short press on the LEFT button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressDPadRight()
+
+ Simulates a short press on the RIGHT button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressDPadUp()
+
+ Simulates a short press on the UP button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressDelete()
+
+ Simulates a short press on the DELETE key.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressEnter()
+
+ Simulates a short press on the ENTER key.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressHome()
+
+ Simulates a short press on the HOME button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressKeyCode(int keyCode)
+
+ Simulates a short press using a key code.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressKeyCode(int keyCode, int metaState)
+
+ Simulates a short press using a key code.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressMenu()
+
+ Simulates a short press on the MENU button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressRecentApps()
+
+ Simulates a short press on the Recent Apps button.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ pressSearch()
+
+ Simulates a short press on the SEARCH button.
+
+ | ||||||||||
+ + + + + + void + | +
+ registerWatcher(String name, UiWatcher watcher)
+
+ Registers a
+
+ UiWatcher to run automatically when the testing framework is unable to
+ find a match using a UiSelector . | ||||||||||
+ + + + + + void + | +
+ removeWatcher(String name)
+
+ Removes a previously registered
+
+ UiWatcher . | ||||||||||
+ + + + + + void + | +
+ resetWatcherTriggers()
+
+ Resets a
+
+ UiWatcher that has been triggered. | ||||||||||
+ + + + + + void + | +
+ runWatchers()
+
+ This method forces all registered watchers to run.
+
+ | ||||||||||
+ + + + + + void + | +
+ setOrientationLeft()
+
+ Simulates orienting the device to the left and also freezes rotation
+ by disabling the sensors.
+
+ | ||||||||||
+ + + + + + void + | +
+ setOrientationNatural()
+
+ Simulates orienting the device into its natural orientation and also freezes rotation
+ by disabling the sensors.
+
+ | ||||||||||
+ + + + + + void + | +
+ setOrientationRight()
+
+ Simulates orienting the device to the right and also freezes rotation
+ by disabling the sensors.
+
+ | ||||||||||
+ + + + + + void + | +
+ sleep()
+
+ This method simply presses the power button if the screen is ON else
+ it does nothing if the screen is already OFF.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ swipe(Point[] segments, int segmentSteps)
+
+ Performs a swipe between points in the Point array.
+
+ | ||||||||||
+ + + + + + 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.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ takeScreenshot(File storePath, float scale, int quality)
+
+ Take a screenshot of current window and store it as PNG
+
+ The screenshot is adjusted per screen rotation
+
+ | ||||||||||
+ + + + + + boolean + | +
+ takeScreenshot(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
+
+ | ||||||||||
+ + + + + + void + | +
+ unfreezeRotation()
+
+ Re-enables the sensors and un-freezes the device rotation allowing its contents
+ to rotate with the device physical rotation.
+
+ | ||||||||||
+ + + + + + void + | +
+ waitForIdle(long time)
+
+ Waits for the current application to idle.
+
+ | ||||||||||
+ + + + + + void + | +
+ waitForIdle()
+
+ Waits for the current application to idle.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ waitForWindowUpdate(String packageName, long timeout)
+
+ Waits for a window content update event to occur.
+
+ | ||||||||||
+ + + + + + void + | +
+ wakeUp()
+
+ This method simulates pressing the power button if the screen is OFF else
+ it does nothing if the screen is already ON.
+
+ |
+ [Expand]
+ Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+
+From class
+
+ java.lang.Object
+
+
+
+
+
+
+ |
Clears the text from the last UI traversal event.
+ See getLastTraversedText()
.
Perform a click at arbitrary coordinates specified by the user
x | +coordinate | +
---|---|
y | +coordinate | +
Helper method used for debugging to dump the current window's layout hierarchy. + The file root location is /data/local/tmp
Disables the sensors and freezes the device rotation at its + current rotation state.
+ | RemoteException | +
---|---|
RemoteException + | + |
+
+
+ This method is deprecated.
The results returned should be considered unreliable
+
Retrieves the last activity to report accessibility events.
Retrieves the name of the last package to report accessibility events.
Gets the height of the display, in pixels. The size is adjusted based + on the current orientation of the display.
Returns the current rotation of the display, as defined in Surface@return
Gets the width of the display, in pixels. The width and height details + are reported based on the current orientation of the display.
Retrieves a singleton instance of UiDevice
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.
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.
Checks if any registered UiWatcher
have triggered.
+
+ See registerWatcher(String, UiWatcher)
+ See hasWatcherTriggered(String)
Checks if a specific registered UiWatcher
has triggered.
+ See registerWatcher(String, UiWatcher)
. If a UiWatcher runs and its
+ 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.
Check if the device is in its natural orientation. This is determined by checking if the + orientation is at 0 or 180 degrees.
Checks the power manager if the screen is ON.
+ | RemoteException | +
---|---|
RemoteException + | + |
Simulates a short press on the BACK button.
Simulates a short press on the CENTER button.
Simulates a short press on the DOWN button.
Simulates a short press on the LEFT button.
Simulates a short press on the RIGHT button.
Simulates a short press on the UP button.
Simulates a short press on the DELETE key.
Simulates a short press on the ENTER key.
Simulates a short press on the HOME button.
Simulates a short press using a key code. + + See KeyEvent
Simulates a short press using a key code. + + See KeyEvent.
keyCode | +the key code of the event. | +
---|---|
metaState | +an integer in which each bit set to 1 represents a pressed meta key | +
Simulates a short press on the MENU button.
Simulates a short press on the Recent Apps button.
+ | RemoteException | +
---|---|
RemoteException + | + |
Simulates a short press on the SEARCH button.
Registers a UiWatcher
to run automatically when the testing framework is unable to
+ find a match using a UiSelector
. See runWatchers()
name | +to register the UiWatcher | +
---|---|
watcher | +UiWatcher |
+
Removes a previously registered UiWatcher
.
+
+ See registerWatcher(String, UiWatcher)
name | +used to register the UiWatcher | +
---|
+ | UiAutomationException | +
---|
Resets a UiWatcher
that has been triggered.
+ If a UiWatcher runs and its checkForCondition()
call
+ returned true
, then the UiWatcher is considered triggered.
+ See registerWatcher(String, UiWatcher)
This method forces all registered watchers to run.
+ See registerWatcher(String, UiWatcher)
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 unfreezeRotation()
.
+ | RemoteException | +
---|---|
RemoteException + | + |
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 unfreezeRotation()
.
+ | RemoteException | +
---|---|
RemoteException + | + |
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 unfreezeRotation()
.
+ | RemoteException | +
---|---|
RemoteException + | + |
This method simply presses the power button if the screen is ON else + it does nothing if the screen is already OFF.
+ | RemoteException | +
---|---|
RemoteException + | + |
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
segments | +is Point array containing at least one Point object | +
---|---|
segmentSteps | +steps to inject between two Points | +
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.
steps | +is the number of move steps sent to the system | +
---|
Take a screenshot of current window and store it as PNG + + The screenshot is adjusted per screen rotation
storePath | +where the PNG should be written to | +
---|---|
scale | +scale the screenshot down if needed; 1.0f for original size | +
quality | +quality of the PNG compression; range: 0-100 | +
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
storePath | +where the PNG should be written to | +
---|
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.
RemoteException + | + |
---|
Waits for the current application to idle.
Waits for the current application to idle. + Default wait timeout is 10 seconds
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.
packageName | +the specified window package name (can be null ).
+ If null , a window update from any front-end window will end the wait |
+
---|---|
timeout | +the timeout for the wait | +
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.
+ | RemoteException | +
---|---|
RemoteException + | + |
A UiObject is a representation of a user interface (UI) element. It is not in any way directly bound to a
+ UI element as an object reference. A UiObject holds information to help it
+ locate a matching UI element at runtime based on the UiSelector
properties specified in
+ its constructor. Since a UiObject is a representative for a UI element, it can
+ be reused for different views with matching UI elements.
+
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + + | +
+ UiObject(UiSelector selector)
+
+ Constructs a UiObject to represent a specific UI element matched by the specified
+
+
+ UiSelector selector properties. |
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(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 + | +
+ clickAndWaitForNewWindow()
+
+ See
+
+ clickAndWaitForNewWindow(long)
+ This method is intended to reliably wait for window transitions that would typically take
+ longer than the usual default timeouts. | ||||||||||
+ + + + + + 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 + | +
+ exists()
+
+ Check if UI element exists.
+
+ | ||||||||||
+ + + + + + Rect + | +
+ getBounds()
+
+ Returns the UI element's
+
+ bounds property. | ||||||||||
+ + + + + + UiObject + | +
+ getChild(UiSelector selector)
+
+ Creates a new UiObject representing a child UI element of the element currently represented
+ by this UiObject.
+
+ | ||||||||||
+ + + + + + int + | +
+ getChildCount()
+
+ Counts the child UI elements immediately under the UI element currently represented by
+ this UiObject.
+
+ | ||||||||||
+ + + + + + String + | +
+ getContentDescription()
+
+ Reads the
+
+ content_desc property of the UI element | ||||||||||
+ + + + + + UiObject + | +
+ getFromParent(UiSelector selector)
+
+ Creates a new UiObject representing a child UI element from the parent element currently
+ represented by this object.
+
+ | ||||||||||
+ + + + + + String + | +
+ getPackageName()
+
+ Reads the UI element's
+
+ package property | ||||||||||
+ + + final + + + UiSelector + | +
+ getSelector()
+
+ Debugging helper.
+
+ | ||||||||||
+ + + + + + String + | +
+ getText()
+
+ Reads the
+
+ text property of the UI element | ||||||||||
+ + + + + + Rect + | +
+ getVisibleBounds()
+
+ Returns the visible bounds of the UI element.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ isCheckable()
+
+ Check if the UI element's
+
+ checkable property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isChecked()
+
+ Check if the UI element's
+
+ checked property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isClickable()
+
+ Check if the UI element's
+
+ clickable property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isEnabled()
+
+ Check if the UI element's
+
+ enabled property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isFocusable()
+
+ Check if the UI element's
+
+ focusable property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isFocused()
+
+ Check if the UI element's
+
+ focused property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isLongClickable()
+
+ Check if the UI element's
+
+ long-clickable property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isScrollable()
+
+ Check if the UI element's
+
+ scrollable property is currently true | ||||||||||
+ + + + + + boolean + | +
+ isSelected()
+
+ Check if the UI element's
+
+ selected property is currently true | ||||||||||
+ + + + + + boolean + | +
+ longClick()
+
+ Long clicks the center of the visible bounds of the UI element
+
+ | ||||||||||
+ + + + + + boolean + | +
+ longClickBottomRight()
+
+ Long clicks bottom and right corner of the UI element
+
+ | ||||||||||
+ + + + + + boolean + | +
+ longClickTopLeft()
+
+ Long clicks on the top and left corner of the UI element
+
+ | ||||||||||
+ + + + + + boolean + | +
+ setText(String text)
+
+ Sets the text in an editable field, after clearing the field's content.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ swipeDown(int steps)
+
+ Perform the action on the UI element that is represented by this object, Also see
+ #scrollToBeginning(int), #scrollToEnd(int), #scrollBackward(),
+ #scrollForward().
+
+ | ||||||||||
+ + + + + + boolean + | +
+ swipeLeft(int steps)
+
+ Perform the action on the UI element that is represented by this object.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ swipeRight(int steps)
+
+ Perform the action on the UI element that is represented by this object.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ swipeUp(int steps)
+
+ Perform the action on the UI element that is represented by this UiObject.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ waitForExists(long timeout)
+
+ Waits a specified length of time for a UI element to become visible.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ waitUntilGone(long timeout)
+
+ Waits a specified length of time for a UI element to become undetectable.
+
+ |
+ [Expand]
+ Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+
+From class
+
+ java.lang.Object
+
+
+
+
+
+
+ |
Constructs a UiObject to represent a specific UI element matched by the specified
+ UiSelector
selector properties.
Clears the existing text contents in an editable field.
+
+ The UiSelector
of this object must reference a UI element that is editable.
+
+ When you call this method, the method first sets focus at the start edge of the field.
+ The method then simulates a long-press to select the existing text, and deletes the
+ selected text.
+
+ If a "Select-All" option is displayed, the method will automatically attempt to use it
+ to ensure full text selection.
+
+ Note that it is possible that not all the text in the field is selected; for example,
+ if the text contains separators such as spaces, slashes, at symbol etc.
+ Also, not all editable fields support the long-press functionality.
UiObjectNotFoundException + | + |
---|
Performs a click at the center of the visible bounds of the UI element represented + by this UiObject.
UiObjectNotFoundException + | + |
---|
Performs a click at the center of the visible bounds of the UI element represented
+ by this UiObject and waits for window transitions.
+
+ This method differ from click()
only in that this method waits for a
+ a new window transition as a result of the click. Some examples of a window transition:
+
timeout | +timeout before giving up on waiting for a new window | +
---|
UiObjectNotFoundException + | + |
---|
See clickAndWaitForNewWindow(long)
+ This method is intended to reliably wait for window transitions that would typically take
+ longer than the usual default timeouts.
UiObjectNotFoundException + | + |
---|
Clicks the bottom and right corner of the UI element
Exception + | + |
---|---|
UiObjectNotFoundException + | + |
Clicks the top and left corner of the UI element
Exception + | + |
---|---|
UiObjectNotFoundException + | + |
Check if UI element exists.
+
+ This methods performs a waitForExists(long)
with zero timeout. This
+ basically returns immediately whether the UI element represented by this UiObject
+ exists or not. If you need to wait longer for this UI element, then see
+ waitForExists(long)
.
Returns the UI element's bounds
property. See getVisibleBounds()
UiObjectNotFoundException + | + |
---|
Creates a new UiObject representing a child UI element of the element currently represented + by this UiObject.
selector | +for UI element to match | +
---|
UiObjectNotFoundException + | + |
---|
Counts the child UI elements immediately under the UI element currently represented by + this UiObject.
UiObjectNotFoundException + | + |
---|
Reads the content_desc
property of the UI element
UiObjectNotFoundException + | + |
---|
Creates a new UiObject representing a child UI element from the parent element currently + represented by this object. Essentially this is starting the search from the parent + element and can also be used to find sibling UI elements to the one currently represented + by this UiObject.
selector | +for the UI element to match | +
---|
UiObjectNotFoundException + | + |
---|
Reads the UI element's package
property
UiObjectNotFoundException + | + |
---|
Debugging helper. A test can dump the properties of a selector as a string
+ to its logs if needed. getSelector().toString();
Reads the text
property of the UI element
UiObjectNotFoundException + | if no match could be found + | +
---|
Returns the visible bounds of the UI element. + + If a portion of the UI element is visible, only the bounds of the visible portion are + reported.
+ | UiObjectNotFoundException | +
---|---|
UiObjectNotFoundException + | + |
Check if the UI element's checkable
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's checked
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's clickable
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's enabled
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's focusable
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's focused
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's long-clickable
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's scrollable
property is currently true
UiObjectNotFoundException + | + |
---|
Check if the UI element's selected
property is currently true
UiObjectNotFoundException + | + |
---|
Long clicks the center of the visible bounds of the UI element
UiObjectNotFoundException + | + |
---|
Long clicks bottom and right corner of the UI element
UiObjectNotFoundException + | + |
---|
Long clicks on the top and left corner of the UI element
UiObjectNotFoundException + | + |
---|
Sets the text in an editable field, after clearing the field's content.
+
+ The UiSelector
selector of this object must reference a UI element that is editable.
+
+ When you call this method, the method first simulates a click()
on
+ editable field to set focus. The method then clears the field's contents
+ and injects your specified text into the field.
+
+ If you want to capture the original contents of the field, call getText()
first.
+ You can then modify the text and use this method to update the field.
text | +string to set | +
---|
UiObjectNotFoundException + | + |
---|
Perform the action on the UI element that is represented by this object, Also see
+ #scrollToBeginning(int), #scrollToEnd(int), #scrollBackward(),
+ #scrollForward(). This method will perform the swipe gesture over any
+ surface. The targeted UI element does not need to have the attribute
+ scrollable
set to true
for this operation to be performed.
steps | +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. | +
---|
UiObjectNotFoundException + | + |
---|
Perform the action on the UI element that is represented by this object. Also see
+ #scrollToBeginning(int), #scrollToEnd(int), #scrollBackward(),
+ #scrollForward(). This method will perform the swipe gesture over any
+ surface. The targeted UI element does not need to have the attribute
+ scrollable
set to true
for this operation to be performed.
steps | +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. | +
---|
UiObjectNotFoundException + | + |
---|
Perform the action on the UI element that is represented by this object. Also see
+ #scrollToBeginning(int), #scrollToEnd(int), #scrollBackward(),
+ #scrollForward(). This method will perform the swipe gesture over any
+ surface. The targeted UI element does not need to have the attribute
+ scrollable
set to true
for this operation to be performed.
steps | +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. | +
---|
UiObjectNotFoundException + | + |
---|
Perform the action on the UI element that is represented by this UiObject. Also see + #scrollToBeginning(int), #scrollToEnd(int), #scrollBackward(), + #scrollForward().
steps | +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. | +
---|
UiObjectNotFoundException + | + |
---|
Waits a specified length of time for a UI element to become visible. + + This method waits until the UI element becomes visible on the display, or + until the timeout has elapsed. You can use this method in situations where + the content that you want to select is not immediately displayed.
timeout | +the amount of time to wait (in milliseconds) | +
---|
Waits a specified length of time for a UI element to become undetectable.
+
+ This method waits until a UI element is no longer matchable, or until the
+ timeout has elapsed.
+
+ A UI element becomes undetectable when the UiSelector
of the object is
+ unable to find a match because the element has either changed its state or is no
+ longer displayed.
+
+ You can use this method when attempting to wait for some long operation
+ to compete, such as downloading a large file or connecting to a remote server.
timeout | +time to wait (in milliseconds) | +
---|
Generated in test runs when a UiSelector
selector could not be matched
+ to any UI element displayed.
+
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + + | ++ UiObjectNotFoundException(String msg) + + | ||||||||||
+ + + + + + + | ++ UiObjectNotFoundException(String detailMessage, Throwable throwable) + + | ||||||||||
+ + + + + + + | ++ UiObjectNotFoundException(Throwable throwable) + + |
+ [Expand]
+ Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+
+From class
+
+ java.lang.Throwable
+
+
+
+
+
+
+ | |||||||||||
+
+From class
+
+ java.lang.Object
+
+
+
+
+
+
+ |
UiScrollable is a UiCollection
and provides support for searching for items in a
+ scrollable user interface (UI) elements. This class can be used with horizontally or vertically scrollable controls..
+
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + + | +
+ UiScrollable(UiSelector container)
+
+ UiScrollable is a
+
+ UiCollection and as such requires a UiSelector to
+ identify the container UI element of the scrollable collection. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + boolean + | ++ flingBackward() + + + + | ||||||||||
+ + + + + + boolean + | +
+ flingForward()
+
+ A convenience version of
+
+ scrollForward(int) , performs a fling | ||||||||||
+ + + + + + boolean + | ++ flingToBeginning(int maxSwipes) + + + + | ||||||||||
+ + + + + + boolean + | ++ flingToEnd(int maxSwipes) + + + + | ||||||||||
+ + + + + + UiObject + | ++ getChildByDescription(UiSelector childPattern, String text, boolean allowScrollSearch) + + + + | ||||||||||
+ + + + + + UiObject + | +
+ getChildByDescription(UiSelector childPattern, String text)
+
+ Searches for child UI element within the constraints of this UiScrollable
+
+ UiSelector
+ container. | ||||||||||
+ + + + + + UiObject + | +
+ getChildByInstance(UiSelector childPattern, int instance)
+
+ Searches for child UI element within the constraints of this UiScrollable
+
+ UiSelector
+ selector. | ||||||||||
+ + + + + + UiObject + | ++ getChildByText(UiSelector childPattern, String text, boolean allowScrollSearch) + + + + | ||||||||||
+ + + + + + UiObject + | +
+ getChildByText(UiSelector childPattern, String text)
+
+ Searches for child UI element within the constraints of this UiScrollable
+
+ UiSelector
+ container. | ||||||||||
+ + + + + + int + | +
+ getMaxSearchSwipes()
+
+ #getChildByDescription(String, boolean) and #getChildByText(String, boolean)
+ use an arguments that specifies if scrolling is allowed while searching for the UI element.
+
+ | ||||||||||
+ + + + + + double + | +
+ getSwipeDeadZonePercentage()
+
+ Returns the percentage of a widget's size that's considered as a no touch zone when swiping.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ scrollBackward(int steps)
+
+ Perform a scroll backward.
+
+ | ||||||||||
+ + + + + + boolean + | ++ scrollBackward() + + + + | ||||||||||
+ + + + + + boolean + | +
+ scrollDescriptionIntoView(String text)
+
+ Performs a swipe Up on the UI element until the requested content-description
+ is visible or until swipe attempts have been exhausted.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ scrollForward()
+
+ A convenience version of
+
+ scrollForward(int) , performs a regular scroll | ||||||||||
+ + + + + + boolean + | +
+ scrollForward(int steps)
+
+ Perform a scroll forward.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ scrollIntoView(UiSelector selector)
+
+ Perform a scroll search for a UI element matching the
+
+ UiSelector selector argument. | ||||||||||
+ + + + + + boolean + | +
+ scrollTextIntoView(String text)
+
+ Performs a swipe up on the UI element until the requested text is visible
+ or until swipe attempts have been exhausted.
+
+ | ||||||||||
+ + + + + + boolean + | ++ scrollToBeginning(int maxSwipes) + + + + | ||||||||||
+ + + + + + boolean + | +
+ scrollToBeginning(int maxSwipes, int steps)
+
+ Scrolls to the beginning of a scrollable UI element.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ scrollToEnd(int maxSwipes, int steps)
+
+ Scrolls to the end of a scrollable UI element.
+
+ | ||||||||||
+ + + + + + boolean + | +
+ scrollToEnd(int maxSwipes)
+
+ See {@link UiScrollable#scrollToEnd(int, int)
+
+ | ||||||||||
+ + + + + + void + | +
+ setAsHorizontalList()
+
+ Set the direction of swipes when performing scroll search
+
+
+ | ||||||||||
+ + + + + + void + | +
+ setAsVerticalList()
+
+ Set the direction of swipes when performing scroll search
+
+
+ | ||||||||||
+ + + + + + void + | +
+ setMaxSearchSwipes(int swipes)
+
+ #getChildByDescription(String, boolean) and #getChildByText(String, boolean)
+ use an arguments that specifies if scrolling is allowed while searching for the UI element.
+
+ | ||||||||||
+ + + + + + void + | +
+ setSwipeDeadZonePercentage(double swipeDeadZonePercentage)
+
+ Sets the percentage of a widget's size that's considered as a no touch zone when swiping.
+
+ |
+ [Expand]
+ Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+
+From class
+
+ com.android.uiautomator.core.UiCollection
+
+
+
+
+
+
+ | |||||||||||
+
+From class
+
+ com.android.uiautomator.core.UiObject
+
+
+
+
+
+
+ | |||||||||||
+
+From class
+
+ java.lang.Object
+
+
+
+
+
+
+ |
UiScrollable is a UiCollection
and as such requires a UiSelector
to
+ identify the container UI element of the scrollable collection. Further operations on
+ the items in the container will require specifying UiSelector as an item selector.
container | +a UiSelector selector
+ |
+
---|
UiObjectNotFoundException + | + |
---|
A convenience version of scrollForward(int)
, performs a fling
UiObjectNotFoundException + | + |
---|
UiObjectNotFoundException + | + |
---|
UiObjectNotFoundException + | + |
---|
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
text | +String may be a partial match for the content-description of a child element. | +
allowScrollSearch | +set to true if scrolling is allowed | +
UiObject
pointing at and instance of childPattern
UiObjectNotFoundException + | + |
---|
Searches for child UI element within the constraints of this UiScrollable UiSelector
+ container. It looks for any child matching the childPattern
argument within its
+ hierarchy with a matching content-description text. The returned UiObject will represent the
+ UI element matching the childPattern
and not the sub element that matched the
+ content description.
getChildByDescription(UiSelector, String, boolean)
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
text | +String of the identifying child contents of of the childPattern |
+
UiObject
pointing at and instance of childPattern
UiObjectNotFoundException + | + |
---|
Searches for child UI element within the constraints of this UiScrollable UiSelector
+ selector. It looks for any child matching the childPattern
argument and
+ return the instance
specified. The operation is performed only on the visible
+ items and no scrolling is performed in this case.
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
instance | +int the desired matched instance of this childPattern |
+
UiObject
pointing at and instance of childPattern
+UiObjectNotFoundException + | + |
---|
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
text | +String of the identifying child contents of of the childPattern |
+
allowScrollSearch | +set to true if scrolling is allowed | +
UiObject
pointing at and instance of childPattern
UiObjectNotFoundException + | + |
---|
Searches for child UI element within the constraints of this UiScrollable UiSelector
+ container. It looks for any child matching the childPattern
argument that has
+ a sub UI element anywhere within its sub hierarchy that has text attribute
+ text
. The returned UiObject will point at the childPattern
+ instance that matched the search and not at the text matched sub element
getChildByText(UiSelector, String, boolean)
childPattern | +UiSelector selector of the child pattern to match and return |
+
---|---|
text | +String of the identifying child contents of of the childPattern |
+
UiObject
pointing at and instance of childPattern
UiObjectNotFoundException + | + |
---|
#getChildByDescription(String, boolean) and #getChildByText(String, boolean)
+ use an arguments that specifies if scrolling is allowed while searching for the UI element.
+ The number of scrolls currently allowed to perform a search can be read by this method.
+ See setMaxSearchSwipes(int)
Returns the percentage of a widget's size that's considered as a no touch zone when swiping. + + Dead zones are set as percentage of a widget's total width or height, denoting a margin + around the swipable area of the widget. Swipes must start and + end inside this margin. + + This is important when the widget being swiped may not respond to the swipe if + started at a point too near to the edge. The default is 10% from either edge.
Perform a scroll backward. If this list is set to vertical (see setAsVerticalList()
+ default) then the swipes will be executed from the top to bottom. If this list is set
+ to horizontal (see setAsHorizontalList()
) then the swipes will be executed from
+ the left to right. Caution is required on devices configured with right to left languages
+ like Arabic and Hebrew.
steps | +use steps to control the speed, so that it may be a scroll, or fling | +
---|
UiObjectNotFoundException + | + |
---|
UiObjectNotFoundException + | + |
---|
Performs a swipe Up on the UI element until the requested content-description
+ is visible or until swipe attempts have been exhausted. See setMaxSearchSwipes(int)
text | +to look for anywhere within the contents of this scrollable. | +
---|
UiObjectNotFoundException + | + |
---|
A convenience version of scrollForward(int)
, performs a regular scroll
UiObjectNotFoundException + | + |
---|
Perform a scroll forward. If this list is set to vertical (see setAsVerticalList()
+ default) then the swipes will be executed from the bottom to top. If this list is set
+ to horizontal (see setAsHorizontalList()
) then the swipes will be executed from
+ the right to left. Caution is required on devices configured with right to left languages
+ like Arabic and Hebrew.
steps | +use steps to control the speed, so that it may be a scroll, or fling | +
---|
UiObjectNotFoundException + | + |
---|
Perform a scroll search for a UI element matching the UiSelector
selector argument.
+ See scrollDescriptionIntoView(String)
and scrollTextIntoView(String)
.
selector | +UiSelector selector |
+
---|
UiObjectNotFoundException + | + |
---|
Performs a swipe up on the UI element until the requested text is visible
+ or until swipe attempts have been exhausted. See setMaxSearchSwipes(int)
text | +to look for | +
---|
UiObjectNotFoundException + | + |
---|
UiObjectNotFoundException + | + |
---|
Scrolls to the beginning of a scrollable UI element. The beginning could be the top most + in case of vertical lists or the left most in case of horizontal lists. Caution is required + on devices configured with right to left languages like Arabic and Hebrew.
steps | +use steps to control the speed, so that it may be a scroll, or fling | +
---|
UiObjectNotFoundException + | + |
---|
Scrolls to the end of a scrollable UI element. The end could be the bottom most + in case of vertical controls or the right most for horizontal controls. Caution + is required on devices configured with right to left languages like Arabic and Hebrew.
steps | +use steps to control the speed, so that it may be a scroll, or fling | +
---|
UiObjectNotFoundException + | + |
---|
See {@link UiScrollable#scrollToEnd(int, int)
UiObjectNotFoundException + | + |
---|
Set the direction of swipes when performing scroll search +
Set the direction of swipes when performing scroll search +
#getChildByDescription(String, boolean) and #getChildByText(String, boolean)
+ use an arguments that specifies if scrolling is allowed while searching for the UI element.
+ The number of scrolls allowed to perform a search can be modified by this method.
+ The current value can be read by calling getMaxSearchSwipes()
swipes | +is the number of search swipes until abort + | +
---|
Sets the percentage of a widget's size that's considered as a no touch zone when swiping. + + Dead zones are set as percentage of a widget's total width or height, denoting a margin + around the swipable area of the widget. Swipes must always start and + end inside this margin. + + This is important when the widget being swiped may not respond to the swipe if + started at a point too near to the edge. The default is 10% from either edge
swipeDeadZonePercentage | +is a value between 0 and 1 + | +
---|
This class provides the mechanism for tests to describe the UI elements they + intend to target. A UI element has many properties associated with it such as + text value, content-description, class name and multiple state information like + selected, enabled, checked etc. Additionally UiSelector allows targeting of UI + elements within a specific display hierarchies to distinguish similar elements + based in the hierarchies they're in.
+ + + + + + + + + + + + + + + + + + + + + + +Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + + | ++ UiSelector() + + |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ + + + + + UiSelector + | +
+ checked(boolean val)
+
+ Set the search criteria to match widgets that
+ are currently checked (usually for checkboxes).
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ childSelector(UiSelector selector)
+
+ Adds a child UiSelector criteria to this selector.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ className(String className)
+
+ Set the search criteria to match the class property
+ for a widget (for example, "android.widget.Button").
+
+ | ||||||||||
+ + + + + <T> + UiSelector + | +
+ className(Class<T> type)
+
+ Set the search criteria to match the class property
+ for a widget (for example, "android.widget.Button").
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ classNameMatches(String regex)
+
+ Set the search criteria to match the class property
+ for a widget (for example, "android.widget.Button").
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ clickable(boolean val)
+
+ Set the search criteria to match widgets that are clickable.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ description(String desc)
+
+ Set the search criteria to match the content-description
+ property for a widget.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ descriptionContains(String desc)
+
+ Set the search criteria to match the content-description
+ property for a widget.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ descriptionMatches(String regex)
+
+ Set the search criteria to match the content-description
+ property for a widget.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ descriptionStartsWith(String desc)
+
+ Set the search criteria to match the content-description
+ property for a widget.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ enabled(boolean val)
+
+ Set the search criteria to match widgets that are enabled.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ focusable(boolean val)
+
+ Set the search criteria to match widgets that are focusable.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ focused(boolean val)
+
+ Set the search criteria to match widgets that have focus.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ fromParent(UiSelector selector)
+
+ Adds a child UiSelector criteria to this selector which is used to
+ start search from the parent widget.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ index(int index)
+
+ Set the search criteria to match the widget by its node
+ index in the layout hierarchy.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ instance(int instance)
+
+ Set the search criteria to match the
+ widget by its instance number.
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ longClickable(boolean val)
+
+ Set the search criteria to match widgets that are long-clickable.
+
+ | ||||||||||
+ + + + + + 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 + | +
+ 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
+ for a widget (for example, the text label to launch an app).
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ textContains(String text)
+
+ Set the search criteria to match the visible text displayed
+ for a widget (for example, the text label to launch an app).
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ textMatches(String regex)
+
+ Set the search criteria to match the visible text displayed
+ for a widget (for example, the text label to launch an app).
+
+ | ||||||||||
+ + + + + + UiSelector + | +
+ textStartsWith(String text)
+
+ Text property is usually the widget's visible text on the display.
+
+ | ||||||||||
+ + + + + + String + | ++ toString() + + |
+ [Expand]
+ Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+
+From class
+
+ java.lang.Object
+
+
+
+
+
+
+ |
Set the search criteria to match widgets that + are currently checked (usually for checkboxes). + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Adds a child UiSelector criteria to this selector. + + Use this selector to narrow the search scope to + child widgets under a specific parent widget.
Set the search criteria to match the class property + for a widget (for example, "android.widget.Button").
className | +Value to match | +
---|
Set the search criteria to match the class property + for a widget (for example, "android.widget.Button").
Set the search criteria to match the class property + for a widget (for example, "android.widget.Button").
Set the search criteria to match widgets that are clickable. + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Set the search criteria to match the content-description + property for a widget. + + The content-description is typically used + by the Android Accessibility framework to + provide an audio prompt for the widget when + the widget is selected. The content-description + for the widget must match exactly + with the string in your input argument. + + Matching is case-sensitive.
desc | +Value to match | +
---|
Set the search criteria to match the content-description + property for a widget. + + The content-description is typically used + by the Android Accessibility framework to + provide an audio prompt for the widget when + the widget is selected. The content-description + for the widget must contain + the string in your input argument. + + Matching is case-insensitive.
desc | +Value to match | +
---|
Set the search criteria to match the content-description + property for a widget. + + The content-description is typically used + by the Android Accessibility framework to + provide an audio prompt for the widget when + the widget is selected. The content-description + for the widget must match exactly + with the string in your input argument.
Set the search criteria to match the content-description + property for a widget. + + The content-description is typically used + by the Android Accessibility framework to + provide an audio prompt for the widget when + the widget is selected. The content-description + for the widget must start + with the string in your input argument. + + Matching is case-insensitive.
desc | +Value to match | +
---|
Set the search criteria to match widgets that are enabled. + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Set the search criteria to match widgets that are focusable. + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Set the search criteria to match widgets that have focus. + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Adds a child UiSelector criteria to this selector which is used to + start search from the parent widget. + + Use this selector to narrow the search scope to + sibling widgets as well all child widgets under a parent.
Set the search criteria to match the widget by its node
+ index in the layout hierarchy.
+
+ The index value must be 0 or greater.
+
+ Using the index can be unreliable and should only
+ be used as a last resort for matching. Instead,
+ consider using the instance(int)
method.
index | +Value to match | +
---|
Set the search criteria to match the
+ widget by its instance number.
+
+ The instance value must be 0 or greater, where
+ the first instance is 0.
+
+ For example, to simulate a user click on
+ the third image that is enabled in a UI screen, you
+ could specify a a search criteria where the instance is
+ 2, the className(String)
matches the image
+ widget class, and enabled(boolean)
is true.
+ The code would look like this:
+
+ new UiSelector().className("android.widget.ImageView")
+ .enabled(true).instance(2);
+
instance | +Value to match | +
---|
Set the search criteria to match widgets that are long-clickable. + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Set the search criteria to match the package name + of the application that contains the widget.
name | +Value to match | +
---|
Set the search criteria to match the package name + of the application that contains the widget.
Set the search criteria to match widgets that are scrollable. + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Set the search criteria to match widgets that + are currently selected. + + Typically, using this search criteria alone is not useful. + You should also include additional criteria, such as text, + content-description, or the class name for a widget. + + If no other search criteria is specified, and there is more + than one matching widget, the first widget in the tree + is selected.
val | +Value to match | +
---|
Set the search criteria to match the visible text displayed + for a widget (for example, the text label to launch an app). + + The text for the widget must match exactly + with the string in your input argument. + Matching is case-sensitive.
text | +Value to match | +
---|
Set the search criteria to match the visible text displayed + for a widget (for example, the text label to launch an app). + + The text for the widget must contain the string in + your input argument. Matching is case-sensitive.
text | +Value to match | +
---|
Set the search criteria to match the visible text displayed + for a widget (for example, the text label to launch an app). + + The text for the widget must match exactly + with the string in your input argument.
Text property is usually the widget's visible text on the display. + + Adding this to the search criteria indicates that the search performed + should match a widget with text value starting with the text parameter. + + The matching will be case-insensitive.
Represents a conditional watcher on the target device. To learn how to register a conditional
+ watcher, see UiDevice.registerWatcher()
.
+
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
+ abstract + + + + + boolean + | +
+ checkForCondition()
+
+ The testing framework calls this handler method automatically when the framework
+ is unable to find a match using the
+
+ UiSelector . |
The testing framework calls this handler method automatically when the framework is unable to find a match using the UiSelector
. When a match is not found after a predetermined time has elapsed, the framework calls the checkForCondition() method of all registered watchers on the device. You can use this method to handle known blocking issues that are preventing the test from proceeding. For example, you can check if a dialog appeared that is blocking the the test, then close the dialog or perform some other appropriate action to allow the test to continue.
The {@code uiautomator} testing framework lets you test your user interface (UI) efficiently by creating automated functional UI testcases that can be run against your app on one or more devices.
+For more information on testing with the {@code uiautomator} framework, see UI Testing.
+ +To run your testcases on the target device, you can use the {@code adb shell} command to invoke the {@code uiautomator} tool. The syntax is: +
+adb shell uiautomator runtest <jar> -c <test_class_or_method> [options] ++ +
Here’s an example:
+adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings+ +
The following table describes the subcommands and options for {@code uiautomator}.
+ + +Subcommand | +Option | +Description | +
---|---|---|
runtest |
+Required. The {@code <jar>} argument is the name of one or more JAR files that you deployed to the target device which contain your uiautomator testcases. You can list more than one JAR file by using a space as a separator. | +|
-c <test_class_or_method> |
+Required. The {@code <test_class_or_method>} argument is a list of one or more specific test classes or test methods from the JARs that you want {@code uiautomator} to run. Each class or method must be fully qualified with the package name, in one of these formats: +
|
+|
--nohup |
+Runs the test to completion on the device even if its parent process is terminated (for example, if the device is disconnected). | +|
-e <NAME> <VALUE> |
+Specify other name-value pairs to be passed to test classes. May be repeated. Note: The {@code -e} options cannot be combined; you must prefix each option with a separate {@code -e} flag. |
+|
-e debug [true|false] |
+Wait for debugger to connect before starting. | +|
-e runner [CLASS] |
+Use the specified test runner class instead. If unspecified, the {@code uiautomator} framework’s default runner will be used. | +|
dump |
+[file] |
+Generate an XML file with a dump of the current UI hierarchy. If a filepath is not specified, by default, the generated dump file is stored on the device in this location {@code /storage/sdcard0/window_dump.xml}. | +
events |
++ | Prints out accessibility events to the console until the connection to the device is terminated | +
The {@code uiautomator} API is bundled in the {@code uiautomator.jar} file under the {@code <android-sdk>/platforms/} directory. The API includes these key classes, interfaces, and exceptions that allow you to capture and manipulate UI components on the target app:
+ +Class | +Description | +
---|---|
Used to enumerate a container's user interface (UI) elements for the purpose of counting, or targeting a sub elements by a child's text or description. | +|
Provides access to state information about the device. You can also use this class to simulate user actions on the device, such as pressing the d-pad hardware button or pressing the Home and Menu buttons. | +|
Represents a user interface (UI) element. | +|
Provides support for searching for items in a scrollable UI container. | +|
Represents a query for one or more target UI elements on a device screen. | +
Interface | +Description | +
---|---|
Represents a conditional watcher on the target device. | +|
Provides auxiliary support for running test cases. | +|
Defines an environment for running multiple tests. All {@code uiautomator} test cases should extend this class. | +
Exception | +Description | +
---|---|
Indicates when a a {@code UiSelector} could not be matched to any UI element displayed. | +
+In addition to unit testing the individual components that make up your Android application (such as activities, services, and content providers), it is also important that you test the behavior of your application’s user interface (UI) when it is running on a device. UI testing ensures that your application returns the correct UI output in response to a sequence of user actions on a device, such as entering keyboard input or pressing toolbars, menus, dialogs, images, and other UI controls. +
++Functional or black-box UI testing does not require testers to know the internal implementation details of the app, only its expected output when a user performs a specific action or enters a specific input. This approach allows for better separation of development and testing roles in your organization. +
+One common approach to UI testing is to run tests manually and verify that the app is behaving as expected. However, this approach can be time-consuming, tedious, and error-prone. A more efficient and reliable approach is to automate the UI testing with a software testing framework. Automated testing involves creating programs to perform testing tasks (test cases) to cover specific usage scenarios, and then using the testing framework to run the test cases automatically and in a repeatable manner.
+ +The Android SDK provides the following tools to support automated, functional UI testing on your application: +
To use these tools, you must have the following versions of the Android development tools installed: +
Here's a short overview of the steps required to automate UI testing: +
Before you start writing your test cases, it's helpful to familiarize yourself with the UI components (including the views and controls) of the targeted application. You can use the {@code uiautomatorviewer} tool to take a snapshot of the foreground UI screen on any Android device that is connected to your development machine. The {@code uiautomatorviewer} tool provides a convenient visual interface to inspect the layout hierarchy and view the properties of the individual UI components that are displayed on the test device. Using this information, you can later create {@code uiautomator} tests with selector objects that target specific UI components to test.
+ + + + + + +To analyze the UI components of the application that you want to test:
+$ uiautomatorviewer
To capture a screen for analysis, click the Device Screenshot button in the GUI of the {@code uiautomatorviewer} tool.
+Note: If you have more than one device connected, specify the device for screen capture by setting the {@code ANDROID_SERIAL} environment variable: +
$ adb devices
set ANDROID_SERIAL=<device serial number>
export ANDROID_SERIAL=<device serial number>
Before using the {@code uiautomator} testing framework, complete these pre-flight tasks: +
+If you are reading this document, chances are that the Android application that you want to test has not been published yet. If you have a copy of the APK file, you can install the APK onto a test device by using the {@code adb} tool. To learn how to install an APK file using the {@code adb} tool, see the {@code adb} documentation.
+ +Before writing your {@code uiautomator} tests, first identify the UI components in the application that you want to test. Typically, good candidates for testing are UI components that are visible and that users can interact with. The UI components should also have visible text labels, {@code android:contentDescription} values, or both. + +
You can inspect the visible screen objects in an application conveniently by using the {@code uiautomatorviewer} tool. For more information about how to analyze an application screen with this tool, see the section Analyzing Your Application’s UI. For more information about the common types of UI components provided by Android, see User Interface.
+ +This step is required because the {@code uiautomator} tool depends on the accessibility features of the Android framework to execute your functional UI tests. You should include these minimum optimizations to support the {@code uiautomator} tool: +
For more information about implementing and testing accessibility, see Making Applications Accessible.
+ +Note: To identify the non-accessible components in the UI, click on the Toggle NAF Nodes option in the {@code uiautomatorviewer} tool.
+ +Generally, Android application developers get accessibility support for free, courtesy of the {@link android.view.View} and {@link android.view.ViewGroup} classes. However, some applications use custom view components to provide a richer user experience. Such custom components won't get the accessibility support that is provided by the standard Android UI components. If this applies to your application, ensure that the application developer exposes the custom drawn UI components to Android accessibility services, by implementing the {@link android.view.accessibility.AccessibilityNodeProvider} class. For more information about making custom view components accessible, see Making Applications Accessible.
+ +If you're developing in Eclipse, the Android SDK provides additional tools that help you write test cases using {@code uiautomator} and buiild your JAR file. In order to set up Eclipse to assist you, you need to create a project that includes the {@code uiautomator} client library, along with the Android SDK library. To configure Eclipse:
+If you did not configure Eclipse as your development environment, make sure that the {@code uiautomator.jar} and {@code android.jar} files from the {@code <android-sdk>/platforms/<sdk>} directory are in your Java class path.
+Once you have completed these prerequisite tasks, you're almost ready to start creating your {@code uiautomator} tests. + +
To build a test that runs in the {@code uiautomator} framework, create a test case that extends the {@code UiAutomatorTestCase} class. In Eclipse, the test case file goes under the {@code src} directory in your project. Later, you will build the test case as a JAR file, then copy this file to the test device. The test JAR file is not an APK file and resides separately from the application that you want to test on the device.
+ +Because the {@code UiAutomatorTestCase} class extends {@code junit.framework.TestCase}, you can use the JUnit {@code Assert} class to test that UI components in the app return the expected results. To learn more about JUnit, you can read the documentation on the junit.org home page.
+ +The first thing your test case should do is access the device that contains the target app. It’s also good practice to start the test from the Home screen of the device. From the Home screen (or some other starting location you’ve chosen in the target app), you can use the classes provided by the {@code uiautomator} API to simulate user actions and to test specific UI components. For an example of how to put together a {@code uiautomator} test case, see the sample test case.
+ +The {@code uiautomator} API is bundled in the {@code uiautomator.jar} file under the {@code <android-sdk>/platforms/} directory. The API includes these key classes that allow you to capture and manipulate UI components on the target app:
+Represents the device state. In your tests, you can call methods on the {@code UiDevice} instance to check for the state of various properties, such as current orientation or display size. Your tests also can use the {@code UiDevice} instance to perform device level actions, such as forcing the device into a specific rotation, pressing the d-pad hardware button, or pressing the Home and Menu buttons.
+To get an instance of {@code UiDevice} and simulate a Home button press: +
+getUiDevice().pressHome(); +
+UiObject appItem = new UiObject(new UiSelector() + .className("android.widget.ListView").instance(1) + .childSelector(new UiSelector().text("Apps"))); ++
The following code example shows how to construct {@code UiObject} instances that represent a Cancel button and a OK button in your application.
++UiObject cancelButton = new UiObject(new UiSelector().text("Cancel")); +UiObject okButton = new UiObject(new UiSelector().text("OK")); ++
You can reuse the {@code UiObject} instances that you have created in other parts of your app testing, as needed. Note that the {@code uiautomator} test framework searches the current display for a match every time your test uses a {@code UiObject} instance to click on a UI element or query a property.
+In the following code example, the {@code uiautomator} test framework searches for a UI element with the text property {@code OK}. If a match is found and if the element is enabled, the framework simulates a user click action on the element.
++if(okButton.exists() && okButton.isEnabled()) +{ + okButton.click(); +} ++
You can also restrict the search to find only elements of a specific class. For example, to find matches of the {@link android.widget.Button} class:
++UiObject cancelButton = new UiObject(new UiSelector().text("Cancel") + .className("android.widget.Button")); +UiObject okButton = new UiObject(new UiSelector().text("OK") + .className("android.widget.Button")); ++
+UiCollection videos = new UiCollection(new UiSelector() + .className("android.widget.FrameLayout")); ++
If the videos are listed within a {@link android.widget.LinearLayout} view, and you want to to retrieve the number of videos in this collection:
++int count = videos.getChildCount(new UiSelector() + .className("android.widget.LinearLayout")); ++
If you want to find a specific video that is labeled with the text element {@code Cute Baby Laughing} from the collection and simulate a user-click on the video:
++UiObject video = videos.getChildByText(new UiSelector() + .className("android.widget.LinearLayout"), "Cute Baby Laughing"); +video.click(); ++
Similarly, you can simulate other user actions on the UI object. For example, if you want +to simulate selecting a checkbox that is associated with the video:
++UiObject checkBox = video.getChild(new UiSelector() + .className("android.widget.Checkbox")); +if(!checkBox.isSelected()) checkbox.click(); ++
For example, the following code shows how to simulate scrolling down the Settings menu and clicking on an About tablet option:
++UiScrollable settingsItem = new UiScrollable(new UiSelector() + .className("android.widget.ListView")); +UiObject about = settingsItem.getChildByText(new UiSelector() + .className("android.widget.LinearLayout"), "About tablet"); +about.click() ++
For more information about these APIs, see the {@code uiautomator} reference.
+ +The following code example shows a simple test case which simulates a user bringing up the Settings app in a stock Android device. The test case mimics all the steps that a user would typically take to perform this task, including opening the Home screen, launching the All Apps screen, scrolling to the Settings app icon, and clicking on the icon to enter the Settings app.
++package com.uia.example.my; + +// Import the uiautomator libraries +import com.android.uiautomator.core.UiObject; +import com.android.uiautomator.core.UiObjectNotFoundException; +import com.android.uiautomator.core.UiScrollable; +import com.android.uiautomator.core.UiSelector; +import com.android.uiautomator.testrunner.UiAutomatorTestCase; + +public class LaunchSettings extends UiAutomatorTestCase { + + public void testDemo() throws UiObjectNotFoundException { + + // Simulate a short press on the HOME button. + getUiDevice().pressHome(); + + // We’re now in the home screen. Next, we want to simulate + // a user bringing up the All Apps screen. + // If you use the uiautomatorviewer tool to capture a snapshot + // of the Home screen, notice that the All Apps button’s + // content-description property has the value “Apps”. We can + // use this property to create a UiSelector to find the button. + UiObject allAppsButton = new UiObject(new UiSelector() + .description("Apps")); + + // Simulate a click to bring up the All Apps screen. + allAppsButton.clickAndWaitForNewWindow(); + + // In the All Apps screen, the Settings app is located in + // the Apps tab. To simulate the user bringing up the Apps tab, + // we create a UiSelector to find a tab with the text + // label “Apps”. + UiObject appsTab = new UiObject(new UiSelector() + .text("Apps")); + + // Simulate a click to enter the Apps tab. + appsTab.click(); + + // Next, in the apps tabs, we can simulate a user swiping until + // they come to the Settings app icon. Since the container view + // is scrollable, we can use a UiScrollable object. + UiScrollable appViews = new UiScrollable(new UiSelector() + .scrollable(true)); + + // Set the swiping mode to horizontal (the default is vertical) + appViews.setAsHorizontalList(); + + // Create a UiSelector to find the Settings app and simulate + // a user click to launch the app. + UiObject settingsApp = appViews.getChildByText(new UiSelector() + .className(android.widget.TextView.class.getName()), + "Settings"); + settingsApp.clickAndWaitForNewWindow(); + + // Validate that the package name is the expected one + UiObject settingsValidation = new UiObject(new UiSelector() + .packageName("com.android.settings")); + assertTrue("Unable to detect Settings", + settingsValidation.exists()); + } +} ++ +
Once you have coded your test, follow these steps to build and deploy your test JAR to your target Android test device:
+<android-sdk>/tools/android uitest-project -n <name> -t 1 -p <path>+The {@code <name>} is the name of the project that contains your {@code uiautomator} test source files, and the {@code <path>} is the path to the corresponding project directory. +
set ANDROID_HOME=<path_to_your_sdk>+
export ANDROID_HOME=<path_to_your_sdk>+
ant build
adb push <path_to_output_jar> /data/local/tmp/+
Here’s an example:
adb push ~/dev/workspace/LaunchSettings/bin/LaunchSettings.jar /data/local/tmp/+
Here’s an example of how to run a test that is implemented in the {@code LaunchSettings.jar} file. The tests are bundled in the {@code com.uia.example.my} package:
+adb shell uiautomator runtest LaunchSettings.jar -c com.uia.example.my.LaunchSettings+
To learn more about the syntax, subcommands, and options for {@code uiautomator}, see the {@code uiautomator} reference.
+ + +Here are some best practices for functional UI testing with the {@code uiautomator} framework:
+