2016-10-02 3 views
0

Я пытаюсь проверить, что происходит, когда пользователь нажимает на элемент в представлении recycler и утверждает, что текст, отображаемый в новом действии, в порядке.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

Странная вещь состоит в том, если я заменю 24 с 0, 1, 2 (любой предмет без видимой необходимо прокрутить) пробный проход.

Любые идеи?

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

+0

Кажется, что вы нажав на клавиатуре, которая является другим приложением. Вот почему у вас такая ошибка - поскольку Espresso не может работать за пределами тестируемого приложения. Попробуйте закрыть клавиатуру или отключить ее из настроек. – 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, он по-прежнему не отображается на экране, поэтому сбой теста).

Во всяком случае, я думаю, что actionOnItemAtPosition должен справиться с этим для нас ..

0

RecyclerView работает таким образом, что элементы, которые не видны на экране не являются частью иерархии представлений. Взгляд адаптера будет завышен позже, если вы прокрутите вниз.

И есть эспрессо matchers доступны для этого: 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' говорят, что сначала прокрутить и затем выполнить действие, также, 'withRecyclerView' ViewMatcher не существует. –

+0

Я нашел интересную ссылку: https://spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews/ –

+0

Я пробовал с этот класс https://gist.github.com/baconpat/8405a88d04bd1942eb5e430d33e4faa2, и он не работает. –

Смежные вопросы