0

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

  • I want to click on specific recycler view based on the variable text 'Job#' Inside it.
  • But cannot perform the same as I do not know the exact position I should click.
  • 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.

RosAng
  • 1,010
  • 2
  • 18
  • 42

1 Answers1

2

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()));

Be_Negative
  • 4,892
  • 1
  • 30
  • 33
  • Got Error : Wrong 1st argument type. Found: 'org.hamcrest.Matcher', required: 'int' – RosAng Sep 22 '16 at 17:31
  • Right, I somehow messed up the copy and paste part- it's supposed to be RecyclerViewAction.actionOnItem – Be_Negative Sep 22 '16 at 22:37
  • To be exact: onView(allOf(withId(R.id.recycler_view),isDisplayed())).perform(RecyclerViewActions.actionOnItem(hasDescendant(withText("Job# 109")), click())); – RosAng Sep 23 '16 at 09:03