Selecting Native Tabs while Espresso testing

╄→гoц情女王★ 提交于 2019-12-10 09:27:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!