首页 > 解决方案 > Android Espresso 测试匹配器 withId 在 ActionBarContextView 中返回 null

问题描述

当我的 ActionBarContextView 显示时,我正在尝试添加浓缩咖啡测试。

在此处输入图像描述

问题是当我尝试通过 id 查找视图时,视图为空。

onView(allOf(withId(R.id.action_mode_bar),withText(actionBarTitle))).check(matches(isDisplayed()))

我也试试

onView(isAssignableFrom(ActionBarContextView::class.java)).check(matches(isDisplayed()))

并且文本ID失败。但是在 ViewHistroy 中,视图被列在那里。

    View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, 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 wanim=0x10302fe
fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS
pfl=FORCE_DRAW_STATUS_BAR_BACKGROUND}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+->FrameLayout{id=-1, visibility=VISIBLE, width=1080, height=1794, 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@70dc8fc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1080, height=1731, 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@ae31dda, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=63.0, child-count=1}
|
+--->ConstraintLayout{id=-1, visibility=VISIBLE, width=1080, height=1731, 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@a653da6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+---->MaterialToolbar{id=2131231037, res-name=toolbar, visibility=VISIBLE, width=1080, height=147, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@a4803e7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+----->AppCompatTextView{id=-1, visibility=VISIBLE, width=222, height=71, 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=androidx.appcompat.widget.Toolbar$LayoutParams@d843b94, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=38.0, text=Task List, input-type=0, ime-target=false, has-links=false}
|
+---->CoordinatorLayout{id=-1, visibility=VISIBLE, width=1080, height=1584, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@3eca63d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=147.0, child-count=3}
|
+----->RecyclerView{id=2131230953, res-name=rv_tasks, visibility=VISIBLE, width=1080, height=1584, 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=androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams@886f783, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=4}
|
+------>ConstraintLayout{id=2131230817, res-name=container, visibility=VISIBLE, width=1080, height=210, 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@5d3692c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=4}
|
+------->AppCompatImageView{id=2131230878, res-name=iv_task_check, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@9175ff5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+------->MaterialTextView{id=2131231048, res-name=tv_task_name, visibility=VISIBLE, width=961, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@aaeb18a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=68.0, text=Task1, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231047, res-name=tv_task_date, visibility=VISIBLE, width=961, height=43, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@135d3fb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=125.0, text=9/20/20, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231046, res-name=tv_est_pommodoros, visibility=VISIBLE, width=35, height=85, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@9a3ec18, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1003.0, y=63.0, text=1, input-type=0, ime-target=false, has-links=false}
|
+------>ConstraintLayout{id=2131230817, res-name=container, visibility=VISIBLE, width=1080, height=210, 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@73898ad, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=210.0, child-count=4}
|
+------->AppCompatImageView{id=2131230878, res-name=iv_task_check, visibility=GONE, width=63, height=63, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@46be7e2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=74.0}
|
+------->MaterialTextView{id=2131231048, res-name=tv_task_name, visibility=VISIBLE, width=961, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@d64c773, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=68.0, text=Task2, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231047, res-name=tv_task_date, visibility=VISIBLE, width=961, height=43, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@ea02630, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=125.0, text=9/20/20, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231046, res-name=tv_est_pommodoros, visibility=VISIBLE, width=35, height=85, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@ef312a9, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1003.0, y=63.0, text=1, input-type=0, ime-target=false, has-links=false}
|
+------>ConstraintLayout{id=2131230817, res-name=container, visibility=VISIBLE, width=1080, height=210, 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@a1e913a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=420.0, child-count=4}
|
+------->AppCompatImageView{id=2131230878, res-name=iv_task_check, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@c1fb1eb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+------->MaterialTextView{id=2131231048, res-name=tv_task_name, visibility=VISIBLE, width=961, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@8768b48, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=68.0, text=Task3, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231047, res-name=tv_task_date, visibility=VISIBLE, width=961, height=43, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@a996de1, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=125.0, text=9/20/20, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231046, res-name=tv_est_pommodoros, visibility=VISIBLE, width=35, height=85, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@fd48706, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1003.0, y=63.0, text=1, input-type=0, ime-target=false, has-links=false}
|
+------>ConstraintLayout{id=2131230817, res-name=container, visibility=VISIBLE, width=1080, height=210, 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@60e7363, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=630.0, child-count=4}
|
+------->AppCompatImageView{id=2131230878, res-name=iv_task_check, visibility=VISIBLE, width=63, height=63, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@ce17b60, tag=null, root-is-layout-requested=false, has-input-connection=false, x=42.0, y=74.0}
|
+------->MaterialTextView{id=2131231048, res-name=tv_task_name, visibility=VISIBLE, width=885, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@bd8f819, tag=null, root-is-layout-requested=false, has-input-connection=false, x=118.0, y=68.0, text=Task3, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231047, res-name=tv_task_date, visibility=VISIBLE, width=885, height=43, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@34f70de, tag=null, root-is-layout-requested=false, has-input-connection=false, x=118.0, y=125.0, text=9/20/20, input-type=0, ime-target=false, has-links=false}
|
+------->MaterialTextView{id=2131231046, res-name=tv_est_pommodoros, visibility=VISIBLE, width=35, height=85, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@6eae2bf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1003.0, y=63.0, text=1, input-type=0, ime-target=false, has-links=false}
|
+----->ContentLoadingProgressBar{id=2131230887, res-name=loader, 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=androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams@1d2ed8c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+----->FloatingActionButton{id=2131230850, res-name=fb_add_task, visibility=GONE, width=147, height=147, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=true, is-selected=false, layout-params=androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams@177fcd5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=891.0, y=1395.0}
|
+---->StatusView{id=2131230995, res-name=status_view, visibility=GONE, width=1080, height=403, 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=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@4f4ebdb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=738.0, child-count=1}
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=403, 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@ccc6ab7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+------>MaterialTextView{id=2131231034, res-name=title, visibility=VISIBLE, width=665, height=71, 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@c176224, tag=null, root-is-layout-requested=false, has-input-connection=false, x=207.0, y=42.0, text=You don't have any task yet, input-type=0, ime-target=false, has-links=false}
|
+------>MaterialTextView{id=2131230999, res-name=subtitle, visibility=VISIBLE, width=410, height=43, 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@1b3f18d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=335.0, y=166.0, text=Do you want to create one?, input-type=0, ime-target=false, has-links=false}
|
+------>MaterialButton{id=2131230797, res-name=button, visibility=VISIBLE, width=231, height=126, 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=android.widget.LinearLayout$LayoutParams@d44ff42, tag=null, root-is-layout-requested=false, has-input-connection=false, x=424.0, y=235.0, text=Create, input-type=0, ime-target=false, has-links=false, is-checked=false}
|
+-->ActionBarContextView{id=16908691, res-name=action_mode_bar, visibility=VISIBLE, width=1080, height=126, 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@a27cd8e, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=63.0, child-count=3}
|
+--->LinearLayout{id=-1, visibility=VISIBLE, width=270, height=71, 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$MarginLayoutParams@fb2d1bc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=189.0, y=27.0, child-count=2}
|
+---->TextView{id=16908686, res-name=action_bar_title, visibility=VISIBLE, width=249, height=71, 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@e72c545, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=2 Selected, input-type=0, ime-target=false, has-links=false}
|
+---->TextView{id=16908685, 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@757349a, 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}
|
+--->LinearLayout{id=16908693, res-name=action_mode_close_button, desc=Done, visibility=VISIBLE, width=147, height=126, 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=android.view.ViewGroup$MarginLayoutParams@9b94da8, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+---->ImageView{id=-1, visibility=VISIBLE, width=63, height=63, 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@17f70c1, tag=null, root-is-layout-requested=false, has-input-connection=false, x=36.0, y=31.0}
|
+--->ActionMenuView{id=-1, visibility=VISIBLE, width=126, height=126, 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.view.ViewGroup$LayoutParams@7176fa7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=954.0, y=0.0, child-count=1}
|
+---->ActionMenuItemView{id=2131230831, res-name=delete, desc=Delete, visibility=VISIBLE, width=126, height=126, 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=android.widget.ActionMenuView$LayoutParams@b299c54, 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}
|
+->View{id=16908336, res-name=navigationBarBackground, visibility=VISIBLE, width=1080, height=126, 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@b5157fd, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=1794.0}
|
+->View{id=16908335, res-name=statusBarBackground, visibility=VISIBLE, width=1080, height=63, 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@51fbcf2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|

如果我尝试仅通过文本查找视图,它可以工作。但我需要通过 ID 找到它以检查视图何时可见以及何时消失

标签: androidtestingandroid-espresso

解决方案


你可能误解allOfanyOf. action_mode_bar是 a ActionBarContextView,这就是为什么allOf当你使用它时失败的原因withText,它只适用于TextView. 所有匹配器必须在一个匹配器中allOf匹配,例如action_bar_titleand withText

onView(allOf(withId(R.id.action_bar_title), withText(actionBarTitle))).check(matches(isDisplayed()))

推荐阅读