Vin*_*k B 4 android automated-tests android-espresso
我有一个带有View pager的选项卡布局。我正在使用Espresso 来测试我的Android应用程序。在我以前的项目中,我使用标签标题执行单击以选择标签位置,如下所示。
Espresso.onView(ViewMatchers.withText("MAP"))
.perform(ViewActions.click());
Run Code Online (Sandbox Code Playgroud)
现在我有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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
和用法:
onView(withId(R.id.tab_layout)).perform(selectTabAtPosition(99))
Run Code Online (Sandbox Code Playgroud)
作为没有切换/使用 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();
}
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
以上两个答案都很好。对我来说,在选择选项卡后,我还想通过与标题匹配来验证它是否是正确的选项卡。由于这篇文章是关于浓缩咖啡测试的,我将在这里分享代码片段,因为它可能对某人有所帮助。
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1477 次 |
| 最近记录: |