使用Espresso和Hamcrest,如何計算使用Espresso的RecyclerView物品
如何計算recyclerView中可用的物料編號?
例如:我想檢查5個項目是否顯示在特定的RecyclerView中(如有必要滾動)。
使用Espresso和Hamcrest,如何計算使用Espresso的RecyclerView物品
如何計算recyclerView中可用的物料編號?
例如:我想檢查5個項目是否顯示在特定的RecyclerView中(如有必要滾動)。
下面的例子ViewAssertion檢查RecyclerView項目計數
public 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));
}
}
然後用這個說法
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5));
我已經開始寫一個庫,它應該使測試更加簡單與咖啡和uiautomator 。這包括RecyclerView操作和斷言的工具。 https://github.com/nenick/espresso-macchiato例如見EspRecyclerView與方法assertItemCountIs(INT)
要完成nenick答案,並提供多一點靈活的解決方案也測試,如果項目COUT是GREATERTHAN,每種不超過...
public class RecyclerViewItemCountAssertion implements ViewAssertion {
private final Matcher<Integer> matcher;
public RecyclerViewItemCountAssertion(int expectedCount) {
this.matcher = is(expectedCount);
}
public RecyclerViewItemCountAssertion(Matcher<Integer> matcher) {
this.matcher = matcher;
}
@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(), matcher);
}
}
用法:
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5));
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(greaterThan(5));
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(lessThan(5));
// ...
給@Stephane's answer添加一點糖語法。
public class RecyclerViewItemCountAssertion implements ViewAssertion {
private final Matcher<Integer> matcher;
public static RecyclerViewItemCountAssertion withItemCount(int expectedCount) {
return withItemCount(is(expectedCount));
}
public static RecyclerViewItemCountAssertion withItemCount(Matcher<Integer> matcher) {
return new RecyclerViewItemCountAssertion(matcher);
}
private RecyclerViewItemCountAssertion(Matcher<Integer> matcher) {
this.matcher = matcher;
}
@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(), matcher);
}
}
用法:
import static your.package.RecyclerViewItemCountAssertion.withItemCount;
onView(withId(R.id.recyclerView)).check(withItemCount(5));
onView(withId(R.id.recyclerView)).check(withItemCount(greaterThan(5));
onView(withId(R.id.recyclerView)).check(withItemCount(lessThan(5));
// ...
很好。我建議把它變成espresso,因爲真的很奇怪,這樣的核心功能不可用。 – Samuel
我用下面的方法來獲取RecyclerView的計數
public static int getCountFromRecyclerView(@IdRes int RecyclerViewId) {
int COUNT = 0;
Matcher matcher = new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View item) {
COUNT = ((RecyclerView) item).getAdapter().getItemCount();
return true;
}
@Override
public void describeTo(Description description) {
}
};
onView(allOf(withId(RecyclerViewId),isDisplayed())).check(matches(matcher));
int result = COUNT;
COUNT = 0;
return result;
}
用法 -
int itemsCount = getCountFromRecyclerView(R.id.RecyclerViewId);
然後執行斷言檢查itemsCount與預期的一樣
基於@Sivakumar Kamichetty答案:
COUNT = 0;
COUNT
變量轉移到一個元素數組。result
是不必要的。不是很好,但工程:
public static int getCountFromRecyclerView(@IdRes int RecyclerViewId) {
final int[] COUNT = {0};
Matcher matcher = new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View item) {
COUNT[0] = ((RecyclerView) item).getAdapter().getItemCount();
return true;
}
@Override
public void describeTo(Description description) {}
};
onView(allOf(withId(RecyclerViewId),isDisplayed())).check(matches(matcher));
return COUNT[0];
}
這個測試總是通過。我認爲它沒有正常工作。 –
adapter.getItemCount()將會變爲null ..請讓我知道原因。 –
@AdamHurwitz許多人已經確認這項工作,但請解釋你的情況,它總是通過。 – nenick