Skip to content

Instantly share code, notes, and snippets.

@igor-brishkoski
Created December 18, 2018 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igor-brishkoski/2c0ac0b4ec661ce050c23c1671db023e to your computer and use it in GitHub Desktop.
Save igor-brishkoski/2c0ac0b4ec661ce050c23c1671db023e to your computer and use it in GitHub Desktop.
@Test
fun mainActivityTest() {
val bottomNavigationItemView = onView(
allOf(
withId(R.id.navigation_dashboard), withContentDescription("Dashboard"),
childAtPosition(
childAtPosition(
withId(R.id.navigation),
0
),
1
),
isDisplayed()
)
)
bottomNavigationItemView.perform(click())
val bottomNavigationItemView2 = onView(
allOf(
withId(R.id.navigation_notifications), withContentDescription("Notifications"),
childAtPosition(
childAtPosition(
withId(R.id.navigation),
0
),
2
),
isDisplayed()
)
)
bottomNavigationItemView2.perform(click())
val textView = onView(
allOf(
withId(R.id.message), withText("Notifications"),
childAtPosition(
allOf(
withId(R.id.container),
childAtPosition(
withId(android.R.id.content),
0
)
),
0
),
isDisplayed()
)
)
textView.check(matches(withText("Notifications")))
val bottomNavigationItemView3 = onView(
allOf(
withId(R.id.navigation_home), withContentDescription("Home"),
childAtPosition(
childAtPosition(
withId(R.id.navigation),
0
),
0
),
isDisplayed()
)
)
bottomNavigationItemView3.perform(click())
val textView2 = onView(
allOf(
withId(R.id.message), withText("Home"),
childAtPosition(
allOf(
withId(R.id.container),
childAtPosition(
withId(android.R.id.content),
0
)
),
0
),
isDisplayed()
)
)
textView2.check(matches(withText("Home")))
}
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)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment