本文介绍了onChildView 和 hasSiblings 与 Espresso的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

public void testTimeConfig(){onData(withDesc(description")).onChildView(withId(R.id.positive)).perform(click());}私有静态匹配器withDesc(字符串desc){返回 allOf(is(instanceOf(String.class)), is(desc));}

运行时出现错误:

在视图可从类中分配:类 android.widget.AdapterView"上执行加载适配器数据"时出错.

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

编辑

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

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

出现此错误:

 在层次结构中没有找到匹配的视图

xml

 <查看android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_alignParentRight="true"android:layout_alignTop=@id/timeGoalWidget"类=com.xxx"android:id="@+id/distanceGoalWidget";app:goalLabel="@string/distance_upper";app:icon="@drawable/ic_icn_summary_track";应用程序:步=0.25";app:valueFormat="decimal_two";android:gravity="center_horizo​​ntal";android:contentDescription="setAGoalDistanceConfigurator"/><查看android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_below=@id/timeGoalWidget"android:layout_alignLeft=@id/timeGoalWidget"类=com.xxx"android:id="@+id/paceGoalWidget";app:goalLabel="@string/pace_upper";app:icon="@drawable/ic_icn_summary_pace";app:valueFormat=时间"应用程序:步骤=10"android:gravity="center_horizo​​ntal";android:layout_marginBottom="60dp";android:contentDescription="setAGoalPaceConfigurator"/><查看android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_alignRight="@id/distanceGoalWidget";android:layout_alignTop=@id/paceGoalWidget"类=com.xxx"android:id="@+id/speedGoalWidget";app:goalLabel="@string/speed_upper";app:icon="@drawable/ic_icn_summary_speed";应用程序:步=0.5";app:valueFormat="decimal_two";android:gravity="center_horizo​​ntal";android:contentDescription="setAGoalSpeedConfigurator"/><查看android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_below=@id/paceGoalWidget"android:layout_alignLeft=@id/paceGoalWidget"类=com.xxx"android:id=@+id/inclineGoalWidget"app:goalLabel="@string/incline_percent_upper";app:icon="@drawable/ic_icn_summary_elevation";应用程序:步=0.5";app:valueFormat="decimal_two";android:gravity="center_horizo​​ntal";android:contentDescription="setAGoalInclineConfigurator"/><查看android:layout_width="wrap_content";android:layout_height="wrap_content";android:layout_alignRight=@id/speedGoalWidget"android:layout_alignTop=@id/inclineGoalWidget"类=com.xxx"android:id="@+id/caloriesGoalWidget";app:goalLabel="@string/calories_upper";app:icon="@drawable/ic_icn_summary_calories";应用程序:步骤=10"app:valueFormat="decimal_zero";android:gravity="center_horizo​​ntal";android:contentDescription="setAGoalCaloriesConfigurator"/></RelativeLayout>

使用了 6 次的视图 xml.

<图像按钮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=@dr​​awable/button_goal_widget"机器人:重力=中心"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"/><线性布局android:id="@+id/goalWidgetValueContainer";android:layout_width="wrap_content";android:layout_height="wrap_content";机器人:方向=垂直"android:layout_toLeftOf="@id/increaseGoalButton"android:layout_centerVertical="true"android:gravity="center_vertical|right";android:layout_marginRight=@dimen/view_goal_setting_value_margin_right"><文本视图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"/><文本视图android:id=@+id/goalValueLabelTextView"android:layout_height="wrap_content";android:layout_width="wrap_content";android:layout_gravity=正确"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() - 示例

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

或没有自定义匹配器的示例(兄弟视图有文本):

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

或(兄弟视图有内容描述):

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

I am trying to access a button from a specific view. The same view is displayed 6 times. This is the code I am using.

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

When I run, I get an error:

Is this the best way to access a child view? If so, how?

EDIT

This is the code I am trying to use now.

and

With this Error:

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

The view xml that is used 6 times.

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


It is the ImageView with id increaseGoalButton that I am trying to click.

解决方案

Based on your last comment you should use onView() instead of onData().I think you'll be able to click the button using hasSibling() - example

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

or examples without your custom matcher (sibling view has text):

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

or (sibling view has content description):

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

EDITED:

OK, I'd try these two variants:

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

or

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

这篇关于onChildView 和 hasSiblings 与 Espresso的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:13