如何使用Espresso测试在选项卡布局中选择特定的选项卡位置 [英] How to select a specific Tab position in Tab Layout using Espresso testing

查看:64
本文介绍了如何使用Espresso测试在选项卡布局中选择特定的选项卡位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有View pager的选项卡布局.我正在使用 Espresso 来测试我的android应用.在我以前的项目中,我使用标签标题执行单击以选择一个标签位置,如下所示.

I have a tab layout with view pager .I am using Espresso for testing my android app. In my previous projects I use the tab title to perform click for selecting a tab Position like follows.

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

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

Right now I a have 114 tabs. So I can't use above method for randomly select these tabs for testing. Is there any way I can select tabs by its position. I already check other solutions but none of those helped me.

推荐答案

应该可以与自定义ViewAction一起使用.像这样:

Should be doable with a custom ViewAction. Something like this:

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))

这篇关于如何使用Espresso测试在选项卡布局中选择特定的选项卡位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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