在我的Android应用程序中,我有工具栏,需要在浓缩咖啡中执行动作bard按钮。我已经尝试了以下操作,但它不起作用
onView(withId(R.id.pageToolbar)).perform(click());
需要执行其后按钮单击操作。
ContentDescription
对我不起作用,所以我必须使用:
onView(allOf(
instanceOf(AppCompatImageButton::class.java), withParent(withId(R.id.toolbar))
))
.perform(click())
因为我没有办法在层次结构中单次识别它:
+-------->AppCompatImageButton{id=-1, visibility=VISIBLE, 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=false, is-selected=false, layout-params=androidx.appcompat.widget.Toolbar$LayoutParams@acd01cf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=21.0, y=0.0}
如果您的应用程序用英语运行,请使用以下方式:
onView(withContentDescription("Navigate up")).perform(click());
要以任何语言运行,请使用以下方式:
onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());
请注意,R.string.abc_action_bar_up_description
来自AppCompat支持库。
还要注意,"箭头"按钮没有其他唯一的id
,因为浓缩咖啡将其视为:
+------> AppCompatImageButton {id=-1, desc=Navigate up, visibility=VISIBLE,
width=84, height=68, 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=false, is-selected=false,
layout-params = android.support.v7.widget.Toolbar$LayoutParams@1af06e3d,
tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}