Selecting Native Tabs while Espresso testing

China☆狼群 提交于 2019-12-05 15:48:21
Eduard Kotysh

You could use swiping to navigate between your tabs:

onView(withId(R.id.viewpager)).perform(swipeLeft());
onView(withId(R.id.viewpager)).perform(swipeRight());

Assuming there is text on the tab, you can do:

onView(withText("Tab Text")).perform(click())

onView, withText and click are static imports.

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

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