Closed michalkierasinski closed 6 years ago
Thank you for the report! We will investigate it in the coming days.
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.
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.
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!
Ok, now it is working. Thanks for the help.
I will try to find a solution for withIndex
.
You're welcome. Closing this issue then.
Steps to reproduce:
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