advanced-android-testing: Espresso Test 2: Adding a note FAIL

I just run code with the doc in Section of Espresso Test 2: Adding a note in Nexus 5 (API 23). Here is the logcat:

android.support.test.espresso.PerformException: Error performing 'scroll RecyclerView to: holder with view: has descendant: with text: is "UI testing for Android"' on view 'with id: com.example.android.testing.notes.mock:id/notes_list'.
at android.support.test.espresso.PerformException$Builder.build(PerformException.java:83)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:70)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
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.example.android.testing.notes.notes.NotesScreenTest.addNoteToNotesList(NotesScreenTest.java:123)
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:257)
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:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: java.lang.RuntimeException: Found 0 items matching holder with view: has descendant: with text: is "UI testing for Android", but position -1 was requested.
at android.support.test.espresso.contrib.RecyclerViewActions$ScrollToViewAction.perform(RecyclerViewActions.java:357)
at android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:144)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
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:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Thanks for fix it.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I think it’s the funniest story ever. I’ve started to playing with withItemText Matcher. After adding logging I’ve found the reason: the soft keyboard software. I’ve had a Swipe setup on Android, it autocorrects words. It was replacing some test words after inserted by Espresso. Please, save other people’ time and add some notice/warning to instrumentation.

And btw as Espresso promises ‘no flakiness’ should I file a bug/feature on Espresso?

I solve this problem as below:

  • First, change device language to English;
  • Second, add these code
@Before
public void registerIdlingResource() {
    // let espresso know to synchronize with background tasks
    IdlingRegistry.getInstance().register(EspressoIdlingResource.getIdlingResource());
}

@After
public void unregisterIdlingResource() {
    IdlingRegistry.getInstance().unregister(EspressoIdlingResource.getIdlingResource());
}

On Device: I have the same problem with prodDebug + had animations off. But by adding IdlingRegistry.getInstance().register(EspressoIdlingResource.getIdlingResource()); everything was solved animations on or off.

On Emulator: Everything worked fine with animation on or off. Does it mean that IdlingRegistry.getInstance().register(EspressoIdlingResource.getIdlingResource()); should be added to the code lab for some devices ?

The keyboard must be set to English. With the German keyboard becomes testing changed to Testung.

With reference to this. Similar to the method in NoteDetailScreenTest.

@Before
public void registerIdlingResource() {
    // let espresso know to synchronize with background tasks
    IdlingRegistry.getInstance().register(EspressoIdlingResource.getIdlingResource());
}

@After
public void unregisterIdlingResource() {
    IdlingRegistry.getInstance().unregister(EspressoIdlingResource.getIdlingResource());
}

I’ve faced the same problem. But the main problem was

// Scroll notes list to added note, by finding its description onView(withId(R.id.notes_list)).perform(scrollTo(hasDescendant(withText(newNoteDescription)))); // Verify note is displayed on onView(withText(newNoteDescription)).check(matches(isDisplayed()));

These two lines were getting called even before the list population. So I added another line onView(withId(R.id.notes_list)).check(matches(isDisplayed())); Just to make sure that the view was showing then my tests passed.