在一个 Activity 中,我有一个带有 2 个选项卡的 ViewPager
:“ALL”和“Filtered”。
两个页面都使用相同的 Fragment
来显示数据,不同之处在于“Filtered”页面按某些条件过滤数据。
我想点击“全部”页面(它可能也存在于“过滤”页面)中的一个项目,如果我这样做:
onData(transactionWithId(960L)).perform(click());
作为回应,我得到:
AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy
然后我尝试通过指定一个额外的约束来完善我的描述,我正在寻找一个可见的项目:
onData(allOf(transactionWithId(960L), isDisplayed())).perform(click());
我得到了同样的错误。
然后我想以某种方式指定,我正在“全部”选项卡中查找我的项目(不确定这是否正确):
onData(allOf(
transactionWithId(960L),
withParent(withText("ALL")))
).perform(click());
但同样的错误。
然后我试图指定我正在寻找一个当前在我面前的AdapterView
:
onData(allOf(
is(instanceOf(Transaction.class)),
transactionWithId(960L))
).inAdapterView(allOf(
isAssignableFrom(AdapterView.class),
isDisplayed())
).perform(click());
我得到了:
PerformException: Error performing 'load adapter data' on view '(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)'.
请注意,我可以使用单个 ListView
单击显示在 Activity
中的项目,我面临的挑战是当我有一个 ViewPager
包含多个选项卡,这些选项卡使用一个 Fragment
来显示数据。
非常感谢任何帮助。
最佳答案
您的方法应该可行,我创建了一个最小的工作示例 here .
问题的要点是区分两个适配器 View 。另一种方法是用 tags 明确标记它们.然后我们可以使用 inAdapterView()
和特定标签上的自定义 Matcher
来限制 DataInteration
。完整代码还是here ,我引用重点:
在适配器 View 中:
boolean isFiltered = ...
AdapterView av = ...
av.setTag(isFiltered);
在测试中:
@Test
public void testClickOnSecondItemInAllTab() {
onData(instanceOf(String.class)).inAdapterView(withTag(false)) //
.atPosition(1) //
.perform(click());
}
标签上的自定义 View 匹配器:
static Matcher<View> withTag(final Object tag) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(final Description description) {
description.appendText("has tag equals to: " + tag);
}
@Override
protected boolean matchesSafely(final View view) {
Object viewTag = view.getTag();
if (viewTag == null) {
return tag == null;
}
return viewTag.equals(tag);
}
};
}
关于android - 在 Espresso 中,指定要使用当前可见的 AdapterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28761898/