ActivityTestRule(MainActivity::class.java) @Rule @JvmField var mGrantPermissionRule = GrantPermissionRule.grant( "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION") @Test fun mainActivityTest() { val appCompatButton = onView( allOf(withId(android.R.id.button1), withText("Accept"), childAtPosition( childAtPosition( withId(R.id.buttonPanel), 0), 3))) appCompatButton.perform(scrollTo(), click()) val bottomNavigationItemView = onView( allOf(withId(R.id.action_games), withContentDescription("Games"), childAtPosition( childAtPosition( withId(R.id.tabs), 0), 1), isDisplayed())) bottomNavigationItemView.perform(click()) val floatingActionButton = onView( allOf(withId(R.id.createNewGameButton), childAtPosition( allOf(withId(R.id.games_container), childAtPosition( withClassName(`is`("android.widget.FrameLayout")), 0)), 2), isDisplayed())) floatingActionButton.perform(click()) val materialCardView = onView( allOf(childAtPosition( allOf(withId(R.id.list), childAtPosition( withId(R.id.swipeRefreshListLayout), 0)), 2), isDisplayed())) materialCardView.perform(click()) val materialCardView2 = onView( allOf(childAtPosition( allOf(withId(R.id.list), childAtPosition( withId(R.id.swipeRefreshListLayout), 0)), 2), isDisplayed())) materialCardView2.perform(click()) val materialCardView3 = onView( allOf(childAtPosition( allOf(withId(R.id.list), childAtPosition( withId(R.id.swipeRefreshListLayout), 0)), 4), isDisplayed())) materialCardView3.perform(click()) val appCompatButton2 = onView( allOf(withId(R.id.gameType), withText("Stroke Play"), childAtPosition( allOf(withId(R.id.bottomMenu), childAtPosition( withClassName(`is`("androidx.coordinatorlayout.widget.CoordinatorLayout")), 2)), 0), isDisplayed())) appCompatButton2.perform(click()) val appCompatRadioButton = onView( allOf(withId(R.id.matchPlay), withText("Match play\nTwo players"), childAtPosition( childAtPosition( withClassName(`is`("android.widget.ScrollView")), 0), 2))) appCompatRadioButton.perform(scrollTo(), 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) } } } }