英文:
Performing click on a button of a child in recycler view using espresso
问题
在一个活动中,我有一个包含2个文本视图和2个按钮的自定义回收视图,如下所示。
垃圾桶图标按钮的ID是deleteButton。我想在Espresso测试中访问此按钮,以便我可以模拟点击。
我尝试过但失败了的两种方法如下:
onView(withId(R.id.basket))
.perform(actionOnItemAtPosition(4, click()));
onView(withText("Cherry"))
.perform(
RecyclerViewActions.actionOnItem(
hasDescendant(withId(R.id.deleteButton)),
ViewActions.click()
)
);
提前感谢您的任何帮助。
英文:
Within an activity, I have a custom recycler view that contains 2 textviews and 2 buttons as shown below.
The id for the recycling bin icon button is deleteButton. I would like to access this button within espresso testing so that I can mock a click.
Two methods that I have attempted and failed are shown below:
onView(withId(R.id.basket))
.perform(actionOnItemAtPosition(4, click() ));
onView(withText("Cherry"))
.perform(
RecyclerViewActions.actionOnItem(
hasDescendant(withId(R.id.deleteButton)),
ViewActions.click()
)
);
Thank you for any help in advance.
答案1
得分: 2
创建自定义视图操作是在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));
英文:
One way to click on a view inside a RecyclerView
item view is by creating a custom view action:
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));
}
};
}
Then use one of the RecyclerViewActions on your RecyclerView
, then actionOnItemView
as subsequent action on the item view if successful:
ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论