RosAng
RosAng

Reputation: 1040

Espresso: Clicking on Recycler view with unknown position

I Have Have a list if recycler views in different positions in screen. Like below enter image description here

I want to know the proper position dynamically.

Upvotes: 0

Views: 728

Answers (1)

Be_Negative
Be_Negative

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

Related Questions