Skip to content

Espresso Test Recorder

Devrath edited this page May 17, 2021 · 3 revisions

About the Espresso Test Recorder tool

Espresso test recorder tool is a good mechanism to record the UI actions and generate the test class at once

How to run the tool

  • Select the test class -> click run in menu -> use Record espresso test.
  • perform the required actions in IDE.
  • Then the test class will be generated.

Disadvantages of using recorder tool

  • Generated code is not by the best practices
  • It's lengthy
  • It's generated in Java

When to use it

  • We can use it when we are learning espresso as a beginner
  • We can use it in tricky scenarios when we are not sure how to write the test code

Sample espresso test generated by the recorder tool

@LargeTest
@RunWith(AndroidJUnit4::class)
class MainActivityRecordedTest {

    @Rule
    @JvmField
    var mActivityTestRule = ActivityTestRule(MainActivity::class.java)

    @Test
    fun mainActivityRecordedTest() {
        val materialButton = onView(
            allOf(
                withId(R.id.greet_button), withText("Greet"),
                childAtPosition(
                    childAtPosition(
                        withId(android.R.id.content),
                        0
                    ),
                    0
                ),
                isDisplayed()
            )
        )
        materialButton.perform(click())
    }

    private fun childAtPosition(
        parentMatcher: Matcher<View>, position: Int
    ): Matcher<View> {

        return object : TypeSafeMatcher<View>() {
            override fun describeTo(description: Description) {
                description.appendText("Child at position $position in parent ")
                parentMatcher.describeTo(description)
            }

            public override fun matchesSafely(view: View): Boolean {
                val parent = view.parent
                return parent is ViewGroup && parentMatcher.matches(parent)
                        && view == parent.getChildAt(position)
            }
        }
    }
}
Clone this wiki locally