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

Android Espresso test failed on physical device (PerformException)

嗨,所以我有这个应用程序,里面有这个 RecipeActivity 和一个 recyclerview。

当您单击一个列表项时,它会打开另一个 activity,在 intent 中传递一个 extra。

我只想用 Espresso 测试这个简单的功能(我从它开始)。 这是我的代码:
@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) 模拟器上 运行 它时 它通过了 (我试了两次才确定)。

当我在我的 Oneplus 3(使用 Android 7.1.1)(或者甚至在具有与 api 级别相同的另一个模拟器中尝试时Nexus 5.0),它没有。 这是错误信息:

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.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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.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.

会是什么?为什么模拟器可以执行位置 0 处的操作却不能?为什么明明存在,却说 viewholder 不存在于该位置?模拟器没有这个问题!

我唯一的想法是物理设备更快,它会在创建取景器之前进行检查......我试图添加一个
.perform(RecyclerViewActions.scrollToPosition(0))

介于 onView 和 actionOnItemAtPosition 之间,但无论如何都不起作用。

我错过了什么?先感谢您!

好的,我解决了,我需要使用 IdlingResources。