android / android/testing-samples
RecyclerViewActions.scrollTo is not working if we use android binding
- Ngôn ngữ chính
- Java
- Star
- 9.3k
- Fork
- 3.6k
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
I tried to use this in espresso but it does not work and it does not scroll at all:
`onView(withId(R.id.my_test_list)).perform(RecyclerViewActions.scrollTo(hasDescendant(withText(testText))));`
the error is:
```
android.support.test.espresso.PerformException: Error performing 'scroll RecyclerView to: holder with view: has descendant: with text: is "test text"' on view 'with id: com.x.x.x/my_test_list'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184)
at android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115)
at android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87)
at com.x.x.x.MyTest.myMethod(MyTest.java:147)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)
Caused by: java.lang.RuntimeException: Found 0 items matching holder with view: has descendant: with text: is "test text", but position -1 was requested.
at android.support.test.espresso.contrib.RecyclerViewActions$ScrollToViewAction.perform(RecyclerViewActions.java:357)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Tests ran to completion.
```
so I wrote this and this one works for me:
`scrollToFind(activityRule.getActivity(),onView(withText(testText)),R.id.test_list);`
```
public static int getCountFromRecyclerView(@IdRes int RecyclerViewId) {
final int[] COUNT = {0};
Matcher matcher = new TypeSafeMatcher() {
@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));
int result = COUNT[0];
COUNT[0] = 0;
return result;
}
public static void scrollToFind(final Activity activity, final ViewInteraction viewInteraction, final int recyclerViewId){
final int result = TestUtils.getCountFromRecyclerView(recyclerViewId) -1;
viewInteraction.withFailureHandler(new FailureHandler() {
@Override
public void handle(Throwable error, Matcher viewMatcher) {
RecyclerView recyclerView = (RecyclerView) activity.findViewById(recyclerViewId);
int last = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if(last == result) {
fail(error.getMessage());
}
onView(ViewMatchers.withId(recyclerViewId)).perform(ViewActions.swipeUp());
scrollToFind(activity,viewInteraction, recyclerViewId);
}
}).perform(click());
}
```
Hướng dẫn đóng góp
Hướng nghiên cứu
Bắt đầu với RecyclerViewActions$ScrollToViewAction tại vị trí trong stack trace và so sánh nó với cách sử dụng android binding được báo cáo trong MyTest.java:147. Tái hiện lỗi bằng matcher và RecyclerView được chỉ ra, sau đó so sánh với workaround scrollToFind tùy chỉnh. Hoàn tất nghĩa là xác định và kiểm thử một bản sửa đáng tin cậy cho thao tác cuộn bị lỗi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- android, java
- Lĩnh vực
- mobile-dev, testing
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 30/100