
- Espresso Testing Framework Tutorial
- Espresso Testing - Home
- Introduction
- Setup Instructions
- Running Tests In Android Studio
- Overview of JUnit
- Architecture
- View Matchers
- Custom View Matchers
- View Assertions
- View Actions
- Testing AdapterView
- Testing WebView
- Testing Asynchronous Operations
- Testing Intents
- Testing UI for Multiple Application
- Test Recorder
- Testing UI Performance
- Testing Accessibility
- Espresso Testing Resources
- Espresso Testing - Quick Guide
- Espresso Testing - Useful Resources
- Espresso Testing - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Espresso Testing Framework - Test Recorder
Writing test case is a tedious job. Even though espresso provides very easy and flexible API, writing test cases may be a lazy and time-consuming task. To overcome this, Android studio provides a feature to record and generate espresso test cases. Record Espresso Test is available under the Run menu.
Let us record a simple test case in our HelloWorldApp by following the steps described below,
Open the Android studio followed by HelloWorldApp application.
Click Run → Record Espresso test and select MainActivity.
The Recorder screenshot is as follows,

Click Add Assertion. It will open the application screen as shown below,

Click Hello World!. The Recorder screen to Select text view is as follows,

Again click Save Assertion This will save the assertion and show it as follows,

Click OK. It will open a new window and ask the name of the test case. The default name is MainActivityTest
Change the test case name, if necessary.
Again, click OK. This will generate a file, MainActivityTest with our recorded test case. The complete coding is as follows,
package com.tutorialspoint.espressosamples.helloworldapp; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import androidx.test.espresso.ViewInteraction; import androidx.test.filters.LargeTest; import androidx.test.rule.ActivityTestRule; import androidx.test.runner.AndroidJUnit4; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.Matchers.allOf; @LargeTest @RunWith(AndroidJUnit4.class) public class MainActivityTest { @Rule public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class); @Test public void mainActivityTest() { ViewInteraction textView = onView( allOf(withId(R.id.textView_hello), withText("Hello World!"), childAtPosition(childAtPosition(withId(android.R.id.content), 0),0),isDisplayed())); textView.check(matches(withText("Hello World!"))); } 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)); } }; } }
Finally, run the test using context menu and check whether the test case run.