What is Espresso?
- It automatically synchronises your test actions with user interface of your application.
- Framework also, ensure that your activity is started before the test runs. It also, lets the test wait until all observed background activities have finished
- The framework prevents direct access to activities and views of the application because holding on to these objects and operating on them off the UI thread is a major source of test flakiness. Thus, you will not see methods like
getView()andgetCurrentActivity()in the Espresso API. - You can still safely operate on views by implementing your own subclasses of
ViewActionandViewAssertion.
UI test for single apps:
- verify that target app behaves as expected when user performs a specific action or enters specific input in activities
- it allows you to check that target app returns the correct UI output in response to user interactions in the app activities
- Espresso allows you to simulate user actions and test complex app user interactions
UI test for multiple apps:
- this type of test provide the correct behaviour of interaction b/w different user apps or between user apps and system apps.
- eg. you want to test that your camera app share images correctly with 3rd party social media application or with default android photo application
- UI Automator allows you to create such scenarios
API components
To Automate, we need 3 basic things i.e. find something, do something and check something
The main components of Espresso include the following:
- Espresso – Entry point to interactions with views (via
onView()andonData()). Also exposes APIs that are not necessarily tied to any view, such aspressBack(). - ViewMatchers –
- Allows to find view in current view hierarchy (Find Something).
- A collection of objects that implement the
Matcher<? super View>interface. You can pass one or more of these to theonView()method to locate a view within the current view hierarchy. - ViewActions –
- allows to perform actions on the views (Do Something).
- A collection of
ViewActionobjects that can be passed to theViewInteraction.perform()method, such asclick(). - ViewAssertions –
- allows to alert state of a view (Check Something)
- A collection of
ViewAssertionobjects that can be passed theViewInteraction.check()method. Most of the time, you will use the matches assertion, which uses a View matcher to assert the state of the currently selected view.
Espresso Test Recorder
Pros of Espresso Test Recorder
- Allows to create effective UI based test cases with user interactions
- we can capture assertions and interactions without accessing app's structure directly which increases execution speed and optimises test cases.
- It saves lot of time to search for locators and then writing test cases.
- It supports multiple assertions making more reliable test cases.
Cons of Espresso Test Recorder
- Currently does not Support Recording of Web Views interactions
- Once we complete Recording once for next time Recording it launches the app there is no API to control such behaviour.
- Cann't record Assertions for Toast Messages.
Pros
- Supported for all android versions
- simpler and quicker to setup
- supports Jacoco to measure code coverage
- extensive testing is possible as it covers many UI actions and gestures (toast messages, camera activity, etc.)
Cons
- The test cases are written inside android application project code and hence building project to run test case takes lot of time.
Gradle is used to give instructions for building your project based on instructions that you give in build.gradle file.
Chapter 1. Introduction to Espresso
What is AndroidX?
Chapter 2. Setting up Espresso
Chapter 3. Writing the First Test
https://developer.android.com/training/testing/espresso/cheat-sheet







