ViewActions


@CheckReturnValue
class ViewActions


A collection of common ViewActions.

Summary

Public functions

java-static ViewAction!

Performs all assertions before the ViewActions in this class and then performs the given ViewAction

java-static Unit
addGlobalAssertion(name: String!, viewAssertion: ViewAssertion!)

Adds a ViewAssertion to be run every time a ViewAction in this class is performed.

java-static Unit
java-static ViewAction!

Returns an action that clears text on the view.

java-static ViewAction!

Same as click(int inputDevice, int buttonState), but uses SOURCE_UNKNOWN as the inputDevice and BUTTON_PRIMARY as the buttonState.

java-static ViewAction!
click(rollbackAction: ViewAction!)

Returns an action that performs a single click on the view.

java-static ViewAction!
click(inputDevice: Int, buttonState: Int)

Returns an action that clicks the view for a specific input device and button state.

java-static ViewAction!

Returns an action that closes soft keyboard.

java-static ViewAction!

Returns an action that double clicks the view.

java-static ViewAction!

Returns an action that long clicks the view.

java-static ViewAction!
openLink(linkTextMatcher: Matcher<String!>!, uriMatcher: Matcher<Uri!>!)

Returns an action that opens a link matching the given link text and uri matchers.

java-static ViewAction!

Same as openLinkWithText(Matcher<String> linkTextMatcher), but uses is(linkText) as the linkTextMatcher.

java-static ViewAction!
openLinkWithText(linkTextMatcher: Matcher<String!>!)

Same as openLink(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher), but uses any(Uri.class) as the uriMatcher.

java-static ViewAction!

Same as openLinkWithUri(Matcher<Uri> uriMatcher), but uses is(uri) as the uriMatcher.

java-static ViewAction!
openLinkWithUri(uriMatcher: Matcher<Uri!>!)

Same as openLink(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher), but uses any(String.class) as the linkTextMatcher.

java-static ViewAction!

Returns an action that clicks the back button.

java-static ViewAction!

Similar to pressBack but will not throw an exception when Espresso navigates outside the application or process under test.

java-static ViewAction!

Returns an action that presses the current action button (next, done, search, etc) on the IME (Input Method Editor).

java-static ViewAction!

Returns an action that presses the specified key with the specified modifiers.

java-static ViewAction!
pressKey(keyCode: Int)

Returns an action that presses the key specified by the keyCode (eg.

java-static ViewAction!

Returns an action that presses the hardware menu key.

java-static Unit

Removes the given assertion from the set of assertions to be run before actions are performed.

java-static ViewAction!
repeatedlyUntil(
    action: ViewAction!,
    desiredStateMatcher: Matcher<View!>!,
    maxAttempts: Int
)

Returns an action that performs given ViewAction on the view until view matches the desired Matcher<View>.

java-static ViewAction!
replaceText(@Nonnull stringToBeSet: String!)

Returns an action that updates the text attribute of a view.

java-static ViewAction!

Returns an action that scrolls to the view.

java-static ViewAction!

Similar to swipeLeft but performs a slow and deliberate swipe instead.

java-static ViewAction!

Returns an action that performs a swipe top-to-bottom across the horizontal center of the view.

java-static ViewAction!

Returns an action that performs a fast swipe right-to-left across the vertical center of the view.

java-static ViewAction!

Returns an action that performs a swipe left-to-right across the vertical center of the view.

java-static ViewAction!

Returns an action that performs a swipe bottom-to-top across the horizontal center of the view.

java-static ViewAction!
typeText(stringToBeTyped: String!)

Returns an action that selects the view (by clicking on it) and types the provided string into the view.

java-static ViewAction!
typeTextIntoFocusedView(stringToBeTyped: String!)

Returns an action that types the provided string into the view.

Public functions

actionWithAssertions

java-static fun actionWithAssertions(viewAction: ViewAction!): ViewAction!

Performs all assertions before the ViewActions in this class and then performs the given ViewAction

Parameters
viewAction: ViewAction!

the ViewAction to perform after the assertions

addGlobalAssertion

java-static fun addGlobalAssertion(name: String!, viewAssertion: ViewAssertion!): Unit

Adds a ViewAssertion to be run every time a ViewAction in this class is performed. The assertion will be run prior to performing the action.

Parameters
name: String!

a name of the assertion to be added

viewAssertion: ViewAssertion!

a ViewAssertion to be added

Throws
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException

if the name/viewAssertion pair is already contained in the global assertions.

clearGlobalAssertions

java-static fun clearGlobalAssertions(): Unit

clearText

java-static fun clearText(): ViewAction!

Returns an action that clears text on the view. View constraints:

  • must be displayed on screen

click

java-static fun click(): ViewAction!

Same as click(int inputDevice, int buttonState), but uses SOURCE_UNKNOWN as the inputDevice and BUTTON_PRIMARY as the buttonState.

click

java-static fun click(rollbackAction: ViewAction!): ViewAction!

Returns an action that performs a single click on the view.

If the click takes longer than the 'long press' duration (which is possible) the provided rollback action is invoked on the view and a click is attempted again.

This is only necessary if the view being clicked on has some different behaviour for long press versus a normal tap.

For example - if a long press on a particular view element opens a popup menu - ViewActions.pressBack() may be an acceptable rollback action. View constraints:

  • must be displayed on screen
  • any constraints of the rollbackAction

click

java-static fun click(inputDevice: Int, buttonState: Int): ViewAction!

Returns an action that clicks the view for a specific input device and button state.

Note: Not supported by API <14. An UnsupportedOperationException will be thrown if called on API <14. For API <14, call click instead. View constraints:

  • must be displayed on screen
Parameters
inputDevice: Int

source input device of the click. Example: SOURCE_MOUSE

buttonState: Int

buttonState associated with the click. Example: BUTTON_PRIMARY

closeSoftKeyboard

java-static fun closeSoftKeyboard(): ViewAction!

Returns an action that closes soft keyboard. If the keyboard is already closed, it is a no-op.

doubleClick

java-static fun doubleClick(): ViewAction!

Returns an action that double clicks the view. View preconditions:

  • must be displayed on screen

longClick

java-static fun longClick(): ViewAction!

Returns an action that long clicks the view. View preconditions:

  • must be displayed on screen

openLink

java-static fun openLink(linkTextMatcher: Matcher<String!>!, uriMatcher: Matcher<Uri!>!): ViewAction!

Returns an action that opens a link matching the given link text and uri matchers. The action is performed by invoking the link's onClick method (as opposed to actually issuing a click on the screen). View preconditions:

  • must be displayed on screen
  • must be assignable from TextView
  • must have links

openLinkWithText

java-static fun openLinkWithText(linkText: String!): ViewAction!

Same as openLinkWithText(Matcher<String> linkTextMatcher), but uses is(linkText) as the linkTextMatcher.

openLinkWithText

java-static fun openLinkWithText(linkTextMatcher: Matcher<String!>!): ViewAction!

Same as openLink(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher), but uses any(Uri.class) as the uriMatcher.

openLinkWithUri

java-static fun openLinkWithUri(uri: String!): ViewAction!

Same as openLinkWithUri(Matcher<Uri> uriMatcher), but uses is(uri) as the uriMatcher.

openLinkWithUri

java-static fun openLinkWithUri(uriMatcher: Matcher<Uri!>!): ViewAction!

Same as openLink(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher), but uses any(String.class) as the linkTextMatcher.

pressBack

java-static fun pressBack(): ViewAction!

Returns an action that clicks the back button.

Throws
androidx.test.espresso.PerformException: androidx.test.espresso.PerformException

if Espresso navigates outside the application or process under test.

pressBackUnconditionally

java-static fun pressBackUnconditionally(): ViewAction!

Similar to pressBack but will not throw an exception when Espresso navigates outside the application or process under test.

pressImeActionButton

java-static fun pressImeActionButton(): ViewAction!

Returns an action that presses the current action button (next, done, search, etc) on the IME (Input Method Editor). The selected view will have its onEditorAction method called.

pressKey

java-static fun pressKey(key: EspressoKey!): ViewAction!

Returns an action that presses the specified key with the specified modifiers.

pressKey

java-static fun pressKey(keyCode: Int): ViewAction!

Returns an action that presses the key specified by the keyCode (eg. Keyevent.KEYCODE_BACK).

pressMenuKey

java-static fun pressMenuKey(): ViewAction!

Returns an action that presses the hardware menu key.

removeGlobalAssertion

java-static fun removeGlobalAssertion(viewAssertion: ViewAssertion!): Unit

Removes the given assertion from the set of assertions to be run before actions are performed.

Parameters
viewAssertion: ViewAssertion!

the assertion to remove

Throws
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException

if the name/viewAssertion pair is not already contained in the global assertions.

repeatedlyUntil

java-static fun repeatedlyUntil(
    action: ViewAction!,
    desiredStateMatcher: Matcher<View!>!,
    maxAttempts: Int
): ViewAction!

Returns an action that performs given ViewAction on the view until view matches the desired Matcher<View>. It will repeat the given action until view matches the desired Matcher<View> or PerformException will be thrown if given number of unsuccessful attempts are made.

Parameters
action: ViewAction!

action to be performed repeatedly

desiredStateMatcher: Matcher<View!>!

action is performed repeatedly until view matches this view matcher

maxAttempts: Int

max number of times for which this action to be performed if view doesn't match the given view matcher

replaceText

java-static fun replaceText(@Nonnull stringToBeSet: String!): ViewAction!

Returns an action that updates the text attribute of a view. View preconditions:

  • must be displayed on screen
  • must be assignable from EditText

scrollTo

java-static fun scrollTo(): ViewAction!

Returns an action that scrolls to the view. View preconditions:

  • must be a descendant of ScrollView
  • must have visibility set to View.VISIBLE

slowSwipeLeft

java-static fun slowSwipeLeft(): ViewAction!

Similar to swipeLeft but performs a slow and deliberate swipe instead. This is useful, for example, to avoid fling. View constraints:

  • must be displayed on screen

swipeDown

java-static fun swipeDown(): ViewAction!

Returns an action that performs a swipe top-to-bottom across the horizontal center of the view. The swipe doesn't start at the very edge of the view, but has a bit of offset. View constraints:

  • must be displayed on screen

swipeLeft

java-static fun swipeLeft(): ViewAction!

Returns an action that performs a fast swipe right-to-left across the vertical center of the view. slowSwipeLeft should be used instead if a slow deliberate swipe is needed (e.g. to avoid fling). The swipe doesn't start at the very edge of the view, but is a bit offset. View constraints:

  • must be displayed on screen

swipeRight

java-static fun swipeRight(): ViewAction!

Returns an action that performs a swipe left-to-right across the vertical center of the view. The swipe doesn't start at the very edge of the view, but is a bit offset. View constraints:

  • must be displayed on screen

swipeUp

java-static fun swipeUp(): ViewAction!

Returns an action that performs a swipe bottom-to-top across the horizontal center of the view. The swipe doesn't start at the very edge of the view, but has a bit of offset. View constraints:

  • must be displayed on screen

typeText

java-static fun typeText(stringToBeTyped: String!): ViewAction!

Returns an action that selects the view (by clicking on it) and types the provided string into the view. Appending a \n to the end of the string translates to a ENTER key event. Note: this method performs a tap on the view before typing to force the view into focus, if the view already contains text this tap may place the cursor at an arbitrary position within the text. View preconditions:

  • must be displayed on screen
  • must support input methods

typeTextIntoFocusedView

java-static fun typeTextIntoFocusedView(stringToBeTyped: String!): ViewAction!

Returns an action that types the provided string into the view. Appending a \n to the end of the string translates to a ENTER key event. Note: this method does not change cursor position in the focused view - text is inserted at the location where the cursor is currently pointed. View preconditions:

  • must be displayed on screen
  • must support input methods
  • must be already focused