问题
I have a viewpager which utilizes the native actionbar Tabs as the indicator. I would like to navigate to different tabs, but Tab component is not a view therefore, onView, or withText does not work properly with perform clicks.
Is there a specific way I can do to navigate through the Tab navigation?
回答1:
You could use swiping to navigate between your tabs:
onView(withId(R.id.viewpager)).perform(swipeLeft());
onView(withId(R.id.viewpager)).perform(swipeRight());
回答2:
Assuming there is text on the tab, you can do:
onView(withText("Tab Text")).perform(click())
onView, withText and click are static imports.
回答3:
I created the following method to select any tab with the text on view.
public MainScreen clickOnTab(String tabText) {
onView(allOf(withClassName(endsWith("TabView")),
withChild(withText(tabText)),
withParent(withParent(withId(R.id.main_activity_tab_layout)))
)).perform(scrollTo()).perform(click());
return this;
}
and in order to call this method simply call the method like below:
MainScreen.getInstance().clickOnTab("Tab Name");
Cheers
来源:https://stackoverflow.com/questions/25124632/selecting-native-tabs-while-espresso-testing