【问题标题】:Espresso : How to get the exact TextView from an LinearLayout? [duplicate]Espresso:如何从 LinearLayout 中获取准确的 TextView? [复制]
【发布时间】:2017-11-13 06:51:25
【问题描述】:

我已经开始使用 Espresso 来自动化我的测试,我面临的问题是,我无法匹配我的 xml 文件中的确切文本视图。

示例 xml 如下:

xmlns:app="http://schemas.xx.com/apk/res-auto"

android:id="@+id/filter_sort_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="2dp">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:animateLayoutChanges="true"
    android:shrinkColumns="0">

    <TableRow>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/dropdown_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="5dp"
                android:clickable="true"
                android:ellipsize="end"
                android:focusable="true"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:paddingBottom="15dp"
                android:paddingEnd="5dp"
                android:paddingTop="15dp"
                android:textColor="@color/colorAccent" />

            <android.support.v7.widget.AppCompatImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="3dp"
                app:srcCompat="@drawable/ic_triangle" />
        </LinearLayout>

我正在尝试检查 id 'dropdown_title' 是否存在并且必须从中获取文本。我使用的代码:

    onView(allOf(
    withId(R.id.dropdown_title),
    withParent(withId(R.id.filter_sort_layout)))).
    check(matches(isDisplayed();

但是,它让我错误地说,

'android.support.test.espresso.NoMatchingViewException: 在层次结构中没有找到匹配的视图: (id: xx.debug:id/dropdown_title 并且有父匹配: with id: xx/filter_sort_layout)

查看层次结构:

    +>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
    |
    +->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1776, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
    |
    +-->ViewStub{id=16909227, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
    |
    +-->FrameLayout{id=-1, visibility=VISIBLE, width=1080, height=1704, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=72.0, child-count=1}
    |
    +--->FitWindowsLinearLayout{id=2131820663, res-name=action_bar_root, visibility=VISIBLE, width=1080, height=1704, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
    |'

我尝试过使用descendantOfA()hasSibling() 方法。不工作。请帮帮我。

【问题讨论】:

    标签: android android-layout automation android-espresso


    【解决方案1】:

    我认为您当前的代码失败了,因为dropdown_title 不是filter_sort_layout 的直接子代。相反,dropdown_titlefilter_sort_layout 的后代。请改用isDescendantOfA

    onView(allOf(withId(R.id.dropdown_title),
           isDescendantOfA(withId(R.id.filter_sort_layout))))
        .check(matches(isDisplayed()));
    

    编辑:

    从下面的 cmets 看来,OP 实际上在应用程序中具有多个同名视图。重复链接介绍了如何处理这种情况。

    【讨论】:

    • 感谢您的回复。我尝试了 isDescendantOfA(),但仍然面临同样的问题。
    • 所以你得到与isDescendantOfA 相同的错误?
    • 是的,我使用了完全相同的代码,它向我抛出了这个错误:android.support.test.espresso.AmbiguousViewMatcherException: '(with id: xx:id/dropdown_title and is descendant of a: with id: xx:id/filter_sort_layout)' 匹配层次结构中的多个视图。
    • 嗯...您的应用中是否有多个filter_sort_layout 布局标签?这似乎是错误消息的意思。
    • 视图在 xx.xml 文件中只定义一次,但在应用的所有过滤视图中被多次使用。那么,我们该如何匹配呢?
    猜你喜欢
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多