Issue
I want to perform two clicks on different two items of recycler view in such way:
onView(withId(R.id.recycler_view))
.perform(
RecyclerViewActions.actionOnItemAtPosition<CustomAdapter.CustomViewHolder>(
1,
longClick()
)
)
onView(withId(R.id.recycler_view))
.perform(
RecyclerViewActions.actionOnItemAtPosition<CustomAdapter.CustomViewHolder>(
2,
longClick()
)
)
but I found that only second click on the item with position 2 is really performed.
Also I tried another variant:
onView(withId(R.id.recycler_view))
.perform(
RecyclerViewActions.actionOnItemAtPosition<CustomAdapter.CustomViewHolder>(
1,
longClick()
),
RecyclerViewActions.actionOnItemAtPosition<CustomAdapter.CustomViewHolder>(
2,
longClick()
)
)
but result was the same - only second click performed.
So, the question is, what I'm missing and how can I implement this case properly?
Thanks for any help!
UPDATE. It seems like initialTouchMode flag not working. I initialize my activity test rule with initialTouchMode = true:
@get:Rule
var activityTestRule = ActivityTestRule(
ListActivity::class.java,
true,
false
)
Then in each @Test method I call activityTestRule.launchActivity(Intent(Intent.ACTION_MAIN)) and after that I make clicks. I found, that only first click not works, but second and third clicks works. So, what can be the reason of such behavior?
Solution
I found the reason of problem. I have a view inside of layout in my recycle view item. This view implements WebView which load some markdown text. Loading takes some time, so my clicks was performed earlier then view was really rendered. Here is the sample of code which make test working:
onView(withId(R.id.recycler_view))
.perform(
waitForMillis(1500),//wait until markdown views loaded
actionOnItemAtPosition<CustomAdapter.CustomViewHolder>(1, longClick()),
actionOnItemAtPosition<CustomAdapter.CustomViewHolder>(2, longClick())
)
where waitForMillis() is the custom action
fun waitForMillis(millis: Long): ViewAction? {
return object : ViewAction {
override fun getConstraints(): Matcher<View> {
return isDisplayed()
}
override fun getDescription(): String {
return "Wait for $millis milliseconds."
}
override fun perform(uiController: UiController, view: View) {
uiController.loopMainThreadForAtLeast(millis)
}
}
}
This solution has disadvantage. To be sure view will be rendered before click you need set fixed time which probably will be more then rendering really need. So best solution would be custom matcher, but I don't have ready example for it now.
Answered By - Allhebra
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.