使用Android Espresso,在仅包含图标的TabLayout中点击选项卡后检查片段的可见性

3

我想要检查在点击标签页后,我的片段是否可见。标签页是通过视图分页设置的。

这是我的实际活动代码,在活动的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()));
}

我使用了这个链接中的信息,帮助我开始创建匹配器并执行点击操作。然而,我的测试失败,并出现以下错误:

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}

成功的仪器测试:

我在我的活动选项卡中添加了以下内容,进行了虚拟测试:

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()));
}

这个测试成功了,但是我不想在我的活动中使用文本,而是想使用标签或其他东西。

1个回答

2

通过以下步骤,我的测试已经通过:

我在 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>

然后在我的实际活动中,在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()));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接