agoda-com / Kakao

This repo is no longer supported. Please visit a https://github.com/KakaoCup/Kakao
Apache License 2.0
1.11k stars 102 forks source link

WithIndex is not working properly #90

Closed michalkierasinski closed 6 years ago

michalkierasinski commented 6 years ago

Steps to reproduce:

  1. Create a layout with views with the same id
  2. Write a simple test using withIndex method
  3. Execute test

Observed Results:

Error message - Espresso could not found a matching view.

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (0th view with: (with id: com.agoda.sample:id/input_layout))

Expected Results:

It should work.

Relevant Code:

Sample project with error

Unlimity commented 6 years ago

Thank you for the report! We will investigate it in the coming days.

Unlimity commented 6 years ago

I found the issue in your sample. The problem is that withIndex matcher is one-time use only. After it matches item, it is no longer usable. You can make it dynamic though. Modify your InputScreen class in attached sample:

class InputScreen : Screen<InputScreen>() {
    fun inputLayout(lambda: KEditText.() -> Unit) = KEditText { withIndex(0, { withId(R.id.input_layout) }) }.invoke(lambda)
}

Hope this will help you. This code will create a new instance of KEditText with withIndex matcher in it on every invocation.

michalkierasinski commented 6 years ago

Your solution does not work. I get the same error: No views in hierarchy found matching: (0th view with: (with id: com.agoda.sample:id/input_layout)).

Every invocation executes the same withIndex matcher.

Unlimity commented 6 years ago

Here's working test:

@RunWith(AndroidJUnit4::class)
class InputTest {
    @Rule
    @JvmField
    val rule = ActivityTestRule(InputActivity::class.java)

    val screen = InputScreen()

    @Test
    fun workingTest() {
        screen {
            inputLayout {
                replaceText("EXAMPLE")
                idle()
            }
        }
    }

    @Test
    fun noWorkingTest() {
        screen {
            inputLayout {
                replaceText("EXAMPLE")
            }

            idle()

            inputLayout {
                hasAnyText()
            }
        }
    }
}

You see, every action and assertion that you perform on a matcher, Espresso is starting over that matching process, because your layout can change since the last invocation. As I said earlier, withIndex is a single-use matcher. So for every action that you perform with that view, you need to provide fresh matcher. You still can execute a batch of assertions or actions using corresponding assert() and act() functions, where you pass lambda which returns espresso's ViewAction and ViewAssertion. Hope that helps. If you have any idea how can we improve withIndex matcher to be reusable, your contributions are always welcome!

michalkierasinski commented 6 years ago

Ok, now it is working. Thanks for the help. I will try to find a solution for withIndex.

Unlimity commented 6 years ago

You're welcome. Closing this issue then.