使用 Android Espresso 从仅包含图标的 TabLayout 单击 Tab 后检查片段可见性

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

我正在尝试检查我的片段在从我的 tabLayout 中单击我的选项卡后是否可见,该选项卡已使用 view pager 设置。

这是我的实际 activity 代码,在我的 activity onCreate 方法中:

mViewPager = findViewById(R.id.contentFrameLayout);
mViewPager.setAdapter(mSectionPageAdapter);
mViewPager.setPagingEnabled(false);

//Set up the tab layout to display tabs
tabLayout = findViewById(R.id.homeTabs);
tabLayout.setupWithViewPager(mViewPager);

for (int i = 0; i< tabLayout.getTabCount(); i++) {
        TabLayout.Tab mTab = tabLayout.getTabAt(i);
        if (mTab != null) {
            switch (i){
                case 0:
                    mTab.setTag(WFragment.class.toString());
                    mTab.setIcon(R.drawable.home_icon_svg);
                    break;
                case 1:
                    mTab.setTag(MFragment.class.toString());
                    mTab.setIcon(R.drawable.l_svg);
                    break;
                case 2:
                    //etc..
            }
        }
    }

这是我的仪器测试:

@Test
public void checkIfMFragmentIsVisible() {
    Matcher<View> matcher = allOf(withTagValue(is((Object) MFragment.class.toString())),
            isDescendantOfA(withId(R.id.homeTabs)));
    onView(matcher).perform(click());
    onView(withId(R.id.mFragmentLayout)).check(matches(isCompletelyDisplayed()));
}

我使用了 link 中的信息,这些信息帮助我开始创建匹配器并执行点击。但是,我的测试失败并出现以下错误:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with tag value: is "class com.test.solution.fragments.MFragment" and is descendant of a: with id: 2131296345)
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:com.google.maps.api.android.lib6.impl.ce{d55b63a G.ED..C.. ......I. 0,0-0,0}

仪器测试成功:

我通过在 activity 的 我的选项卡中添加以下内容来尝试进行虚拟测试:

TabLayout.Tab mTab = tabLayout.getTabAt(i);
            if (mTab != null) {
                switch (i){
                    case 0:
                        mTab.setText("case 0");
                        //etc..
                    case 1:
                        mTab.setText("case 1");
                        //etc..
                    case 2:
                        //etc..

在我的仪器测试中:

@Test
public void checkIfMFragmentIsVisible() {
    Matcher<View> matcher = allOf(withText("case 1"),
                isDescendantOfA(withId(R.id.homeTabs)));
        onView(matcher).perform(click());
        onView(withId(R.id.mFragmentLayout)).check(matches(isCompletelyDisplayed()));
}

测试成功,但是,我不想在 activity 中使用文本,而是想使用标签或其他东西。

我通过执行以下操作通过了测试:

我在 res/values 下创建了一个名为 ids.xml 的 ID 文件,我在其中创建了以下内容:

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

然后在我的 实际 activity 中,我在 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..
            }
        }
    }

然后在我的 Espresso 测试中 我使用 withId 而不是 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()));
}