android - onChildView 和 hasSiblings 与 Espresso

标签 android android-testing hamcrest android-espresso

我正在尝试从特定 View 访问按钮。同一 View 显示 6 次。这是我正在使用的代码。

public void testTimeConfig(){
    onData(withDesc("description")).onChildView(withId(R.id.positive)).perform(click());
}

private static Matcher<Object> withDesc(String desc) {
    return allOf(is(instanceOf(String.class)), is(desc));
}

当我运行时,出现错误:

Error performing 'load adapter data' on view 'is assignable from class: class android.widget.AdapterView'.

这是访问 subview 的最佳方式吗?如果是,怎么办?

编辑

这是我现在尝试使用的代码。

   onView(allOf((withContentDescription("description")), hasSibling(withContentDescription("SettingsLayout")), hasSibling(withId(R.id.positive)))).perform(click());

   onView(allOf((withContentDescription("description")), hasSibling(withId(R.id.positive)))).perform(click());

出现此错误:

   No views in hierarchy found matching

xml

    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/setAGoalTitle"
        android:layout_alignParentLeft="true"
        class="com.xxx"
        android:layout_marginTop="30dp"
        android:id="@+id/timeGoalWidget"
        app:goalLabel="@string/time_min_upper"
        app:icon="@drawable/ic_icn_summary_time"
        app:step="300"
        app:valueFormat="time"
        android:gravity="center_horizontal"
        android:layout_marginBottom="60dp"
        android:contentDescription="setAGoalTimeConfigurator"/>

    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/timeGoalWidget"
        class="com.xxx"
        android:id="@+id/distanceGoalWidget"
        app:goalLabel="@string/distance_upper"
        app:icon="@drawable/ic_icn_summary_track"
        app:step="0.25"
        app:valueFormat="decimal_two"
        android:gravity="center_horizontal"
        android:contentDescription="setAGoalDistanceConfigurator"/>

    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/timeGoalWidget"
        android:layout_alignLeft="@id/timeGoalWidget"
        class="com.xxx"
        android:id="@+id/paceGoalWidget"
        app:goalLabel="@string/pace_upper"
        app:icon="@drawable/ic_icn_summary_pace"
        app:valueFormat="time"
        app:step="10"
        android:gravity="center_horizontal"
        android:layout_marginBottom="60dp"
        android:contentDescription="setAGoalPaceConfigurator"/>


    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/distanceGoalWidget"
        android:layout_alignTop="@id/paceGoalWidget"
        class="com.xxx"
        android:id="@+id/speedGoalWidget"
        app:goalLabel="@string/speed_upper"
        app:icon="@drawable/ic_icn_summary_speed"
        app:step="0.5"
        app:valueFormat="decimal_two"
        android:gravity="center_horizontal"
        android:contentDescription="setAGoalSpeedConfigurator"/>


    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/paceGoalWidget"
        android:layout_alignLeft="@id/paceGoalWidget"
        class="com.xxx"
        android:id="@+id/inclineGoalWidget"
        app:goalLabel="@string/incline_percent_upper"
        app:icon="@drawable/ic_icn_summary_elevation"
        app:step="0.5"
        app:valueFormat="decimal_two"
        android:gravity="center_horizontal"
        android:contentDescription="setAGoalInclineConfigurator"/>


    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/speedGoalWidget"
        android:layout_alignTop="@id/inclineGoalWidget"
        class="com.xxx"
        android:id="@+id/caloriesGoalWidget"
        app:goalLabel="@string/calories_upper"
        app:icon="@drawable/ic_icn_summary_calories"
        app:step="10"
        app:valueFormat="decimal_zero"
        android:gravity="center_horizontal"
        android:contentDescription="setAGoalCaloriesConfigurator"/>


</RelativeLayout>

使用了6次的 View xml。

<ImageButton
    android:id="@+id/increaseGoalButton"
    android:layout_height="@dimen/view_goal_setting_incrementor_button_height_width"
    android:layout_width="@dimen/view_goal_setting_incrementor_button_height_width"
    android:background="@drawable/button_goal_widget"
    android:gravity="center"
    android:src="@drawable/ic_plus"
    android:textSize="@dimen/view_goal_setting_incrementor_button_text_size"
    android:textColor="@color/goal_configuration_widget_button_text_color"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="1dp"
    android:contentDescription="@string/increase"/>

<ImageButton
    android:id="@+id/decreaseGoalButton"
    android:layout_height="@dimen/view_goal_setting_incrementor_button_height_width"
    android:layout_width="@dimen/view_goal_setting_incrementor_button_height_width"
    android:background="@drawable/button_goal_widget"
    android:gravity="center"
    android:src="@drawable/ic_minus"
    android:textSize="@dimen/view_goal_setting_incrementor_button_text_size"
    android:layout_alignParentRight="true"
    android:textColor="@color/goal_configuration_widget_button_text_color"
    android:layout_below="@id/increaseGoalButton"
    android:contentDescription="@string/decrease"/>

<LinearLayout
    android:id="@+id/goalWidgetValueContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_toLeftOf="@id/increaseGoalButton"
    android:layout_centerVertical="true"
    android:gravity="center_vertical|right"
    android:layout_marginRight="@dimen/view_goal_setting_value_margin_right" >

    <TextView
        android:id="@+id/goalValueTextView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textColor="@color/goal_configuration_widget_value_text_color"
        android:textSize="@dimen/view_goal_setting_value_text_size"
        android:lineSpacingExtra="-10sp"
        app:typeface="proxima_bold"
        android:text="999:00"/>

    <TextView
        android:id="@+id/goalValueLabelTextView"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="right"
        android:textSize="@dimen/view_goal_setting_value_label_text_size"
        android:textColor="@color/goal_configuration_widget_value_text_color"
        android:includeFontPadding="false"
        android:lineSpacingExtra="10sp"
        android:text="@string/incline_percent_upper"/>

</LinearLayout>

我尝试点击的是 ID 为 increaseGoalButton 的 ImageView。

最佳答案

根据您最后的评论,您应该使用 onView() 而不是 onData()。 我认为您可以使用 hasSibling() 单击按钮 - example

onView(allOf(withId(R.id.positive), hasSibling(withDesc(someString))))
    .perform(click());

或没有自定义匹配器的示例(同级 View 有文本):

onView(allOf(withId(R.id.positive), hasSibling(withText(someString))))
    .perform(click());

或者(同级 View 有内容描述):

onView(allOf(withId(R.id.positive), hasSibling(withContentDescription(someString))))
    .perform(click());

已编辑:

好的,我会尝试这两种变体:

onView(allOf(withId(R.id.increaseGoalButton), isDescendantOfA(withId(R.id.timeGoalWidget))))
    .perform(click());

onView(allOf(withId(R.id.increaseGoalButton), withParent(withId(R.id.timeGoalWidget))))
    .perform(click());

关于android - onChildView 和 hasSiblings 与 Espresso,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20848620/

相关文章:

android - 运行没有权限的flutter项目出错

Android APK 与测试 APK?

java - Hamcrest describeMismatchSafely 总是在我的实现中打印 Object.toString()

java - 使用自定义对象验证数组

android - react native -Redux : Multiple instances of same state in app

java - Android - 按钮上的文字

android-testing - 在 InstrumentationTestCase 运行之间重置应用程序状态

java - 使用 Hamcrest 检查某些元素是否在集合中

Android - 使位图适合屏幕

java - 将 Room DB 与 Rx 结合使用时出现 CursorWindowAllocation 异常