I'm trying to record an espresso test in AndroidStudio 4.2. The recording seems to work fine. However, when I try to run the recorded tests they run about halfway until there is an activity switch, then it fails because the views aren't found and I keep getting NoMatchingViewException
.
I do a bunch of clicks in the first activity, which triggers the loading of a second activity. When running the test trying to find the view in the second activity never works.
@Test
fun firstRecordedTest() {
val materialButton = onView(
allOf(
withId(R.id.key1Button), withText("1"),
childAtPosition(
childAtPosition(
withClassName(`is`("android.widget.LinearLayout")),
0
),
3
),
isDisplayed()
)
)
materialButton.perform(click())
// Activity switches here
// The following never works
val matcher: Matcher<View> = allOf(
withId(R.id.tab_contacts), withContentDescription("Contacts"),
childAtPosition(
childAtPosition(
withId(R.id.navigation_view),
0
),
1
),
isDisplayed()
)
val bottomNavigationItemView = onView(matcher)
bottomNavigationItemView.perform(click())
}
I have tried to put different kinds of timers etc to wait for the view to load, but either it gets stuck or it fails. I have also tried this answer with no luck.
Am I missing something? Is it not possible to continue a test after switching activity even if the espresso recorder allows it?