{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}
@CheckReturnValue public final class ViewActions
A collection of common ViewActions.
Public constructors |
|
|---|---|
Public methods |
|
|---|---|
static ViewAction |
actionWithAssertions(ViewAction viewAction)Performs all assertions before the |
static void |
addGlobalAssertion(String name, ViewAssertion viewAssertion)Adds a |
static void |
|
static ViewAction |
Returns an action that clears text on the view. |
static ViewAction |
click(int inputDevice, int buttonState)Returns an action that clicks the view for a specific input device and button state. |
static ViewAction |
click()Same as |
static ViewAction |
click(ViewAction rollbackAction)Returns an action that performs a single click on the view. |
static ViewAction |
Returns an action that closes soft keyboard. |
static ViewAction |
Returns an action that double clicks the view. |
static ViewAction |
Returns an action that long clicks the view. |
static ViewAction |
Returns an action that opens a link matching the given link text and uri matchers. |
static ViewAction |
openLinkWithText(String linkText)Same as |
static ViewAction |
openLinkWithText(Matcher<String> linkTextMatcher)Same as |
static ViewAction |
openLinkWithUri(String uri)Same as |
static ViewAction |
openLinkWithUri(Matcher<Uri> uriMatcher)Same as |
static ViewAction |
Returns an action that clicks the back button. |
static ViewAction |
Similar to |
static ViewAction |
Returns an action that presses the current action button (next, done, search, etc) on the IME (Input Method Editor). |
static ViewAction |
pressKey(int keyCode)Returns an action that presses the key specified by the keyCode (eg. |
static ViewAction |
pressKey(EspressoKey key)Returns an action that presses the specified key with the specified modifiers. |
static ViewAction |
Returns an action that presses the hardware menu key. |
static void |
removeGlobalAssertion(ViewAssertion viewAssertion)Removes the given assertion from the set of assertions to be run before actions are performed. |
static ViewAction |
repeatedlyUntil(Returns an action that performs given |
static ViewAction |
replaceText(@Nonnull String stringToBeSet)Returns an action that updates the text attribute of a view. |
static ViewAction |
scrollTo()Returns an action that scrolls to the view. |
static ViewAction |
Similar to |
static ViewAction |
Returns an action that performs a swipe top-to-bottom across the horizontal center of the view. |
static ViewAction |
Returns an action that performs a fast swipe right-to-left across the vertical center of the view. |
static ViewAction |
Returns an action that performs a swipe left-to-right across the vertical center of the view. |
static ViewAction |
swipeUp()Returns an action that performs a swipe bottom-to-top across the horizontal center of the view. |
static ViewAction |
Returns an action that selects the view (by clicking on it) and types the provided string into the view. |
static ViewAction |
typeTextIntoFocusedView(String stringToBeTyped)Returns an action that types the provided string into the view. |
public static ViewAction actionWithAssertions(ViewAction viewAction)
Performs all assertions before the ViewActions in this class and then performs the given ViewAction
| Parameters | |
|---|---|
ViewAction viewAction |
the |
public static void addGlobalAssertion(String name, ViewAssertion viewAssertion)
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 | |
|---|---|
String name |
a name of the assertion to be added |
ViewAssertion viewAssertion |
a |
| Throws | |
|---|---|
java.lang.IllegalArgumentException java.lang.IllegalArgumentException |
if the name/viewAssertion pair is already contained in the global assertions. |
public static ViewAction clearText()
Returns an action that clears text on the view. View constraints:
public static ViewAction click(int inputDevice, int buttonState)
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:
| Parameters | |
|---|---|
int inputDevice |
source input device of the click. Example: |
int buttonState |
buttonState associated with the click. Example: |
public static ViewAction click()
Same as click(int inputDevice, int buttonState), but uses SOURCE_UNKNOWN as the inputDevice and BUTTON_PRIMARY as the buttonState.
public static ViewAction click(ViewAction rollbackAction)
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:
public static ViewAction closeSoftKeyboard()
Returns an action that closes soft keyboard. If the keyboard is already closed, it is a no-op.
public static ViewAction doubleClick()
Returns an action that double clicks the view. View preconditions:
public static ViewAction longClick()
Returns an action that long clicks the view. View preconditions:
public static ViewAction openLink(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher)
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:
public static ViewAction openLinkWithText(String linkText)
Same as openLinkWithText(Matcher<String> linkTextMatcher), but uses
is(linkText) as the linkTextMatcher.
public static ViewAction openLinkWithText(Matcher<String> linkTextMatcher)
Same as openLink(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher), but uses any(Uri.class) as the uriMatcher.
public static ViewAction openLinkWithUri(String uri)
Same as openLinkWithUri(Matcher<Uri> uriMatcher), but uses is(uri) as the uriMatcher.
public static ViewAction openLinkWithUri(Matcher<Uri> uriMatcher)
Same as openLink(Matcher<String> linkTextMatcher, Matcher<Uri> uriMatcher), but uses any(String.class) as the linkTextMatcher.
public static ViewAction pressBack()
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. |
public static ViewAction pressBackUnconditionally()
Similar to pressBack but will not throw an exception when Espresso navigates outside the application or process under test.
public static ViewAction pressImeActionButton()
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.
public static ViewAction pressKey(int keyCode)
Returns an action that presses the key specified by the keyCode (eg. Keyevent.KEYCODE_BACK).
public static ViewAction pressKey(EspressoKey key)
Returns an action that presses the specified key with the specified modifiers.
public static ViewAction pressMenuKey()
Returns an action that presses the hardware menu key.
public static void removeGlobalAssertion(ViewAssertion viewAssertion)
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. |
public static ViewAction repeatedlyUntil(
ViewAction action,
Matcher<View> desiredStateMatcher,
int maxAttempts
)
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 | |
|---|---|
ViewAction action |
action to be performed repeatedly |
Matcher<View> desiredStateMatcher |
action is performed repeatedly until view matches this view matcher |
int maxAttempts |
max number of times for which this action to be performed if view doesn't match the given view matcher |
public static ViewAction replaceText(@Nonnull String stringToBeSet)
Returns an action that updates the text attribute of a view. View preconditions:
public static ViewAction scrollTo()
Returns an action that scrolls to the view. View preconditions:
public static ViewAction slowSwipeLeft()
Similar to swipeLeft but performs a slow and deliberate swipe instead. This is useful, for example, to avoid fling. View constraints:
public static ViewAction swipeDown()
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:
public static ViewAction swipeLeft()
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:
public static ViewAction swipeRight()
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:
public static ViewAction swipeUp()
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:
public static ViewAction typeText(String stringToBeTyped)
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:
public static ViewAction typeTextIntoFocusedView(String stringToBeTyped)
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: