diff --git a/kakao/src/main/kotlin/com/agoda/kakao/common/builders/ViewBuilder.kt b/kakao/src/main/kotlin/com/agoda/kakao/common/builders/ViewBuilder.kt index 8c3cb4d8..52bc7c59 100644 --- a/kakao/src/main/kotlin/com/agoda/kakao/common/builders/ViewBuilder.kt +++ b/kakao/src/main/kotlin/com/agoda/kakao/common/builders/ViewBuilder.kt @@ -37,38 +37,11 @@ class ViewBuilder { private val viewMatchers = arrayListOf>() /** - * Matches only view at given [index], if there are multiple views that matches - * - * IMPORTANT: this matcher is single-use only, since it does not reset it's - * index counter due to specific espresso's matching process. Thus only one action - * and/or assertion can be performed on such a [KView][com.agoda.kakao.common.views.KView]. - * - * If you need to match view with index multiple times, each time you should match - * with new instance of [withIndex] - * - * Take a look at the example: - * ``` - * class InputScreen : Screen() { - * fun inputLayout(lambda: KEditText.() -> Unit) = KEditText { withIndex(0, { withId(R.id.input_layout) }) }.invoke(lambda) - * } - * - * @Test - * fun test() { - * screen { - * inputLayout { - * replaceText("EXAMPLE") - * } - * - * inputLayout { - * hasAnyText() - * } - * } - * } - * ``` - * - * @param index Index of the view to match - * @param function [ViewBuilder] that will result in matcher - */ + * Matches only view at given [index], if there are multiple views that matches + * + * @param index Index of the view to match + * @param function [ViewBuilder] that will result in matcher + */ fun withIndex(index: Int, function: ViewBuilder.() -> Unit) { viewMatchers.add(IndexMatcher(ViewBuilder().apply(function).getViewMatcher(), index)) } diff --git a/kakao/src/main/kotlin/com/agoda/kakao/common/matchers/IndexMatcher.kt b/kakao/src/main/kotlin/com/agoda/kakao/common/matchers/IndexMatcher.kt index 07c472f8..73d8a270 100644 --- a/kakao/src/main/kotlin/com/agoda/kakao/common/matchers/IndexMatcher.kt +++ b/kakao/src/main/kotlin/com/agoda/kakao/common/matchers/IndexMatcher.kt @@ -15,12 +15,20 @@ import org.hamcrest.TypeSafeMatcher */ class IndexMatcher(private val matcher: Matcher, private val index: Int) : TypeSafeMatcher() { private var currentIndex = 0 + private val seen = mutableSetOf() override fun describeTo(desc: Description) { desc.appendText("${index}th view with: ") .appendDescriptionOf(matcher) } - public override fun matchesSafely(view: View): Boolean = - matcher.matches(view) && currentIndex++ == index + public override fun matchesSafely(view: View): Boolean { + if (seen.contains(view)) { + currentIndex = 0 + seen.clear() + } + + seen.add(view) + return matcher.matches(view) && currentIndex++ == index + } }