Skip to content

Most visited

Recently visited

navigation

Espresso

public final class Espresso
extends Object

java.lang.Object
   ↳ android.support.test.espresso.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 closeSoftKeyboard()

Closes soft keyboard if open.

static List<IdlingResource> getIdlingResources()

This method is deprecated. use getResources() instead.

static DataInteraction onData(Matcher<? extends Object> dataMatcher)

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

static void onIdle()

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 onView(Matcher<View> viewMatcher)

Creates a ViewInteraction for a given view.

static void openActionBarOverflowOrOptionsMenu(Context context)

Opens the overflow menu displayed within an ActionBar.

static void openContextualActionModeOverflowMenu()

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

static void pressBack()

Press on the back button.

static void pressBackUnconditionally()

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(IdlingResource) instead.

static void registerLooperAsIdlingResource(Looper looper)

This method is deprecated. use registerLooperAsIdlingResource(Looper) instead.

static void registerLooperAsIdlingResource(Looper looper, boolean considerWaitIdle)

This method is deprecated. use registerLooperAsIdlingResource(Looper) instead.

static void setFailureHandler(FailureHandler failureHandler)

Changes the default FailureHandler to the given one.

static boolean unregisterIdlingResources(IdlingResource... resources)

This method is deprecated. use unregister(IdlingResource) instead.

Inherited methods

From class java.lang.Object

Public methods

closeSoftKeyboard

void closeSoftKeyboard ()

Closes soft keyboard if open.

getIdlingResources

List<IdlingResource> getIdlingResources ()

This method is deprecated.
use getResources() instead.

Returns a list of all currently registered IdlingResources.

Returns
List<IdlingResource>

onData

DataInteraction onData (Matcher<? extends 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).

Parameters
dataMatcher Matcher: a matcher used to find the data object.

Returns
DataInteraction

onIdle

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(Matcher) or onData(Matcher). These Apis already use Espresso's internal synchronisation mechanisms and do not require a call to onIdle().

Throws
AppNotIdleException when app does not go Idle within the master policies timeout

onIdle

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.

Parameters
action Callable: callable executed when the app goes idle.

Returns
T the computed result of the action callable

Throws
AppNotIdleException when app does not go Idle within the master policies timeout

onView

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.

Parameters
viewMatcher Matcher: used to select the view.

Returns
ViewInteraction

See also:

openActionBarOverflowOrOptionsMenu

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.

Parameters
context Context

openContextualActionModeOverflowMenu

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

void pressBack ()

Press on the back button.

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

pressBackUnconditionally

void pressBackUnconditionally ()

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

registerIdlingResources

boolean registerIdlingResources (IdlingResource... resources)

This method is deprecated.
use register(IdlingResource) instead.

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.

Parameters
resources IdlingResource

Returns
boolean true if all resources were successfully registered

registerLooperAsIdlingResource

void registerLooperAsIdlingResource (Looper looper)

This method is deprecated.
use registerLooperAsIdlingResource(Looper) instead.

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

Parameters
looper Looper

Throws
IllegalArgumentException if looper is the main looper.

registerLooperAsIdlingResource

void registerLooperAsIdlingResource (Looper looper, 
                boolean considerWaitIdle)

This method is deprecated.
use registerLooperAsIdlingResource(Looper) instead.

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.

Parameters
looper Looper

considerWaitIdle boolean

Throws
IllegalArgumentException if looper is the main looper.

setFailureHandler

void setFailureHandler (FailureHandler failureHandler)

Changes the default FailureHandler to the given one.

Parameters
failureHandler FailureHandler

unregisterIdlingResources

boolean unregisterIdlingResources (IdlingResource... resources)

This method is deprecated.
use unregister(IdlingResource) instead.

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

Parameters
resources IdlingResource

Returns
boolean true if all resources were successfully unregistered

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)