google-developer-training / android-basics-kotlin-affirmations-app-solution

Apache License 2.0
58 stars 49 forks source link

Android Basics issue: #107

Closed TecBrat closed 10 months ago

TecBrat commented 2 years ago

URL of codelab https://developer.android.com/codelabs/android-basics-kotlin-affirmations-test-lists-and-adapters?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-kotlin-unit-2-pathway-3%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fandroid-basics-kotlin-affirmations-test-lists-and-adapters

In which task and step of the codelab can this issue be found? Step 7 "Test the recycler view"

Describe the problem Instead of scrolling to a hard coded point, we are instructed to try scrolling to a specific text with this code:

    @Test
    fun scroll_to_item(){
        onView(withId(R.id.recycler_view)).perform(
            RecyclerViewActions
                .scrollTo<RecyclerView.ViewHolder>(
                    withText(R.string.affirmation10)
                )
        )

        onView(withText(R.string.affirmation10))
            .check(matches(isDisplayed())
            )
    }

The view does not scroll and the test fails.

Steps to reproduce?

  1. Follow this codelab
  2. See error.

Versions What version of Android Studio are you using? Android Studio Arctic Fox | 2020.3.1 Patch 3 Build #AI-203.7717.56.2031.7784292, built on September 30, 2021 Runtime version: 11.0.10+0-b96-7249189 amd64 VM: OpenJDK 64-Bit Server VM by Oracle Corporation Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 1280M Cores: 4 Registry: external.system.auto.import.disabled=true, ide.images.show.chessboard=true Non-Bundled Plugins: com.thoughtworks.gauge, org.jetbrains.kotlin, com.gmail.blueboxware.libgdxplugin, org.intellij.plugins.markdown

Additional information androidx.test.espresso.PerformException: Error performing 'scroll RecyclerView to: holder with view: an instance of android.widget.TextView and view.getText() equals string from resource id: <2131689500>' on view 'Animations or transitions are enabled on the target device. For more info check: https://developer.android.com/training/testing/espresso/setup#set-up-environment

view.getId() is <2131231058/com.example.affirmations:id/recycler_view>'. at androidx.test.espresso.PerformException$Builder.build(PerformException.java:1) at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:9) at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:7) at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:8) at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:11) at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:4) at com.example.affirmations.AffirmationsListTests.scroll_to_item(AffirmationsListTests.kt:21) ... 29 trimmed Caused by: java.lang.RuntimeException: Found 0 items matching holder with view: an instance of android.widget.TextView and view.getText() equals string from resource id: <2131689500>, but position -1 was requested. at androidx.test.espresso.contrib.RecyclerViewActions$ScrollToViewAction.perform(RecyclerViewActions.java:361) at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:2) at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:22) at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:1) at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:2) at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:1) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

StewMcc commented 2 years ago

Test will fail if following all previous examples as it requires hasDescendant to be able to detect it is a textview within a descendant.

@Test
fun scroll_to_item() {
    onView(withId(R.id.recycler_view)).perform(
        RecyclerViewActions
            .scrollTo<RecyclerView.ViewHolder>(
                hasDescendant(withText(R.string.affirmation10))
            )
    )

    onView(withText(R.string.affirmation10))
        .check(matches(isDisplayed())
        )
}

^^ notice the addition of hasDescendant in scrollTo.

Just hit this today 😄 myself.

gouravkhunger commented 2 years ago

Damn, it happened with me too! Spent some minutes and found the solution with hasDescendant().

I was going file an issue here but glad that it is already reported :)

TecBrat commented 2 years ago

StewMcc, Thank you for answering! This kind of thing has a tendency to derail me, since I don't have the background to dig out the solution. I should have just moved on, but for some psychological reason I just couldn't. Now I can. It's great to have a community of support!

inusedname commented 2 years ago

I'm looking for the error, and there it is! Hope Android Team guys fix it :)

dogtim commented 2 years ago

Yes, this should be fixed in the document. Anyway, love u guys to help me figure out this problem from @StewMcc 's reply

soloHub commented 1 year ago

Thankfully, the solution is here. (Thanks to @StewMcc ) Android team Please help fix the document

RequestPrivacy commented 1 year ago

Still there, needs fix although the focus lies on the compose side I guess...