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

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

我只是想用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)上(甚至在具有与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.


会是什么呢?当仿真器可以执行时,为什么不能在位置0执行操作?为什么它说在明确的位置上不存在该视图持有者?模拟器没有这个问题!

我唯一的想法是物理设备更快,并且它会在创建Viewholder之前进行检查...我试图添加一个

.perform(RecyclerViewActions.scrollToPosition(0))


在onView和actionOnItemAtPosition之间,但仍然无法正常工作。

我想念什么?先感谢您!

最佳答案

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

关于java - 物理设备上的Android Espresso测试失败(PerformException),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46959184/

10-11 22:37
查看更多