Issue
I am writing espresso tests for my application. For one test I have to click a button on a RecyclerView-Item. Said item is bigger than the screen, so when I scroll to the item the required button is still not displayed (which is required so I can click it).
I tried to scroll to the view with
onView(withId(R.id.recycler_view)).perform(scrollTo<MyViewHolder>(hasDescendant(withId(R.id.target_button))))
but this just scrolled to the top of the item that contains the button. Since the button is at the very bottom of that item and the item is larger than the screen, when I try to click the button I get
Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
I also tried
onView(withId(R.id.recycler_view)).perform(actionOnItemAtPosition<MyViewHolder>(0, EspressoUtils.clickChildViewWithId(R.id.target_button)))
where EspressoUtils.clickChildViewWithId(id: Int)
is what is described in the accepted answer of this question. This also yielded the same error. How can I scroll to the specific child within the RecyclerView-Item?
Solution
The following solution works for me when clicking a view inside a RecyclerView item that is twice as big as my screen. Note: the view I'm clicking is actually off the screen but since it's in the view hierarchy, it can be clicked.
Create a custom ViewAction:
private fun clickChildViewWithId(id: Int) = object : ViewAction {
override fun getConstraints() = null
override fun getDescription() = "click child view with id $id"
override fun perform(uiController: UiController, view: View) {
val v = view.findViewById<View>(id)
v.performClick()
}
}
Usage:
onView(withId(R.id.recycler_view))
.perform(actionOnItemAtPosition<MyViewHolder>(0, clickChildViewWithId(R.id.target_button)))
Answered By - A Droid
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.