Android Espresso,RecyclerViewActions 请求权限

Posted

技术标签:

【中文标题】Android Espresso,RecyclerViewActions 请求权限【英文标题】:Android Espresso, RecyclerViewActions ask for permission 【发布时间】:2016-10-02 19:03:03 【问题描述】:

我正在尝试测试当用户单击回收站视图上的某个项目时会发生什么,并断言新活动中显示的文本正常。

@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 替换为 012(无需滚动即可看到的任何项目)测试通过。

有什么想法吗?

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

【问题讨论】:

似乎您正在敲击键盘,这是另一个应用程序。这就是为什么您会出现这样的错误 - 因为 Espresso 无法在被测应用程序之外运行。尝试关闭键盘或从设置中禁用它。 我不是 :( 你可以在这里查看 gif:https://github.com/matoelorriaga/pokemon-mvp/ 那我就没有别的办法了,在这里问你问题-groups.google.com/forum/#!forum/android-testing-support-library 【参考方案1】:

RecyclerView 的工作方式是,屏幕上不可见的项目不属于视图层次结构。如果向下滚动,适配器视图将在稍后膨胀。

还有 espresso 匹配器可用: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());

【讨论】:

developer.android.com/reference/android/support/test/espresso/… actionOnItemAtPosition docs说它首先滚动然后执行操作,而且withRecyclerView ViewMatcher不存在。 我发现了一个有趣的链接:spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews 我试过这个课程gist.github.com/baconpat/8405a88d04bd1942eb5e430d33e4faa2,但它不起作用。【参考方案2】:

好吧,我找到了解决方案:

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 应该为我们处理这个问题..

【讨论】:

以上是关于Android Espresso,RecyclerViewActions 请求权限的主要内容,如果未能解决你的问题,请参考以下文章

Espresso Android,点击 WebView 元素错误

无法从firebase数据库android,recycler视图中检索数据

Android recycler view onItemClick 识别适配器

浓缩咖啡:适配器视图的 android.support.test.espresso.AmbiguousViewMatcherException

Android Espresso单元测试

Android - Espresso:为每次测试重新创建活动