如何使用 Espresso 测试 select 选项卡布局中的特定选项卡位置
How to select a specific Tab position in Tab Layout using Espresso testing
我有一个带视图寻呼机的选项卡布局。我正在使用 Espresso 来测试我的 android 应用程序。在我以前的项目中,我使用选项卡标题来执行点击 selecting 选项卡位置,如下所示。
Espresso.onView(ViewMatchers.withText("MAP"))
.perform(ViewActions.click());
现在我有 114 个标签页。所以我不能使用上述方法随机 select 这些选项卡进行测试。有什么办法可以按位置 select 选项卡。我已经检查了其他解决方案,但其中 none 帮助了我。
应该可以使用自定义 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))
作为没有 switched/used 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();
}
}
}
};
}
以上两个答案都很好。对我来说,在选择选项卡后,我还想通过匹配标题来验证它是否是正确的选项卡。因为,这个 post 是关于浓缩咖啡测试的,我将在这里分享代码片段,因为它可能对某人有帮助。
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)
}
}
}
我有一个带视图寻呼机的选项卡布局。我正在使用 Espresso 来测试我的 android 应用程序。在我以前的项目中,我使用选项卡标题来执行点击 selecting 选项卡位置,如下所示。
Espresso.onView(ViewMatchers.withText("MAP"))
.perform(ViewActions.click());
现在我有 114 个标签页。所以我不能使用上述方法随机 select 这些选项卡进行测试。有什么办法可以按位置 select 选项卡。我已经检查了其他解决方案,但其中 none 帮助了我。
应该可以使用自定义 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))
作为没有 switched/used 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();
}
}
}
};
}
以上两个答案都很好。对我来说,在选择选项卡后,我还想通过匹配标题来验证它是否是正确的选项卡。因为,这个 post 是关于浓缩咖啡测试的,我将在这里分享代码片段,因为它可能对某人有帮助。
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)
}
}
}