使用浓缩咖啡在回收站视图中单击 child 的按钮
Performing click on a button of a child in recycler view using espresso
在 activity 中,我有一个包含 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
上使用 RecyclerViewActions 之一,然后 actionOnItemView
作为项目视图的后续操作(如果成功):
ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));
在 activity 中,我有一个包含 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
上使用 RecyclerViewActions 之一,然后 actionOnItemView
作为项目视图的后续操作(如果成功):
ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));