gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:32 28 4
gpt4 key购买 nike

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

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

关于android - 如何使用 Espresso 测试在选项卡布局中选择特定的选项卡位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49626315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com