【问题标题】:Check fragment visibility after clicking on Tab from TabLayout that only contains icon using Android Espresso从仅包含使用 Android Espresso 的图标的 TabLayout 中单击 Tab 后检查片段可见性
【发布时间】:2018-12-04 08:27:58
【问题描述】:

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

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

我使用了来自 this 链接的信息,该链接帮助我开始创建匹配器并执行点击。但是,我的测试失败并出现以下错误:

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

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

【问题讨论】:

    标签: android android-espresso android-tablayout android-tabs android-instrumentation


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多