Espresso ☕ | A test framework

Asanka Vithanage
2 min readMar 22, 2018

--

Espresso is the Android’s test automation framework (open source) provided by Google for testing reliability of UI, that is for UI test case writing the help of JUnit4.(other libraries are also exists)

Main Features include, Automatic synchronization with UI, that is espresso do wait (sleep)until all the pixels of UI is completely rendered on to the view or it continuously inspect the UI thread until the that becomes idle. No need of writing thread.sleep(x) anymore with Espresso. Apart from this main feature, espresso is easy to setup, provides fast execution and many more benefits.

Three main components under @test: 1. View matchers- Finds/locate the UI elements that is called a view, 2.View Actions-Perform actions on the UI elements like click on button, scroll down, double click or whatever action. 3.View Assertions-Check what you expect with the actual as usual.

  1. Viewmatchers-Following are the basic ways to catch views using espresso although we can use a combination of properties/ events to catch the the UI element/object if we couldn't find the element using a single property/event.

viewInteraction viewById = onView(withId(R.id.loginId));

viewInteraction viewByText = onView(withText(“Hello World”));

viewInteraction viewByText = onView(withContentdescription(“example content”));

viewInteraction viewByHintText = onView(withHint(“login hint text”));

viewInteraction viewBySpinnerText = onView(withSpinnerText(“moving text on screen”));

viewInteraction viewhavingLinks = onView(hasLinks());

Example combinations of viewmachers: We can select all of the elements with id as loginId which are clickable and having a text as “Hello world” as follows.

onView(allof(withId(R.id.loginId), isClickable(), WithText(“Hello World”)));

similarly, there are a lot of events and properties of elements we can combine to pick the target.For more check the link https://developer.android.com/reference/android/support/test/espresso/matcher/ViewMatchers.html

2. ViewActions: Once the target element is selected we can do some action using the Perform method.

Example:

viewInteraction ClickableElement= onView(allof(withId(R.id.loginId), isClickable(), WithText(“Hello World”)));

clickableElement.perform(click()); or

we can write the entire thing in a single statement as

onView(allof(withId(R.id.loginId), isClickable(), WithText(“Hello World”))).perform(click());

similarly we can typeText, cleartext, replacetext, doubleclick, scrolTo and the whatever you think of.

3. ViewAssertions: Use .check() method with viewIntercation object.You may have perform an action on that viewInteraction object or may not.As example you can check a particular object is displayed by using

onView(withId(R.id.loginId)).check(matches(isDisplayed()));

or

onView(viewInteraction).Perform(viewAction).check(viewAssertion);

  • When to use OnData instead of onView? Whenever with adapter view, i.e views that comes with data and layout such as gridview, Listview and spinners etc. are the adapter views in android and if we use onView instead of onData NoMatchingViewExceptioncan be seen.

For more checkout the Espresso cheat sheet: https://developer.android.com/training/testing/espresso/cheat-sheet.html

--

--

Asanka Vithanage
Asanka Vithanage

Written by Asanka Vithanage

Software Quality Assurance Professional, Problem Solver, SOA Tester, Automation Engineer, CI/CD Practitioner, DevOps enthusiast

No responses yet