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.

Learn more

ConstraintLayout, or not so fast, after all…

Recently, while browsing through news from Android world I came across a concept of ConstraintLayout. It is a new layout delivered by Android and Google, supporting Android versions from API 9 on. Digging into possibilities it is to give, I decided to check how new Layout Builder behaves and what is ConstraintLayout like in use.
Read more

Creating BuildVariants with Gradle

When creating an application, sometimes at some point we want to get several versions of our application that differ in some more or less important details. To easily obtain different versions of your application, just make a few changes in Gradle.
Read more

Custom Quick Tiles Settings in Android 7

In recent editions of Android system, settings underwent a small revolution. The main settings screen has been reorganized, new options have been added. Android Lollipop introduced shortcuts to quickly switch most important settings so that they are visible immediately after expanding the upper panel. Android Nougat, along with the API 24, provided us with the ability to add our own settings there, related to our application.
Read more

Project estimation

Check out how we use our knowledge in practice, and make your project with us.

Why choose us?

Logo Mobile Trends Awards

Mobile Trends Awards 2017

Nomination in M-commerce category

17

clients reviews

Clutch logo
Logo Legalni bukmacherzy

Legal Bookmakers Award 2019

Best Mobile App

60+

projects in portfolio