I have a sliding tabs app, with recyclerview in every tab. I am testing the app using Android Espresso. I want to click on one of the recyclerview item. when i do this:
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(2, click()));
I get this exception:
android.support.test.espresso.AmbiguousViewMatcherException: 'with id: com.uriel.frankel:id/recyclerView' matches multiple views in the hierarchy.
any Ideas?
答案 0 :(得分:1)
嗯,我猜您在标签中为每个RecyclerView
使用相同的xml文件。因此,您的所有RecyclerView's
都具有相同的ID。因此,您无法使用您的ID来引用RecyclerView's
。
因此,要么为每个RecyclerView
提供自己的ID,要么尝试使用ViewMatcher
之类的任何withText("any specific text in your recyclers")
进行搜索。 Here您可以找到适合您情况的方法。
答案 1 :(得分:0)
我建议创建ViewHolder
匹配器以匹配RecyclerView
内的特定项目。然后,您就可以像PositionableRecyclerViewAction
一样使用scrollToHolder(...)
,例如:
public static Matcher<RecyclerView.ViewHolder> withMessageSubjectInViewHolder(String itemSubject) {
Checks.checkNotNull(itemSubject);
return new BoundedMatcher<RecyclerView.ViewHolder,
MyRecyclerViewItemAdapter.MyViewHolder>(MyRecyclerViewItemAdapter.MyViewHolder.class) {
@Override
public boolean matchesSafely(MyRecyclerViewItemAdapter.MyViewHolder holder) {
boolean isMatches = false;
if (!(holder.subject == null)) {
isMatches = ((itemSubject.equals(holder.subject.getText().toString()))
&& (holder.subject.getVisibility() == View.VISIBLE));
}
return isMatches;
}
@Override
public void describeTo(Description description) {
description.appendText("with message subject: " + itemSubject);
}
};
}
这就是我使用它的方式:
onView(withId(R.id.my_recycler_view_id)).perform(scrollToHolder(withMessageSubjectInViewHolder(unique_message_subject)));
onView(withText(unique_message_subject)).check(matches(isDisplayed())).perform(click());