Espresso Framework Guide

Betul Necanli
6 min readMay 8

--

Espresso is a testing framework for Android that allows you to write concise and reliable UI tests for your Android app. It’s designed to make it easy to write tests that run on the UI thread and interact with the user interface of your app.

🚀 As an Android developer, it’s important to know about testing frameworks like Espresso. Espresso is a widely used testing framework for Android applications that allows developers to test their app’s user interface (UI) and interactions with user inputs. Additionally, many companies require experience with testing frameworks like Espresso as a prerequisite for Android developer positions.

👉 Here are some key points to keep in mind when working with Espresso:

  1. Espresso is designed to be easy to use and provides fast and reliable test results.
  2. The framework provides a set of APIs for interacting with UI components such as buttons, text fields, and lists.
  3. Espresso tests run inside the same process as the app being tested, which makes them faster and more reliable.
  4. Espresso tests are written in Java or Kotlin and can be run using Android Studio’s built-in testing tools.
  5. The framework provides a set of matchers that can be used to find UI components by ID, text, or other attributes.
  6. Espresso also provides a set of actions that can be used to interact with UI components, such as clicking buttons or typing text.
  7. The framework supports advanced features such as multi-window and multi-process testing, and can be integrated with other testing frameworks such as Mockito and JUnit.

👉Here is a simple Espresso cheat sheet that summarizes some of the most common methods and concepts:

⭐️ Matchers:

withId: Matches a view with a given resource ID.

withText: Matches a view with a given text.

withContentDescription: Matches a view with a given content description.

hasTextColor: Matches a view with a given text color.

hasFocus: Matches a view that currently has focus.

isDisplayed: Matches a view that is currently displayed on the screen.

isEnabled: Matches a view that is currently enabled.

isClickable: Matches a view that is currently clickable.

⭐️Actions:

click: Performs a click on a view.

doubleClick: Performs a double-click on a view.

typeText: Types a string into a text field.

replaceText: Replaces the text in a text field.

scrollTo: Scrolls to a view that is not currently visible.

closeSoftKeyboard: Closes the soft keyboard.

pressBack: Simulates pressing the Back button.

pressMenuKey: Simulates pressing the Menu button.

swipeLeft, swipeRight, swipeUp, swipeDown: Performs a swipe gesture in the given direction.

⭐️View Actions:

actionWithAssertions: Wraps a ViewAction to perform assertions after it completes.

actionOnItemAtPosition: Performs an action on a specific item in a RecyclerView.

clickChildViewWithId: Clicks a child view with a given ID in a RecyclerView.

clickOn: Clicks a view at a specific point.

clickOnActionBarHomeButton: Clicks on the ActionBar home button.

scrollToHolder: Scrolls a RecyclerView to a ViewHolder with a specific position.

swipeDown and swipeUp: Swipes a RecyclerView in the specified direction.

👉The most common annotations used in Espresso:

  1. @RunWith(AndroidJUnit4::class): This annotation is used to specify the test runner to be used when running Espresso tests. The AndroidJUnit4 runner is used for Android tests that run on an Android device or emulator.
  2. @Before: This annotation is used to mark a method that should be run before each test method. This is typically used to set up any resources or dependencies needed for the tests.
  3. @After: This annotation is used to mark a method that should be run after each test method. This is typically used to clean up any resources or dependencies that were used during the tests.
  4. @Test: This annotation is used to mark a method as a test method. Espresso test methods should be annotated with this annotation.
  5. @Rule: This annotation is used to define rules that should be applied to each test method. For example, the ActivityTestRule is a rule that launches an activity before each test method and shuts it down after the test is complete.
  6. @UiThreadTest: This annotation is used to run a test on the UI thread. This can be useful when testing code that requires access to the UI thread.
  7. @LargeTest: This annotation is used to mark a test as a large test, which typically means that it takes longer to run and may use more resources.
  8. @MediumTest: This annotation is used to mark a test as a medium test, which typically means that it takes less time to run and uses fewer resources than a large test.
  9. @SmallTest: This annotation is used to mark a test as a small test, which typically means that it is a quick and lightweight test.

By using these annotations, you can customize the behavior of your tests and make them more efficient and effective.

👉Here’s an example of using Espresso to test an Android app in Kotlin:

In this example, we use the ActivityScenarioRule to launch the MainActivity and test it. The onView method is used to find a view by its ID. The perform method is used to perform an action on the view, such as clicking a button. The check method is used to make assertions about the view, such as checking its text.

Here, we are testing a button click in the MainActivity. We use onView to find the button and perform to click it. Then, we use onView again to find the text view and check to assert that its text is "Button clicked!".

Espresso provides a large number of additional actions and assertions that you can use to interact with and test your UI. Some of the most commonly used actions include typing text into an EditText, scrolling a RecyclerView, and opening a navigation drawer. Some of the most commonly used assertions include checking the visibility of a view, checking the text of a TextView, and checking the state of a Switch.

In conclusion, Espresso is a powerful testing framework for Android that makes it easy to write tests for your UI. By using Espresso, you can ensure that your app behaves correctly and provides a great user experience.

👉A basic example of how to use Espresso to test a simple login screen:

Let’s break this down:

  1. @RunWith(AndroidJUnit4::class) specifies that we're using the AndroidJUnit4 test runner.
  • @get:Rule defines a JUnit rule that launches the LoginActivity before each test and shuts it down afterwards.
  • onView is the main entry point to Espresso's API. We use it to find and interact with UI components.
  • withId(R.id.email_field) and withId(R.id.password_field) are Matcher objects that identify the email and password fields respectively.
  • typeText(“test@test.com”) and typeText(“password”) simulate user input by typing text into the email and password fields.
  • click() simulates a button click on the login button.
  • withId(R.id.welcome_text) is a Matcher object that identifies the welcome text that should be displayed after a successful login.
  • check(matches(isDisplayed())) verifies that the welcome text is currently displayed on the screen.
  • withText(R.string.invalid_credentials_error) is a Matcher object that identifies the error message that should be displayed when invalid credentials are entered.
  • check(matches(isDisplayed())) verifies that the error message is currently displayed on the screen.

This is just a basic example, but Espresso can handle much more complex scenarios as well.

👉Selecting an item from a list:

In this example, we use the onView method to find a list view and select the fourth item in the list by calling the actionOnItemAtPosition method. We then verify that the item details view is displayed by calling check(matches(isDisplayed())).

👉Verifying the contents of a web view:

In this example, we use the onView method to find a web view and We then use the check method to verify that the web view's current URL contains the string "google.com" by calling the withElement and hasUrl methods.

👉Performing a scroll action:

In this example, we use the onView method to find a recycler view and perform a scroll action to position 20 by calling the RecyclerViewActions.scrollToPosition method. We then verify that the item at position 20 is displayed by calling onView again with the withText matcher.

--

--