代码之家  ›  专栏  ›  技术社区  ›  ovalb

Android Espresso测试在物理设备上失败(PerformException)

  •  1
  • ovalb  · 技术社区  · 7 年前
    嗨,我有一个应用程序,里面有一个RecycleView,具有这种互动性。

    当您单击一个列表项时,它会打开另一个活动,并在意图中传递额外的内容。

    我只是想用浓缩咖啡测试一下这个简单的功能(我从它开始)。 这是我的代码:
    @RunWith(AndroidJUnit4.class)
        public class ShowIngredientsTest {
        @Rule
        public IntentsTestRule<RecipeActivity> testRule =
            new IntentsTestRule<>(RecipeActivity.class);
    
        @Test
        public void checkIfIntentWorks() {
            onView(withId(R.id.recipes_recyclerview))
                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
    
            intended(hasExtraWithKey(RECIPE_PARCEL));
        }
    }
    

    它通过了 当我在 Nexus 5.0(Android 7.0)emulator (我已经试过两次了)。

    Oneplus 3(Android 7.1.1) (甚至在另一个与Nexus 5.0具有相同api级别的仿真器中),它没有。 这是错误消息:

    android.support.test.espresso.PerformException: Error performing 'android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction@3d98130' on view 'with id: com.onval.bakingapp:id/recipes_recyclerview'.
        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.onval.bakingapp.ShowIngredientsTest.checkIfIngredientsAreShows(ShowIngredientsTest.java:34)
        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:2110)
        Caused by: java.lang.IllegalStateException: No view holder at position: 0
        at android.support.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction.perform(RecyclerViewActions.java:290)
        at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6321)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
        Tests ran to completion.
    

    .perform(RecyclerViewActions.scrollToPosition(0))
    

    在onView和ActionOnItemPosition之间,但它无论如何都不起作用。

    1 回复  |  直到 5 年前
        1
  •  1
  •   ovalb    7 年前

    好的,我解决了,我需要使用空闲资源。