Skip to content

Espresso concepts

Devrath edited this page May 17, 2021 · 10 revisions
Title
The espresso formulae
Why to use onView instead of findViewById
Fluent API
Hamcrest Matcher
Hamcrest cheat sheet

The espresso formulae

  • The espresso formulae consist of three parts ViewMatcher<--->ViewAction<--->ViewAssertion
onView(ViewMatcher)   // Locate a view on the screen. 
.perform(ViewAction)  // Perform a action on the screen like clicking, typing etc.
.check(Assertion)     // Also we can can check that whether it works as expected.

Why to use onView instead of findViewById

  • This is because of synchronization.
  • By using onView, the espresso will wait for the application to become idle and then perform the test, Otherwise, the application will be flaky. This ensures that the views are properly rendered and there are no pending async tasks
  • If we use findViewById, the app loses the synchronization event that may trigger before the UI is rendered.

Fluent API

  • Once we have a reference with the help of OnView we can chain any number of actions until it is pertaining to that particular view because each action returns a view-interaction.

Hamcrest Matcher

We can use the hamcrest matcher in combination with the espresso to perform certain test actions.

Clone this wiki locally