public final class Espresso


Entry point to the Espresso framework. Test authors can initiate testing by using one of the on* methods (e.g. onView) or perform top-level user actions (e.g. pressBack).

Summary

Public methods

static void

Closes soft keyboard if open.

static List<IdlingResource>

This method is deprecated.

use getResources instead.

static DataInteraction

Creates an DataInteraction for a data object displayed by the application.

static void

Loops the main thread until the app goes idle.

static T
<T> onIdle(Callable<T> action)

Loops the main thread until the app goes idle.

static ViewInteraction

Creates a ViewInteraction for a given view.

static void

Opens the overflow menu displayed within an ActionBar.

static void

Opens the overflow menu displayed in the contextual options of an ActionMode.

static void

Press on the back button.

static void

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

static boolean
registerIdlingResources(IdlingResource[] resources)

This method is deprecated.

use register instead.

static void

This method is deprecated.

use registerLooperAsIdlingResource instead.

static void
registerLooperAsIdlingResource(Looper looper, boolean considerWaitIdle)

This method is deprecated.

use registerLooperAsIdlingResource instead.

static void

Changes the default FailureHandler to the given one.

static boolean
unregisterIdlingResources(IdlingResource[] resources)

This method is deprecated.

use unregister instead.

Public methods

closeSoftKeyboard

public static void closeSoftKeyboard()

Closes soft keyboard if open.

getIdlingResources

public static List<IdlingResourcegetIdlingResources()

Returns a list of all currently registered IdlingResources.

onData

@CheckReturnValue
public static DataInteraction onData(Matcher<Object> dataMatcher)

Creates an DataInteraction for a data object displayed by the application. Use this method to load (into the view hierarchy) items from AdapterView widgets (e.g. ListView).

This method builds a DataInteraction object - it does not interact with the application under test at all. It is expected that the caller use the ViewInteraction object to perform an action or assertion.

Parameters
Matcher<Object> dataMatcher

a matcher used to find the data object.

Returns
DataInteraction

a DataInteraction that will perform an action or assertion.

onIdle

public static void onIdle()

Loops the main thread until the app goes idle.

Only call this method for tests that do not interact with any UI elements, but require Espresso's main thread synchronisation! This method is mainly useful for test utilities and frameworks that are build on top of Espresso.

For UI tests use onView or onData. These APIs already use Espresso's internal synchronisation mechanisms and do not require a call to onIdle.

Throws
androidx.test.espresso.AppNotIdleException

when app does not go Idle within the master policies timeout.

java.lang.RuntimeException

when being invoked on the main thread.

onIdle

public static T <T> onIdle(Callable<T> action)

Loops the main thread until the app goes idle.

Same as onIdle, but takes an additional Callable as parameter, which is executed after the app goes idle. This works on any thread, including the main thread.

Parameters
Callable<T> action

callable executed when the app goes idle.

Returns
T

the computed result of the action callable.

Throws
androidx.test.espresso.AppNotIdleException

when app does not go Idle within the master policies timeout.

onView

@CheckReturnValue
public static ViewInteraction onView(Matcher<View> viewMatcher)

Creates a ViewInteraction for a given view. Note: the view has to be part of the view hierarchy. This may not be the case if it is rendered as part of an AdapterView (e.g. ListView). If this is the case, use Espresso.onData to load the view first.

This method builds a ViewInteraction object - it does not interact with the application under test at all. It is expected that the caller use the ViewInteraction object to perform an action or assertion.

Parameters
Matcher<View> viewMatcher

used to select the view.

See also
onData

openActionBarOverflowOrOptionsMenu

public static void openActionBarOverflowOrOptionsMenu(Context context)

Opens the overflow menu displayed within an ActionBar.

This works with both native and SherlockActionBar ActionBars.

Note the significant differences of UX between ActionMode and ActionBars with respect to overflows. If a hardware menu key is present, the overflow icon is never displayed in ActionBars and can only be interacted with via menu key presses.

openContextualActionModeOverflowMenu

public static void openContextualActionModeOverflowMenu()

Opens the overflow menu displayed in the contextual options of an ActionMode.

This works with both native and SherlockActionBar action modes.

Note the significant difference in UX between ActionMode and ActionBar overflows - ActionMode will always present an overflow icon and that icon only responds to clicks. The menu button (if present) has no impact on it.

pressBack

public static void pressBack()

Press on the back button.

Throws
androidx.test.espresso.PerformException

if currently displayed activity is root activity, since pressing back button would result in application closing.

pressBackUnconditionally

public static void pressBackUnconditionally()

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

registerIdlingResources

public static boolean registerIdlingResources(IdlingResource[] resources)

Registers one or more IdlingResources with the framework. It is expected, although not strictly required, that this method will be called at test setup time prior to any interaction with the application under test. When registering more than one resource, ensure that each has a unique name. If any of the given resources is already registered, a warning is logged.

Returns
boolean

true if all resources were successfully registered

registerLooperAsIdlingResource

public static void registerLooperAsIdlingResource(Looper looper)

Registers a Looper for idle checking with the framework. This is intended for use with non-UI thread loopers.

Throws
java.lang.IllegalArgumentException

if looper is the main looper.

registerLooperAsIdlingResource

public static void registerLooperAsIdlingResource(Looper looper, boolean considerWaitIdle)

Registers a Looper for idle checking with the framework. This is intended for use with non-UI thread loopers.

This method allows the caller to consider Thread.State.WAIT to be 'idle'.

This is useful in the case where a looper is sending a message to the UI thread synchronously through a wait/notify mechanism.

Throws
java.lang.IllegalArgumentException

if looper is the main looper.

setFailureHandler

public static void setFailureHandler(FailureHandler failureHandler)

Changes the default FailureHandler to the given one.

unregisterIdlingResources

public static boolean unregisterIdlingResources(IdlingResource[] resources)

Unregisters one or more IdlingResources. If any of the given resources are not already registered, a warning is logged.

Returns
boolean

true if all resources were successfully unregistered