# Espresso

Artifact: [androidx.test.espresso:espresso-core](https://developer.android.com/jetpack/androidx/releases/test-espresso) [View Source](https://cs.android.com/search?q=file:androidx/test/espresso/Espresso.java+class:androidx.test.espresso.Espresso)

*** ** * ** ***


```
class Espresso
```

<br />

*** ** * ** ***

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 functions |
|---|---|
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#closeSoftKeyboard()()` Closes soft keyboard if open. |
| `java-static (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource!>!` | `[getIdlingResources](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#getIdlingResources())()` **This function is deprecated.** use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#getResources()` instead. <br /> |
| `java-static https://developer.android.com/reference/kotlin/androidx/test/espresso/DataInteraction!` | `@https://docs.oracle.com/javase/8/docs/api/javax/annotation/CheckReturnValue.html https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onData(org.hamcrest.Matcher<? extends java.lang.Object>)(dataMatcher: https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!>!)` Creates an `https://developer.android.com/reference/kotlin/androidx/test/espresso/DataInteraction` for a data object displayed by the application. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onIdle()()` Loops the main thread until the app goes idle. |
| `java-static T!` | `<T> https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onIdle(java.util.concurrent.Callable<T>)(action: https://developer.android.com/reference/java/util/concurrent/Callable.html<T!>!)` Loops the main thread until the app goes idle. |
| `java-static https://developer.android.com/reference/kotlin/androidx/test/espresso/ViewInteraction!` | `@https://docs.oracle.com/javase/8/docs/api/javax/annotation/CheckReturnValue.html https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onView(org.hamcrest.Matcher<android.view.View>)(viewMatcher: https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/view/View.html!>!)` Creates a `https://developer.android.com/reference/kotlin/androidx/test/espresso/ViewInteraction` for a given view. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#openActionBarOverflowOrOptionsMenu(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html!)` Opens the overflow menu displayed within an ActionBar. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#openContextualActionModeOverflowMenu()()` Opens the overflow menu displayed in the contextual options of an ActionMode. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#pressBack()()` Press on the back button. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#pressBackUnconditionally()()` Similar to `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#pressBack()` but will **not** throw an exception when Espresso navigates outside the application or process under test. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `[registerIdlingResources](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#registerIdlingResources(androidx.test.espresso.IdlingResource...))(resources: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource!>!)` **This function is deprecated.** use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#register(androidx.test.espresso.IdlingResource...)` instead. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[registerLooperAsIdlingResource](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#registerLooperAsIdlingResource(android.os.Looper))(looper: https://developer.android.com/reference/android/os/Looper.html!)` **This function is deprecated.** use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#registerLooperAsIdlingResource(android.os.Looper)` instead. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[registerLooperAsIdlingResource](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#registerLooperAsIdlingResource(android.os.Looper,boolean))(looper: https://developer.android.com/reference/android/os/Looper.html!, considerWaitIdle: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` **This function is deprecated.** use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#registerLooperAsIdlingResource(android.os.Looper)` instead. <br /> |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#setFailureHandler(androidx.test.espresso.FailureHandler)(failureHandler: https://developer.android.com/reference/kotlin/androidx/test/espresso/FailureHandler!)` Changes the default `https://developer.android.com/reference/kotlin/androidx/test/espresso/FailureHandler` to the given one. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `[unregisterIdlingResources](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#unregisterIdlingResources(androidx.test.espresso.IdlingResource...))(resources: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource!>!)` **This function is deprecated.** use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#unregister(androidx.test.espresso.IdlingResource...)` instead. <br /> |

## Public functions

### closeSoftKeyboard

```
java-static fun closeSoftKeyboard(): Unit
```

Closes soft keyboard if open.

### getIdlingResources

```
java-static fun [getIdlingResources](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#getIdlingResources())(): (Mutable)List<IdlingResource!>!
```

> [!CAUTION]
> **This function is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#getResources()` instead.

Returns a list of all currently registered `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource`s.

### onData

```
@CheckReturnValue
java-static fun onData(dataMatcher: Matcher<Any!>!): DataInteraction!
```

Creates an `https://developer.android.com/reference/kotlin/androidx/test/espresso/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 |
|---|---|
| `dataMatcher: https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!>!` | a matcher used to find the data object. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/DataInteraction!` | a DataInteraction that will perform an action or assertion. |

### onIdle

```
java-static fun onIdle(): Unit
```

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 `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onView(org.hamcrest.Matcher<android.view.View>)` or `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onData(org.hamcrest.Matcher<? extends java.lang.Object>)`. These APIs already use Espresso's internal synchronisation mechanisms and do not require a call to `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onIdle()`.

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/AppNotIdleException` | when app does not go Idle within the master policies timeout. |
| `https://developer.android.com/reference/java/lang/RuntimeException.html` | when being invoked on the main thread. |

### onIdle

```
java-static fun <T> onIdle(action: Callable<T!>!): T!
```

Loops the main thread until the app goes idle.

Same as `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onIdle()`, but takes an additional `https://developer.android.com/reference/java/util/concurrent/Callable.html` as parameter, which is executed after the app goes idle. This works on any thread, including the main thread.

| Parameters |
|---|---|
| `action: https://developer.android.com/reference/java/util/concurrent/Callable.html<T!>!` | callable executed when the app goes idle. |

| Returns |
|---|---|
| `T!` | the computed result of the action callable. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/AppNotIdleException` | when app does not go Idle within the master policies timeout. |

### onView

```
@CheckReturnValue
java-static fun onView(viewMatcher: Matcher<View!>!): ViewInteraction!
```

Creates a `https://developer.android.com/reference/kotlin/androidx/test/espresso/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 |
|---|---|
| `viewMatcher: https://junit.org/junit4/javadoc/latest/org/hamcrest/Matcher.html<https://developer.android.com/reference/android/view/View.html!>!` | used to select the view. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#onData(org.hamcrest.Matcher<? extends java.lang.Object>)` |   |

### openActionBarOverflowOrOptionsMenu

```
java-static fun openActionBarOverflowOrOptionsMenu(context: Context!): Unit
```

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

```
java-static fun openContextualActionModeOverflowMenu(): Unit
```

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

```
java-static fun pressBack(): Unit
```

Press on the back button.

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/espresso/PerformException` | if currently displayed activity is root activity, since pressing back button would result in application closing. |

### pressBackUnconditionally

```
java-static fun pressBackUnconditionally(): Unit
```

Similar to `https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#pressBack()` but will **not** throw an exception when Espresso navigates outside the application or process under test.

### registerIdlingResources

```
java-static fun [registerIdlingResources](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#registerIdlingResources(androidx.test.espresso.IdlingResource...))(resources: Array<IdlingResource!>!): Boolean
```

> [!CAUTION]
> **This function is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#register(androidx.test.espresso.IdlingResource...)` instead.

Registers one or more `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource`s 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 |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `true` if all resources were successfully registered |

### registerLooperAsIdlingResource

```
java-static fun [registerLooperAsIdlingResource](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#registerLooperAsIdlingResource(android.os.Looper))(looper: Looper!): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#registerLooperAsIdlingResource(android.os.Looper)` instead.

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

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if looper is the main looper. |

### registerLooperAsIdlingResource

```
java-static fun [registerLooperAsIdlingResource](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#registerLooperAsIdlingResource(android.os.Looper,boolean))(looper: Looper!, considerWaitIdle: Boolean): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#registerLooperAsIdlingResource(android.os.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.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if looper is the main looper. |

### setFailureHandler

```
java-static fun setFailureHandler(failureHandler: FailureHandler!): Unit
```

Changes the default `https://developer.android.com/reference/kotlin/androidx/test/espresso/FailureHandler` to the given one.

### unregisterIdlingResources

```
java-static fun [unregisterIdlingResources](https://developer.android.com/reference/kotlin/androidx/test/espresso/Espresso#unregisterIdlingResources(androidx.test.espresso.IdlingResource...))(resources: Array<IdlingResource!>!): Boolean
```

> [!CAUTION]
> **This function is deprecated.**   
>
> use `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingRegistry#unregister(androidx.test.espresso.IdlingResource...)` instead.

Unregisters one or more `https://developer.android.com/reference/kotlin/androidx/test/espresso/IdlingResource`s. If any of the given resources are not already registered, a warning is logged.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `true` if all resources were successfully unregistered |