浓缩咖啡:"Unresolved reference: atPosition"



我正试图使用在我看到它在StackOverflow上的各种答案中使用的方式:

Espresso.onView(withId(R.id.rv_feeds))
.check(matches(atPosition(0, hasDescendant(withText("Test text")))));

Android Studio找到所需的所有导入,除了atPosition。我是不是漏掉了一个需要的gradle导入?

testImplementation "androidx.test.ext:junit-ktx:1.1.3"
testImplementation "androidx.test:core-ktx:1.4.0"
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation "androidx.test:core:1.4.0"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.test.espresso:espresso-contrib:3.4.0"
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestUtil 'androidx.test:orchestrator:1.4.0'

谢谢。

是的。你失去了:

androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'

似乎atPosition是由某人编写的扩展方法,不确定是否正确,但我在这个stackoverflow答案中找到了一个这样的示例代码,并且工作良好:https://stackoverflow.com/a/34795431/1241783

如果链接断开,下面是代码片段:

public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) {
checkNotNull(itemMatcher);
return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
@Override
public void describeTo(Description description) {
description.appendText("has item at position " + position + ": ");
itemMatcher.describeTo(description);
}
@Override
protected boolean matchesSafely(final RecyclerView view) {
RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position);
if (viewHolder == null) {
// has no item on such position
return false;
}
return itemMatcher.matches(viewHolder.itemView);
}
}
};
}

最新更新