如何使用浓缩咖啡测试在选项卡布局中选择特定的选项卡位置



我有一个带有view pager的选项卡布局。在我以前的项目中,我使用选项卡标题执行单击以选择如下的选项卡位置。

    Espresso.onView(ViewMatchers.withText("MAP"))
            .perform(ViewActions.click());

现在我有114个选项卡。因此,我无法使用上述方法随机选择这些选项卡进行测试。有什么方法可以通过其位置选择选项卡。我已经检查了其他解决方案,但是这些解决方案都没有帮助我。

应使用自定义ViewAction可行。这样的东西:

fun selectTabAtPosition(tabIndex: Int): ViewAction {
    return object : ViewAction {
        override fun getDescription() = "with tab at index $tabIndex"
        override fun getConstraints() = allOf(isDisplayed(), isAssignableFrom(TabLayout::class.java))
        override fun perform(uiController: UiController, view: View) {
            val tabLayout = view as TabLayout
            val tabAtIndex: TabLayout.Tab = tabLayout.getTabAt(tabIndex)
                    ?: throw PerformException.Builder()
                            .withCause(Throwable("No tab at index $tabIndex"))
                            .build()
            tabAtIndex.select()
        }
    }
}

和用法:

onView(withId(R.id.tab_layout)).perform(selectTabAtPosition(99))

作为没有切换/使用Kotlin的人,这就是Java。它基本上与@be_negative相同,因此只需以相同的方式使用它。

@NonNull
private static ViewAction selectTabAtPosition(final int position) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return allOf(isDisplayed(), isAssignableFrom(TabLayout.class));
        }
        @Override
        public String getDescription() {
            return "with tab at index" + String.valueOf(position);
        }
        @Override
        public void perform(UiController uiController, View view) {
            if (view instanceof TabLayout) {
                TabLayout tabLayout = (TabLayout) view;
                TabLayout.Tab tab = tabLayout.getTabAt(position);
                if (tab != null) {
                    tab.select();
                }
            }
        }
    };
}

以上两个答案都不错。对我来说,选择标签后,我还想通过与标题匹配来验证它是正确的选项卡。由于这篇文章是关于浓缩咖啡测试的,因此我将在这里共享代码片段,因为它可能对某人有帮助。

fun matchCurrentTabTitle(tabTitle: String): Matcher<View> {
return object : TypeSafeMatcher<View>() {
    override fun describeTo(description: Description?) {
        description?.appendText("unable to match title of current selected tab with $tabTitle")
    }
    override fun matchesSafely(item: View?): Boolean {
        val tabLayout = item as TabLayout
        val tabAtIndex: TabLayout.Tab = tabLayout.getTabAt(tabLayout.selectedTabPosition)
                ?: throw PerformException.Builder()
                        .withCause(Throwable("No tab at index ${tabLayout.selectedTabPosition}"))
                        .build()
        return tabAtIndex.text.toString().contains(tabTitle, true)
    }
  }
}
fun matchTabTitleAtPosition(tabTitle: String, tabIndex: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
    override fun describeTo(description: Description?) {
        description?.appendText("unable to select tab at index $tabIndex and match title with $tabTitle")
    }
    override fun matchesSafely(item: View?): Boolean {
        val tabLayout = item as TabLayout
        val tabAtIndex: TabLayout.Tab = tabLayout.getTabAt(tabIndex)
                ?: throw PerformException.Builder()
                        .withCause(Throwable("No tab at index $tabIndex"))
                        .build()
        return tabAtIndex.text.toString().contains(tabTitle, true)
    }
  }
}

最新更新