Android: 如何使用 Espresso 在 EditTextPreference 上执行点击操作

Android: How to perform click action on EditTextPreference using Espresso

我是 UI 测试的新手,尤其是使用 Espresso。在我的用例中,我需要检查应用程序是否接受 PreferenceScreen 片段内的 EditTextPreference 上的输入文本,但在此之前我需要执行单击以显示编辑对话框然后输入文字。

这是首选项片段具有编辑小部件的方式:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditTextPreference
        android:key="@string/pref_stream_target_key"
        android:title="@string/pref_stream_target_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_resolution_key"
        android:title="@string/pref_stream_resolution_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_bitrate_key"
        android:title="@string/pref_stream_bitrate_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_framerate_key"
        android:title="@string/pref_stream_framerate_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_peerlatency_key"
        android:title="@string/pref_stream_peerlatency_label"
        app:useSimpleSummaryProvider="true" />

</PreferenceScreen>

这就是我尝试执行点击的方式

with(launchFragmentInContainer<StreamSettingsFragment>()) {
    onFragment { fragment ->
        Espresso.onData(PreferenceMatchers.withTitle(R.string.pref_stream_target_label))
            .perform(ViewActions.click())
    }
}

我不确定是否有必要执行点击操作以检查 EditTextPreference 中更改侦听器背后的逻辑。

运行 对此进行的测试只会保留应用程序 运行 而什么都不做。有什么我想念的吗?

编辑: 让浓缩咖啡工作而不做任何事情的解决方案是将代码从 onFragment 块中取出,但是我仍然无法执行EditTextPreference 小部件的操作。

这是抛出的错误:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: is assignable from class <class android.widget.AdapterView>

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=2218, has-focus=false, 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, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION hwFlags=#10 isEmuiStyle=0 statusBarColor=#ff000000 navigationBarColor=#fffcfcfc wanim=0x1030001
  fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
|
+->ActionBarOverlayLayout{id=16908903, res-name=decor_content_parent, visibility=VISIBLE, width=1080, height=2218, has-focus=false, 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, layout-params=android.widget.FrameLayout$LayoutParams@aa563f7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1080, height=1966, has-focus=false, 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, layout-params=com.android.internal.widget.ActionBarOverlayLayout$LayoutParams@4eb0264, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=252.0, child-count=1} 
|
+--->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1966, has-focus=false, 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, layout-params=android.widget.FrameLayout$LayoutParams@2f83982, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
|
+---->FrameLayout{id=16908351, res-name=list_container, visibility=VISIBLE, width=1080, height=1966, has-focus=false, 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, layout-params=android.widget.LinearLayout$LayoutParams@1c24893, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
|
+----->RecyclerView{id=2131296596, res-name=recycler_view, visibility=VISIBLE, width=1080, height=1966, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@687e0d0, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=5} 
|
+------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@2746bce, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
|
+------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@3bedaef, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
+-------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@2d239fc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
+------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@615a885, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
+-------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=400, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@c6f76da, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream target, input-type=0, ime-target=false, has-links=false} 
|
+-------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@848370b, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
+------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@506f9e8, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
+------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@ade26a6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=241.0, child-count=3} 
|
+------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@ac138e7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
+-------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@d45cc94, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
+------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@985633d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
+-------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=539, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@360732, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream Resolution, input-type=0, ime-target=false, has-links=false} 
|
+-------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@fde7c83, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
+------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@bfd1e00, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
+------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@eb9647e, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=482.0, child-count=3} 
|
+------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@9025ddf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
+-------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@8161a2c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
+------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@76abcf5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
+-------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=420, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@8834a8a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream Bitrate, input-type=0, ime-target=false, has-links=false} 
|
+-------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@158f8fb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
+------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@be0ad18, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
+------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@4538556, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=723.0, child-count=3} 
|
+------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@b429d7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
+-------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@4bf82c4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
+------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@6fa95ad, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
+-------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=514, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@31aa0e2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream framerate, input-type=0, ime-target=false, has-links=false} 
|
+-------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@278c73, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
+------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@1940730, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
+------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@245e92e, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=964.0, child-count=3} 
|
+------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@c647ccf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
+-------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@bea665c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
+------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@525cd65, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
+-------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=761, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@6cb6a3a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream peer latency (SRT), input-type=0, ime-target=false, has-links=false} 
|
+-------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@1d616eb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
+------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@7e58c48, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
+---->TextView{id=16908292, res-name=empty, 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, layout-params=android.widget.LinearLayout$LayoutParams@52adae1, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false} 
|
+-->ActionBarContainer{id=16908719, res-name=action_bar_container, visibility=VISIBLE, width=1080, height=144, 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=false, is-selected=false, layout-params=com.android.internal.widget.ActionBarOverlayLayout$LayoutParams@e7d36c7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=108.0, child-count=2} 
|
+--->ActionBarView{id=16908718, res-name=action_bar, visibility=VISIBLE, width=1080, height=144, 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=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@6eb24f4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
|
+---->LinearLayout{id=-1, visibility=VISIBLE, width=620, height=144, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=false, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.app.ActionBar$LayoutParams@19441d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=24.0, y=0.0, child-count=2} 
|
+----->HomeView{id=-1, visibility=VISIBLE, width=120, height=144, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@8f10692, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
|
+------>ImageView{id=16909572, res-name=up, 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, layout-params=android.widget.FrameLayout$LayoutParams@58c7863, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
|
+------>ImageView{id=16908332, res-name=home, visibility=VISIBLE, width=96, height=96, 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=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@90f9c60, tag=null, root-is-layout-requested=false, has-input-connection=false, x=12.0, y=24.0} 
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=500, height=73, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@414f9de, tag=null, root-is-layout-requested=false, has-input-connection=false, x=120.0, y=35.0, child-count=2} 
|
+------>TextView{id=16908722, res-name=action_bar_title, visibility=VISIBLE, width=476, height=73, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@c437bf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Genius Live Camera, input-type=0, ime-target=false, has-links=false} 
|
+------>TextView{id=16908721, res-name=action_bar_subtitle, 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, layout-params=android.widget.LinearLayout$LayoutParams@9421e8c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false} 
|
+---->ActionMenuView{id=-1, visibility=VISIBLE, 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=false, is-selected=false, layout-params=android.view.ViewGroup$LayoutParams@2bdd9d5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1080.0, y=72.0, child-count=0} 
|
+--->ActionBarContextView{id=16908723, res-name=action_context_bar, 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, layout-params=android.widget.FrameLayout$LayoutParams@2f2d5ea, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
+-->ActionBarContainer{id=16909433, res-name=split_action_bar, 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, layout-params=com.android.internal.widget.ActionBarOverlayLayout$LayoutParams@b8e90db, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
    at dalvik.system.VMStack.getThreadStackTrace(Native Method)
    at java.lang.Thread.getStackTrace(Thread.java:1730)
    at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:12)
    at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:7)
    at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:8)
    at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:11)
    at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:4)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.apply(DataInteraction.java:1)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.apply(DataInteraction.java:2)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:7)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:13)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.displayDataMatcher(DataInteraction.java:1)
    at androidx.test.espresso.DataInteraction.makeTargetMatcher(DataInteraction.java:2)
    at androidx.test.espresso.DataInteraction.perform(DataInteraction.java:1)
    at com.domain.division.app.fragment.StreamSettingsFragmentTest.validTarget(StreamSettingsFragmentTest.kt:27)

删除 onFragment 位 - 这样您就可以在主线程上以同步方式直接与 Fragment 交互。 Espresso 旨在在测试线程上工作,并自行处理与 UI 的同步:

launchFragmentInContainer<StreamSettingsFragment>()

onData(withTitle(R.string.pref_stream_target_label)).perform(ViewActions.click())

编辑

好的,现在你得到 No views in hierarchy found matching: is assignable from class <class android.widget.AdapterView> 。这是什么意思?这意味着 onData 试图在 AdapterView(如 ListView)中找到一个对象,但没有找到。

好的,为什么不呢?查看错误报告,您将看到:

+----->RecyclerView{id=2131296596, res-name=recycler_view, visibility=VISIBLE, width=1080, height=1966, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@687e0d0, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=5} 
|
+------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@2746bce, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
|
+------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@3bedaef, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
+-------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@2d239fc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 

这是什么意思?这意味着 PreferenceFragment 为其项目使用 RecyclerView 而不是 ListView。因此,PreferenceMatchersonData 将不起作用。

好的,那你怎么办?对于 RecyclerViews,您可以使用 RecyclerViewActions

您之前的尝试可能会转化为类似以下内容:

// We know the ID from the error stacktrace you were given
onView(withId(R.id.recycler_view)) 
    .perform(actionOnItem<RecyclerView.ViewHolder>(
        // Find the preference that has "target label" text view as a subview
        hasDescendant(withText(R.string.pref_stream_target_label)),
        click() // And click it
    ))