Reputation: 1040
I Have Have a list if recycler views in different positions in screen.
Like below
The position of recycler view keeps changing. Tried below code but it clicks on static position '6'
recyclerView = onView( allOf(withId(R.id.recycler_view), isDisplayed())); recyclerView.perform(actionOnItemAtPosition(6, click()));
I want to know the proper position dynamically.
Upvotes: 0
Views: 728
Reputation: 4972
Clicking the position in instrumentation tests, in my opinion, is a bad practice as it leads to non-deterministic tests. This is applicable to both RecyclerView
and AdapterView
.
So in order to not rely on a position you need an itemView
matcher for your recyclerview action.
ItemView matcher is a view matcher that matches the itemview of the ViewHolder
. In your case you need to match a LinearLayout
that holds the highlighted RelativeLayout
, which can be represented as hasDescendant(withText("Job 109"))
The end solution in your case should look like:
onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(Recyclerview actions.actionOnItem(hasDescendant(withText("Job 109")), click()));
Upvotes: 2