????У????
????Ui Automator ?????InstrumentationTestCase?????InstrumentationTestCase?????????JUnit Assert?????????????????????JUnit Assert???ж?????
??????????????????????????????????????????
private static final String CALC_PACKAGE = "com.myexample.calc";
public void testTwoPlusThreeEqualsFive() {
// Enter an equation: 2 + 3 = ?
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("two")).click();
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("plus")).click();
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("three")).click();
mDevice.findObject(new UiSelector()
.packageName(CALC_PACKAGE).resourceId("equals")).click();
// Verify the result = 5
UiObject result = mDevice.findObject(By.res(CALC_PACKAGE?? "result"));
assertEquals("5"?? result.getText());
}
????Api
????UiDevice
void    clearLastTraversedText()
// Clears the text from the last UI traversal event.
// ??????UI???????????
boolean click(int x?? int y)
// Perform a click at arbitrary coordinates specified by the user
// ??????????
boolean drag(int startX?? int startY?? int endX?? int endY?? int steps)
// Performs a swipe from one coordinate to another coordinate.
// ???
void    dumpWindowHierarchy(File dest)
// Dump the current window hierarchy to a File.
// dump??????λ??????????
void    dumpWindowHierarchy(OutputStream out)
// Dump the current window hierarchy to an OutputStream.
// dump??????λ?????????
void    dumpWindowHierarchy(String fileName)
// This method is deprecated. Use dumpWindowHierarchy(File) or dumpWindowHierarchy(OutputStream) instead.
// dump??????λ??????????
UiObject2   findObject(BySelector selector)
// Returns the first object to match the selector criteria.
// ????BySelector????
UiObject    findObject(UiSelector selector)
// Returns a UiObject which represents a view that matches the specified selector criteria.
// ????UiSelector ????
List<UiObject2> findObjects(BySelector selector)
// Returns all objects that match the selector criteria.
// ????BySelector????
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
// ??????Activity????????????????
String  getCurrentPackageName()
// Retrieves the name of the last package to report accessibility events.
// ??????package
int getDisplayHeight()
// Gets the height of the display?? in pixels.
int getDisplayRotation()
// Returns the current rotation of the display?? as defined in Surface
Point   getDisplaySizeDp()
// Returns the display size in dp (device-independent pixel) The returned display size is adjusted per screen rotation.
int getDisplayWidth()
// Gets the width of the display?? in pixels.
static UiDevice getInstance()
// This method is deprecated. Should use getInstance(Instrumentation) instead. This version hides UiDevice's dependency on having an Instrumentation reference and is prone to misuse.
// ??????????
static UiDevice getInstance(Instrumentation instrumentation)
// Retrieves a singleton instance of UiDevice
String  getLastTraversedText()
// Retrieves the text from the last UI traversal event received.
// ???????α????????
String  getLauncherPackageName()
// Retrieves default launcher package name
// ??????е?packagename
String  getProductName()
// Retrieves the product name of the device.
boolean hasAnyWatcherTriggered()
// Checks if any registered UiWatcher have triggered.
// ???????д?????????
boolean hasObject(BySelector selector)
// Returns whether there is a match for the given selector criteria.
// ????з??????????
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 openNotification()
// Opens the notification shade.
// ????
boolean openQuickSettings()
// Opens the Quick Settings shade.
// ??????
<R> R   performActionAndWait(Runnable action?? EventCondition<R> condition?? long timeout)
// Performs the provided action and waits for the condition to be met.
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    setCompressedLayoutHeirarchy(boolean compressed)
// Enables or disables layout hierarchy compression.
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(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 swipe(Point[] segments?? int segmentSteps)
// Performs a swipe between points in the Point array.
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.
<R> R   wait(SearchCondition<R> condition?? long timeout)
// Waits for given the condition to be met.
void    waitForIdle(long timeout)
// 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.
// ???????
????UiObject
void    clearTextField()
// Clears the existing text contents in an editable field.
// ?????????
boolean click()
// Performs a click at the center of the visible bounds of the UI element represented by this UiObject.
// ???
boolean clickAndWaitForNewWindow()
// Waits for window transitions that would typically take longer than the usual default timeouts.
// ?????????????
boolean clickAndWaitForNewWindow(long timeout)
// Performs a click at the center of the visible bounds of the UI element represented by this UiObject and waits for window transitions.
// ????????????棬?????????
boolean clickBottomRight()
// Clicks the bottom and right corner of the UI element
// ??????±?
boolean clickTopLeft()
// Clicks the top and left corner of the UI element
boolean dragTo(UiObject destObj?? int steps)
// Drags this object to a destination UiObject.
// ???
boolean dragTo(int destX?? int destY?? int steps)
// Drags this object to arbitrary coordinates.
boolean exists()
// Check if view exists.
// ?ж???????
Rect    getBounds()
// Returns the view's bounds property.
// ??????
UiObject    getChild(UiSelector selector)
// Creates a new UiObject for a child view that is under the present UiObject.
// ????????????????
int getChildCount()
// Counts the child views immediately under the present UiObject.
// ????????????
String  getClassName()
// Retrieves the className property of the UI element.
// ??????????class name
String  getContentDescription()
// Reads the content_desc property of the UI element
UiObject    getFromParent(UiSelector selector)
// Creates a new UiObject for a sibling view or a child of the sibling view?? relative to the present UiObject.
String  getPackageName()
// Reads the view's 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 view.
// ?????????
boolean isCheckable()
// Checks 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()
// Checks if the UI element's clickable property is currently true.
boolean isEnabled()
// Checks 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 view's long-clickable property is currently true
boolean isScrollable()
// Check if the view's scrollable property is currently true
boolean isSelected()
// Checks 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 performMultiPointerGesture(PointerCoords... touches)
// Performs a multi-touch gesture.
boolean performTwoPointerGesture(Point startPoint1?? Point startPoint2?? Point endPoint1?? Point endPoint2?? int steps)
// Generates a two-pointer gesture with arbitrary starting and ending points.
boolean pinchIn(int percent?? int steps)
// Performs a two-pointer gesture?? where each pointer moves diagonally toward the other?? from the edges to the center of this UiObject .
boolean pinchOut(int percent?? int steps)
// Performs a two-pointer gesture?? where each pointer moves diagonally opposite across the other?? from the center out towards the edges of the this UiObject.
boolean setText(String text)
// Sets the text in an editable field?? after clearing the field's content.
// ????????????
boolean swipeDown(int steps)
// Performs the swipe down action on the UiObject.
boolean swipeLeft(int steps)
// Performs the swipe left action on the UiObject.
boolean swipeRight(int steps)
// Performs the swipe right action on the UiObject.
boolean swipeUp(int steps)
// Performs the swipe up action on the UiObject.
boolean waitForExists(long timeout)
// Waits a specified length of time for a view to become visible.
boolean waitUntilGone(long timeout)
// Waits a specified length of time for a view to become undetectable.
???????
???????
????* ????????в????????????????????
????* ??????????????????????????????????????豸??????????~?APP??????~?????????~????????
????* ????????λ?????robotium??????
???????
????* Ui Automator???android level 16?????????????????level 16???????API????????uiautomator????
????* ?????????resource-id??λ??????????level 18??????????
????* ??????????????????????????????jar????????