Espresso
\
What is Espresso?
Espresso is a frameworke use to write reusable UI test for Android, this framework also ensures that your activity is started before the tests run. Espresso test wait until all background activities have finished. It also could test a single or test across applications.
Example:
@Test
public void greeterSaysHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"));
onView(withId(R.id.greet_button)).perform(click());
onView(withText("Hello Steve!")).check(matches(isDisplayed()));
}
What is synchronization capabilities of Espresso:
When the test invokes onView(), Espresso check the following:
- The message queue is empty.
- There are no instances of
AsyncTaskcurrently executing a task. - All developer-defined
idling resourcesare idle.
What are the Packages in Espresso?
espresso-core: This package contains core and basic View matchers, actions, and assertions.espresso-web: This package contains resources for WebView support.espresso-idling-resource: For synchronization with background jobs.espresso-contrib: This package containDatePicker,RecyclerViewandDraweractions, accessibility checks, and CountingIdlingResource.espresso-intents: For hermetic testing.espresso-remote: Location of Espresso’s multi-process functionality.
What are the basic components on Espresso?
ViewMatchers: allows inding view in the current view hierarchy.ViewActions: allows performing actions on the views.ViewAssertions: allows asserting state of a view.
Espresso basics:
What are the main components of Espresso include?
- Espresso :
onView()andonData(). exposes APIs that are not tied to any view, such aspressBack(). - ViewMatchers – A collection of objects that implement the Matcher<? super View> interface.
- ViewActions – ViewAction objects that can be passed to the ViewInteraction.perform() method.
- ViewAssertions – ViewAssertion objects that can be passed the ViewInteraction.check() method.
Example:
onView(withId(R.id.my_view))
.perform(click())
.check(matches(isDisplayed()));
Create UI tests with Espresso Test Recorder:
- Record UI interactions:
- Click Run > Record Espresso Test.
- In the Select Deployment Target window, choose the device on which you want to record the test. If necessary, create a new Android Virtual Device. Click OK.
- Espresso Test Recorder triggers a build of your project, and the app must install and launch before Espresso Test Recorder allows you to interact with it.