【发布时间】: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