Issue
I have a tab layout with view pager .I am using Espresso for testing my android app. In my previous projects I use the tab title to perform click for selecting a tab Position like follows.
Espresso.onView(ViewMatchers.withText("MAP"))
.perform(ViewActions.click());
Right now I a have 114 tabs. So I can't use above method for randomly select these tabs for testing. Is there any way I can select tabs by its position. I already check other solutions but none of those helped me.
Solution
Should be doable with a custom ViewAction. Something like this:
fun selectTabAtPosition(tabIndex: Int): ViewAction {
return object : ViewAction {
override fun getDescription() = "with tab at index $tabIndex"
override fun getConstraints() = allOf(isDisplayed(), isAssignableFrom(TabLayout::class.java))
override fun perform(uiController: UiController, view: View) {
val tabLayout = view as TabLayout
val tabAtIndex: TabLayout.Tab = tabLayout.getTabAt(tabIndex)
?: throw PerformException.Builder()
.withCause(Throwable("No tab at index $tabIndex"))
.build()
tabAtIndex.select()
}
}
}
and the usage:
onView(withId(R.id.tab_layout)).perform(selectTabAtPosition(99))
Answered By - Be_Negative
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.