[英]Performing click on a button of a child in recycler view using espresso
在一个活动中,我有一个自定义回收器视图,其中包含 2 个文本视图和 2 个按钮,如下所示。
回收站图标按钮的 id 是 deleteButton。 我想在浓缩咖啡测试中访问这个按钮,以便我可以模拟点击。
我尝试过但失败的两种方法如下所示:
onView(withId(R.id.basket))
.perform(actionOnItemAtPosition(4, click() ));
onView(withText("Cherry"))
.perform(
RecyclerViewActions.actionOnItem(
hasDescendant(withId(R.id.deleteButton)),
ViewActions.click()
)
);
感谢您提前提供任何帮助。
在RecyclerView
项目视图中单击视图的一种方法是创建自定义视图操作:
public static ViewAction actionOnItemView(Matcher<View> matcher, ViewAction action) {
return new ViewAction() {
@Override public String getDescription() {
return String.format("performing ViewAction: %s on item matching: %s", action.getDescription(), StringDescription.asString(matcher));
}
@Override public Matcher<View> getConstraints() {
return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());
}
@Override public void perform(UiController uiController, View view) {
List<View> results = new ArrayList<>();
for (View v : TreeIterables.breadthFirstViewTraversal(view)) {
if (matcher.matches(v)) results.add(v);
}
if (results.isEmpty()) {
throw new RuntimeException(String.format("No view found %s", StringDescription.asString(matcher)));
} else if (results.size() > 1) {
throw new RuntimeException(String.format("Ambiguous views found %s", StringDescription.asString(matcher)));
}
action.perform(uiController, results.get(0));
}
};
}
然后在RecyclerView上使用RecyclerView
之一,如果成功,则将actionOnItemView
作为项目视图的后续操作:
ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.