Espresso Test Recorder – How to Use It in Your Project?

Espresso Test Recorder – How to Use It in Your Project?

With the release of Android Studio version 2.2, Google has given developers a lot of new features. One of them is Espresso Test Recorder. What is it and how it makes running tests easier?

Espresso test Recorder

Espresso is a framework for writing UI tests for our applications. It is a part of Android Support Testing Library. With Espresso Test Recorder creating tests is much easier and faster than writing them yourself.

Our task is to perform actions as if using the application in a usual way and then add assertions that evaluate whether the application is working properly. At the end of the so-called recording, generated is a test code launched in the same way as a test you would have written.

Set up

To get started with Test Recorder, we need to add the following dependencies to our app/build.gradle file:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'

In the same file, add the following in the android.defaultConfig section:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Test recording

After such preparation we can start recording the test. From the top menu choose Run -> Record Espresso Test.

Espresso screen recorder panel

Select the device on which you want to run the application, it can be both an emulator or a physical device.

At this point it is worth noting that, for tests recording to work properly, we need to turn off animations in the development options.

We should now see a window containing all the interactions and assertions that we have done / added. At the beginning it will obviously be empty.

Espresso screen recorder panel

Recording

Select the device on which you want to run the application, it can be both an emulator or a physical device. At this point it is worth noting that, for tests recording to work properly, we need to turn off animations in the development options. We should now see a window containing all the interactions and assertions that we have done / added. At the beginning it will obviously be empty.

Record your test tab on Espresso screen recorder

After each performed interaction the list will be updated.

Record your test tab on Espresso screen recorder with one assertion

At any time we can select any view and then add an assertion assessing whether or not it is in proper condition, by clicking on the button “Add Assertion”. Unfortunately, the choice of assertions is not very wide, and is confined to, “exists” or “does not exist”, for all the views, in addition to the “text is” for TextView and its subclasses.

Record your test tab on Espresso screen recorder - app preview

After performing all actions and adding all the assertions, we can click “Complete Recording” button. Upon completion of the recording, generated will be a code file for our test.

@RunWith(AndroidJUnit4.class)
public class MainActivityTest3 {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void mainActivityTest3() {
        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.button_1), withText("Button 1"),
                        withParent(allOf(withId(R.id.activity_main),
                                withParent(withId(android.R.id.content)))),
                        isDisplayed()));
        appCompatButton.perform(click());

        ViewInteraction textView = onView(
                allOf(withId(R.id.text_tv), withText("Button 1"),
                        childAtPosition(
                                allOf(withId(R.id.activity_main),
                                        childAtPosition(
                                                withId(android.R.id.content),
                                                0)),
                                0),
                        isDisplayed()));
        textView.check(matches(withText("Button 1")));

    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

Espresso Test Recorder is definitely an interesting and promising tool. Unfortunately, mainly due to the limitations associated with a small number of assertions to be used, at the moment it does not allow for creating more complex tests. However, I encourage you to check how it works and track its progress. In the future, it can even completely replace the traditional way of creating UI tests.

Grzesiek

Grzesiek Lipski

Android Developer

Learn more

5 Reasons Why You Should Build the UI in ConstraintLayout

ConstraintLayout is a popular solution that Android developers use every day. And not without a good reason. If you’re at the beginning of your journey as a software engineer, get familiar with the most important features this layout can offer. It will help you understand the way it works, and Android app development will become easier.

Read more

Bitrise Tests Made Easier: Update JIRA Issues with Build Number and Forget About Delays in QA Testing

When you trigger Bitrise build with changes and forget to tell QA specialists some essential information, there are two likely scenarios. You waste time waiting for the update from the tester, only you don’t know it’s never coming. Or you get so many questions about builds that you can’t keep up. Sounds familiar? If so, let us tell you about the JIRA issue update that keeps workflow in order.

Read more

How to Implement Feature Flags and Gain More Control over Your App?

Every person with software development experience can share a few stories about incorrectly working features. We build our apps out of small pieces, introduce advanced architecture patterns, and yet sometimes one of the elements fails, causing bugs or even system failures. In such cases, feature toggling can save the day! Find out how to implement feature flags, and make your app more stable.

Read more

Project estimation

Let us know what product you want to build and how we can help you.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2017

Nomination in
M-COMMERCE category

Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

Mobile Trends Awards logo

Mobile Trends Awards 2020

Nomination in SPORT & RECREATION category

17

client reviews

Clutch logo