如何使用Espresso测试在选项卡布局中选择特定的选项卡位置

编程入门 行业动态 更新时间:2024-10-27 16:28:26
本文介绍了如何使用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测试在选项卡布局中选择特定的选项卡位置

本文发布于:2023-11-07 04:10:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项卡   如何使用   布局   位置   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!