top of page

Espresso Framework for Android Devices


Form Looking for a reliable and easy to use testing framework i have stumbled upon Android's very own espresso framework.

Below are the Details mentioned about the testing framework.

Turn off animations on your test device — leaving system animations turned on in the test device might cause unexpected results or may lead your test to fail.

1. Window Animation Scale

2. Transition animation Scale

3. Animator Duration scale

Setting Up Espresso

1. Add the following dependencies in the build.gradle file,

2. Install the Platform tools from the Android SDK manager.

Espresso Action Commands :

  • ViewActions.click(): Clicks on the view.

  • ViewActions.typeText(): Clicks on a view and enters a specified string.

  • ViewActions.scrollTo(): Scrolls to the view. The target view must be subclassed from ScrollView and the value of its android:visibilityproperty must be VISIBLE. For views that extend AdapterView (for example, ListView), the onData() method takes care of scrolling for you.

  • ViewActions.pressKey(): Performs a key press using a specified keycode.

  • ViewActions.clearText(): Clears the text in the target view.

Espresso View Matchers command :

Calling methods in the ViewMatchers class. For example, to find a view by looking for a text string it displays, you can call a method like this:

onView(withText("Sign-in"));

Similarly you can call withId() and providing the resource ID (R.id) of the view, as shown in the following example:

onView(withId(R.id.button_signin));

Espresso View Assertion command :

The ViewAssertions class provides a list of helper methods for specifying common assertions. The assertions you can use include:

  • doesNotExist: Asserts that there is no view matching the specified criteria in the current view hierarchy.

  • matches: Asserts that the specified view exists in the current view hierarchy and its state matches some given Hamcrest matcher.

  • selectedDescendentsMatch: Asserts that the specified children views for a parent view exist, and their state matches some given Hamcrest matcher.

Simple Explanation of Usage :

onView(withId(R.id.my_view)) // withId(R.id.my_view) is a ViewMatcher .perform(click()) // click() is a ViewAction .check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion

Sample Test Script :

This Test script is used for automation the subtraction function in the Sample Calculator application

Espresso Test Recorder :

Steps to start Espresso Test Recorder,

Step 1 : Click Run --> Record Espresso Test Recorder

Step 2 : Select the deployment target. It can be your target device or the virtual device you have installed.

Step 3 : Espresso will trigger the build of the project and the app will install before the launch of the espresso test recorder.

So once you do your actions in the app , all of those gets recorded in the in the recorder engine

Creation of Test Report :

Step 1 : Run the Test as shown below,

Step 2 : Once the test is completed we can see our test results in our run window below,

Step 4 : Click on the export results button shown below,

We can export the results in the HTML format or XML or we can apply our own style sheets on that.

Sample Report (Test Case Passed) :

Sample Report (Test Case Failed) :

Reference :

https://developer.android.com/studio/test/espresso-test-recorder.html

bottom of page