Issue
Hi, so I have this app that has this RecipeActivity with a recyclerview inside.When you click a list item, it opens up another activity, passing an extra in the intent.
I just wanna test that simple functionality with Espresso (i'm starting with it). This is my code:@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));
}
}
Now, the weird thing is that it passes when I run it on the Nexus 5.0 (Android 7.0) emulator (i've tried twice to be sure).
When I try it on my Oneplus 3 (with Android 7.1.1) (or even in another emulator with the same api level as the Nexus 5.0), it doesn't. This is the error message:
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.
What could it be? Why is it not able to perform the action at position 0 when the emulator can? Why does it say that viewholder doesn't exist in that position when it clearly does? The emulator doesn't have that issue!
My only idea would be that the physical device is faster, and it checks before the viewholder is created...I tried to add a.perform(RecyclerViewActions.scrollToPosition(0))
in between the onView and the actionOnItemAtPosition, but it doesn't work anyway.
What am I missing? Thank you in advance!Solution
Ok I solved it, I needed to use IdlingResources.
Answered By - ovalb
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.