使用Android Espresso在TabLayout中单击仅包含图标的Tab后,检查片段的可见性 [英] Check fragment visibility after clicking on Tab from TabLayout that only contains icon using Android Espresso

查看:184
本文介绍了使用Android Espresso在TabLayout中单击仅包含图标的Tab后,检查片段的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的tabLayout中单击我的选项卡后查看片段是否可见,该tabLayout是使用view pager设置的.

I'm trying to check if my fragment is visible after performing a click on my tab from my tabLayout which has been set up with view pager.

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

我使用了链接中的信息,该链接帮助我开始创建匹配器并执行点击.但是,我的测试失败并显示以下错误:

I used information from this link which helped me getting started with creating a matcher and performing a click. However, my test is failing with the following error:

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}

成功的乐器测试:

我通过在活动的我的标签中添加以下内容来尝试进行虚拟测试:

I tried a dummy test by adding the following in my tabs in my 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()));
}

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

This test was successful, however, I do not want to use a text in my activity, but I want to use a tag or something else.

推荐答案

我的测试通过以下操作通过:

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

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>

然后在我的实际活动中,我在Tablayout下添加了以下内容:

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..
            }
        }
    }

然后在我的 Espresso测试中,我使用的是withId而不是withTagValue:

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

这篇关于使用Android Espresso在TabLayout中单击仅包含图标的Tab后,检查片段的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆