2016-10-02 4 views
0

ユーザーがリサイクラービューでアイテムをクリックしたときに何が起こるかをテストし、新しいアクティビティに表示されるテキストがOKであることを確認しようとしています。私は、このエラーメッセージが出ていAndroid Espresso、RecyclerViewActions許可を依頼する

@RunWith(AndroidJUnit4.class) 
public class MainActivityTest { 

    @Rule 
    public ActivityTestRule<MainActivity> mainActivityTestRule = 
      new ActivityTestRule<>(MainActivity.class); 

    @Test 
    public void test() { 
     onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition(24, click())); 
     onView(withId(R.id.pokemon_id)).check(matches(withText("# 25"))); 
    } 

} 

android.support.test.espresso.PerformException: Error performing 'single click - At Coordinates: 269, 933 and precision: 16, 16' on view 'with id: com.melorriaga.pokemon:id/recycler_view'. 
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.melorriaga.pokemon.MainActivityTest.test(MainActivityTest.java:67) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
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 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
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:1944) 
Caused by: android.support.test.espresso.PerformException: Error performing 'Send down motion event' on view 'unknown'. 
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83) 
at android.support.test.espresso.action.MotionEvents.sendDown(MotionEvents.java:104) 
at android.support.test.espresso.action.Tap.sendSingleTap(Tap.java:133) 
at android.support.test.espresso.action.Tap.access$100(Tap.java:35) 
at android.support.test.espresso.action.Tap$1.sendTap(Tap.java:40) 
at android.support.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:98) 
at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:303) 
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5343) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission 
at android.support.test.espresso.base.InputManagerEventInjectionStrategy.innerInjectMotionEvent(InputManagerEventInjectionStrategy.java:162) 
at android.support.test.espresso.base.InputManagerEventInjectionStrategy.innerInjectMotionEvent(InputManagerEventInjectionStrategy.java:160) 
at android.support.test.espresso.base.InputManagerEventInjectionStrategy.injectMotionEvent(InputManagerEventInjectionStrategy.java:132) 
at android.support.test.espresso.base.EventInjector.injectMotionEvent(EventInjector.java:96) 
at android.support.test.espresso.base.UiControllerImpl$3.call(UiControllerImpl.java:223) 
at android.support.test.espresso.base.UiControllerImpl$3.call(UiControllerImpl.java:220) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission 
at android.os.Parcel.readException(Parcel.java:1546) 
at android.os.Parcel.readException(Parcel.java:1499) 
at android.hardware.input.IInputManager$Stub$Proxy.injectInputEvent(IInputManager.java:417) 
at android.hardware.input.InputManager.injectInputEvent(InputManager.java:681) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.test.espresso.base.InputManagerEventInjectionStrategy.innerInjectMotionEvent(InputManagerEventInjectionStrategy.java:146) 
... 11 more 

基本的には:Injecting to another application requires INJECT_EVENTS permission

奇妙なことは、私はせずに任意のアイテムを表示02412を(置き換えるならば、ということですテストパスをスクロールする必要があります)。

アイデア?画面上に表示されていないアイテムは、ビュー階層の一部ではないような方法で動作RecyclerView

https://github.com/matoelorriaga/pokemon-mvp/blob/master/app/src/androidTest/java/com/melorriaga/pokemon/MainActivityTest.java

+0

.. actionOnItemAtPositionが私たちのためにそれを扱うべきだと思いますが、あなたが他のアプリケーションでキーボード、をタップしているようです。そのため、エスプレッソはテスト対象のアプリの外では動作できないため、このようなエラーが発生します。キーボードを閉じたり、設定から無効にしてください。 – denys

+0

私はそうではありません:(ここではgifを確認できます: 'https:// github.com/matoelorriaga/pokemon-mvp /' –

+0

他に何も考えていません、https://groups.google .com/forum /#!forum/android-testing-support-library – denys

答えて

0

はまあ、私は解決策を見つけた:

onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition(30)); 
onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition(24, click())); 
onView(withId(R.id.pokemon_id)).check(matches(withText("# 25"))); 

基本的には、まずあなたがアイテムを作るいくつかの位置までスクロールする必要があります(私が24にスクロールすると、まだ画面に表示されないので、テストは失敗します)。

とにかく、私は

0

。後でスクロールすると、アダプタビューは後で膨張します。

そして、そのために利用可能なエスプレッソマッチャありますhttps://google.github.io/android-testing-support-library/docs/espresso/lists/index.html#recyclerviews

public static void tapRecyclerViewItem(int recyclerViewId, int position) { 
    onView(withId(recyclerViewId)).perform(scrollToPosition(position)); 
    onView(withRecyclerView(recyclerViewId).atPosition(position)).perform(click()); 
} 
+0

https://developer.android.com/reference/android/support/test/espresso/contrib/RecyclerViewActions.html 'actionOnItemAtPosition' docsは最初にそれを言うスクロールしてアクションを実行すると、 'withRecyclerView' ViewMatcherは存在しません。 –

+0

興味深いリンクが見つかりました:https://spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews/ –

+0

私は試しましたこのクラスはhttps://gist.github.com/baconpat/8405a88d04bd1942eb5e430d33e4faa2であり、動作しません。 –

関連する問題