Check fragment visibility after clicking on Tab from TabLayout that only contains icon using Android Espresso

拟墨画扇 提交于 2019-12-01 20:33:50

My test passed by doing the following:

I create an id file under res/values named ids.xml where I created the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="tab_h_id" type="id"/>
    <item name="tab_m_id" type="id"/>
</resources>

Then within my actual activity I added the following under my Tablayout:

for (int i = 0; i< tabLayout.getTabCount(); i++) {
        TabLayout.Tab mTab = tabLayout.getTabAt(i);
        if (mTab != null) {
            switch (i){
                case 0:
                    View tabViewH = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
                    tabViewH.setId(R.id.tab_h_id); //assigning id for tab view for Espresso testing
                    mTab.setIcon(R.drawable.home_icon_svg);
                    break;
                case 1:
                    View tabViewM = ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(i);
                    tabViewM.setId(R.id.tab_m_id); //assigning id for tab view for Espresso testing
                    mTab.setIcon(R.drawable.l_svg);
                    break;
                case 2:
                    //etc..
            }
        }
    }

Then in my Espresso test I'm using withId instead of withTagValue:

@Test
public void checkIfMFragmentIsVisible() {
    Matcher<View> matcher = allOf(withId(R.id.tab_m_id),
            isDescendantOfA(withId(R.id.homeTabs)));
    onView(matcher).perform(click());
    onView(withId(R.id.mFragmentLayout)).check(matches(isCompletelyDisplayed()));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!