【问题标题】:Espresso: clicking one of many textviews with same Id浓缩咖啡:单击具有相同 ID 的多个文本视图之一
【发布时间】:2022-04-04 10:19:26
【问题描述】:

如何从具有相同 ID 的不同文本视图中单击单个文本视图。这些是线性布局的子级。它们的文本像 A、B、C、D 一样动态地提供给 id 为 txt_option_index 的 textview。

<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>

我试过了:

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

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

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

但没有运气, 错误

android.support.test.espresso.AmbiguousViewMatcherException

【问题讨论】:

  • 尝试使用onData()匹配器而不是onView。方法如下:stackoverflow.com/questions/29378552/…
  • 我试过onData(),但没用。我也试过onPosition(),但没用,因为它不是具有位置值的gridview。在这种情况下,只有一个文本视图被多次使用来显示问题的选项。这些选项是动态创建的。

标签: android automated-tests android-espresso


【解决方案1】:

您可以在 Activity 或 Fragment 中的 textView 上使用标签,例如:

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

在测试中你可以像这样测试:

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

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多