-
Notifications
You must be signed in to change notification settings - Fork 0
Espresso Test Recorder
Devrath edited this page May 17, 2021
·
3 revisions
Espresso test recorder tool is a good mechanism to record the UI actions and generate the test class at once
- Select the test class -> click
run
in menu -> useRecord espresso test
. - perform the required actions in IDE.
- Then the test class will be generated.
- Generated code is not by the best practices
- It's lengthy
- It's generated in Java
- 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
@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)
}
}
}
}