android / android/testing-samples
RecyclerViewActions.scrollTo is not working if we use android binding
- 主要言語
- Java
- スター
- 9.3k
- フォーク
- 3.6k
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
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());
}
```
コントリビューションガイド
調査の方向性
スタックトレースの位置にある RecyclerViewActions$ScrollToViewAction から始め、MyTest.java:147 で報告されている android binding の使用方法と比較します。示されている matcher と RecyclerView を使って失敗を再現し、その後、カスタムの scrollToFind workaround と比較します。失敗したスクロール操作に対する信頼できる修正を特定してテストできれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- android, java
- 領域
- mobile-dev, testing
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 30/100