1

How to click a single textview out of different textviews, with same Id. These are the child of linear layout.They have text which was given dynamically like A, B, C, D to the textview having id txt_option_index.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:id="@+id/option_rl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/border">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/topMarginU2"
android:layout_centerVertical="true"
android:id="@+id/option_sign_bg_rl">
<TextView
android:id="@+id/txt_option_index"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/circle_shape_white_answer"
android:gravity="center"
android:text="A"
android:textColor="@color/black"
android:textSize="10dp" />
</RelativeLayout>
<TextView
android:id="@+id/optiontext"
android:layout_width="match_parent"
android:layout_marginTop="@dimen/topMarginU2"
android:layout_marginBottom="@dimen/topMarginU2"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/option_sign_bg_rl"
android:text="option one found"
android:layout_marginLeft="@dimen/topMarginU2"
android:clickable="true"
android:singleLine="false"
style="@style/txt_14_light_hex_grey_Text" />
</RelativeLayout>

I have tried:

ViewInteraction relativeLayout = onView(allOf(withId(R.id.txt_option_index),
withParent(withId(R.id.option_sign_bg_rl)),withText("B")));
relativeLayout.perform(scrollTo(), click());

and

ViewInteraction rr = onView(allOf(withId(R.id.txt_option_index),withText("A")));    rr.perform(click());

and

onView(allOf(withId(R.id.txt_option_index),hasSibling(withText("A")))).perform(click());

but no luck, Error

android.support.test.espresso.AmbiguousViewMatcherException
think_better
  • 343
  • 1
  • 2
  • 11
  • try to use `onData()` matcher instead of `onView`. Here's how: http://stackoverflow.com/questions/29378552/in-espresso-how-to-choose-one-the-view-who-have-same-id-to-avoid-ambiguousviewm – piotrek1543 Aug 10 '16 at 09:22
  • I have tried on `onData()` but it is of no use.And I have also tried `onPosition()` but not useful as it is not a gridview with position value. In this case there is only one textview which is used multiple times to show options of a question. These option are created dynamically. – think_better Aug 12 '16 at 07:45

1 Answers1

1

You can use tag on textView in activity or fragment for Example:

binding.optiontext.tag = "Something you want to set unique"

and In test you can test like :

onView(allOf(withId(R.id.optiontext),withText("A"),withTagvalue(equalTo("your tag")))

I hope this helps.

AndroidSam27
  • 238
  • 2
  • 11