Bor*_* S. 46 android hamcrest android-espresso android-recyclerview
使用Espresso和Hamcrest,
如何计算recyclerView中可用的项目编号?
例子:我想检查特定的RecyclerView中是否显示了5个项目(必要时滚动).
nen*_*ick 77
这里有一个示例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));
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用这个断言
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5));
Run Code Online (Sandbox Code Playgroud)
我已经开始编写一个库,应该使用espresso和uiautomator使测试更加简单.这包括RecyclerView操作和断言的工具.https://github.com/nenick/espresso-macchiato例如,使用方法assertItemCountIs(int)查看EspRecyclerView
Bra*_*bin 28
在@Stephane的答案中添加一些语法糖.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
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)));
// ...
Run Code Online (Sandbox Code Playgroud)
Sté*_*ane 18
完成nenick答案并提供更灵活的解决方案,以测试项目cout是否更大,更少...
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);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
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));
// ...
Run Code Online (Sandbox Code Playgroud)
t0m*_*t0m 10
基于@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];
}
Run Code Online (Sandbox Code Playgroud)
小智 8
我使用以下方法获取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;
}
Run Code Online (Sandbox Code Playgroud)
用法-
int itemsCount = getCountFromRecyclerView(R.id.RecyclerViewId);
Run Code Online (Sandbox Code Playgroud)
然后执行断言以检查itemsCount是否符合预期
经验证的答案有效,但我们可以在没有适配器意识的情况下用一行来解决这个问题:
onView(withId(R.id.your_recycler_view_id)).check(matches(hasChildCount(2)))
Run Code Online (Sandbox Code Playgroud)
替换your_recycler_view_id为您的 ID 和2要断言的编号。
您可以创建自定义BoundedMatcher:
object RecyclerViewMatchers {
@JvmStatic
fun hasItemCount(itemCount: Int): Matcher<View> {
return object : BoundedMatcher<View, RecyclerView>(
RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("has $itemCount items")
}
override fun matchesSafely(view: RecyclerView): Boolean {
return view.adapter.itemCount == itemCount
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
onView(withId(R.id.recycler_view)).check(matches((hasItemCount(5))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16992 次 |
| 最近记录: |