Search code examples
androidandroid-espressoandroid-adapterview

Espresso : How to click on an item at position 2 in AdapterView


I am new to Espresso and Mobile Testing, and I am facing a challenge. I have to search for a contact in App and click on contact displayed at second position.

I checked many posts which were similar but however none of those worked for my example. Code to Search : onView(withId(R.id.textSearch)).perform(typeText("pa"));

Code to Select : onView(withText("Parth Vyas")).perform(click());

But here if I want to select any element which is displayed at position 2, how can I do that?


Solution

  • If you know the position after search, then you can try something like:

    onData(anything()).inAdapterView(YOUR_ADAPTER_VIEW_MATCHER).atPosition(2).perform(click());