如何点击RecyclerView with Android Espresso中的每个项目



有关于如何在您输入的任何位置单击回收器视图中的某个项目的帖子,但是有没有办法单击所有视图,特别是考虑到我可能不知道回收器视图中会有多少视图。

我想像这个链接一样获取回收器视图中的项目数量,但它涉及从活动中获取实际的回收器视图,这有时是不可能的。

我还看到了一段代码,例如:

public static class RecyclerViewItemCountAssertion implements ViewAssertion { private final int expectedCount;
public RecyclerViewItemCountAssertion(int expectedCount) {
this.expectedCount = expectedCount;
}
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.Adapter adapter = recyclerView.getAdapter();
assertThat(adapter.getItemCount(), is(expectedCount));
}
}

但我不确定如何操纵以获得计数

所以在你的测试中很简单:

final View viewById = activityTestRule.getActivity().findViewById(R.id.your_recycler_view_id);
final RecyclerView recyclerView = (RecyclerView) viewById;
final RecyclerView.Adapter adapter = recyclerView.getAdapter();
final int itemsCount = adapter.getItemCount();

因此,您可以从0迭代到itemsCount-1

您可以使用此类在位置获取回收器视图元素(在您的情况下0 to itemsCount-1)

public class RecyclerViewMatcher {
private final int recyclerViewId;
public RecyclerViewMatcher(int recyclerViewId) {
this.recyclerViewId = recyclerViewId;
}
public Matcher<View> atPosition(final int position) {
return atPositionOnView(position, -1);
}
public Matcher<View> atPositionOnView(final int position, final int targetViewId) {
return new TypeSafeMatcher<View>() {
Resources resources = null;
View childView;
public void describeTo(Description description) {
String idDescription = Integer.toString(recyclerViewId);
if (this.resources != null) {
try {
idDescription = this.resources.getResourceName(recyclerViewId);
} catch (Resources.NotFoundException var4) {
idDescription = String.format("%s (resource name not found)", recyclerViewId);
}
}
description.appendText("with id: " + idDescription);
}
public boolean matchesSafely(View view) {
this.resources = view.getResources();
if (childView == null) {
RecyclerView recyclerView =
(RecyclerView) view.getRootView().findViewById(recyclerViewId);
if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
} else {
return false;
}
}
if (targetViewId == -1) {
return view == childView;
} else {
View targetView = childView.findViewById(targetViewId);
return view == targetView;
}
}
};
}}

并像这样使用此类:

onView(new RecyclerViewMatcher(R.id.your_recycler_view_id)
.atPositionOnView(0, R.id.your_item_body))
.perform(click());

因此,最后您可以单击回收器视图中的所有项目。我希望这会有所帮助。干杯。

我在这里大声思考,但我会尝试的几件事是:
a- 检查是否有任何方法可以将其放入 ViewHolder/RecyclerView 适配器中。这实际上取决于您何时要触发所有项目的点击,所以我猜它对您不起作用。
b-尝试使用尝试/捕获进行一段时间循环以获取项目。从软件工程最佳实践的角度来看,这听起来不是一个好主意,但如果你的目标只是让某些东西工作,它应该有效。
c-如果您无法获得回收器视图本身,是否有某种方法可以访问用于填充回收器视图本身的数组列表(或其他任何内容)?

一些问题:
1-在什么情况下,您无法获得实际的回收者视图?如果你无法得到它,那么你将如何触发它的点击?
2-您能否分享您的代码以及您需要在哪里执行此操作?

免责声明:我对安卓开发有点陌生,但我希望这有所帮助。

为了线程安全,您应该使用自定义视图操作,如下所示

@RunWith(AndroidJUnit4.class)
public class XXXXActivityTest {
int count=0;
@Test
public void xxxxxxx() throws Exception {
onView(allOf(withId(R.id.drawer_list))).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
count=((ListView)view).getAdapter().getCount();
}
});
}
}

然后你可以迭代

onView(new RecyclerViewMatcher(R.id.drawer_list)
.atPositionOnView(0, R.id.your_item_body))
.perform(click());
assertThat(.......) for all items

最新更新