本文介绍了如何使用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测试在选项卡布局中选择特定的选项卡位置
发布评论