当前位置: 首页 > 面试题库 >

使用Android Espresso测试时如何单击操作栏上的项目?

支洋
2023-03-14
问题内容

我正在使用Android
Espresso测试我的活动。我有多个操作栏项,其中大多数隐藏在溢出中。我正在尝试运行此测试,但是错误表明层次结构中没有视图(与不可见视图不同):

@MediumTest
public void testClickInsertItem() {
  Espresso.onView(ViewMatchers.withId(R.id.action_insert)).perform(ViewActions.click());
}

错误:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: blogr.vpm.fr.blogr:id/action_insert
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.ListView{5283cfb0 VFED.VC. ........ 2,2-762,973 #7f07000b app:id/allitems}

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=768, height=1184, 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=1}
|
+->ActionBarOverlayLayout{id=16909075, res-name=action_bar_overlay_layout, visibility=VISIBLE, width=768, height=1184, 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}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=768, height=1038, 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=146.0, child-count=1}
|
+--->RelativeLayout{id=-1, visibility=VISIBLE, width=768, height=1038, 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=1}
|
+---->ViewPager{id=2131165186, res-name=pager, visibility=VISIBLE, width=764, height=1034, has-focus=true, 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, root-is-layout-requested=false, has-input-connection=false, x=2.0, y=2.0, child-count=3}
|
+----->PagerTitleStrip{id=2131165187, res-name=pager_title_strip, visibility=VISIBLE, width=764, height=59, 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, root-is-layout-requested=false, has-input-connection=false, x=763.0, y=0.0, child-count=3}
|
+------>TextView{id=-1, visibility=VISIBLE, width=125, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=8.0, text=Free text, input-type=0, ime-target=false, has-links=false}
|
(...)
|
+-->ActionBarContainer{id=16909076, res-name=action_bar_container, visibility=VISIBLE, width=768, height=96, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=50.0, child-count=2}
|
+--->ActionBarView{id=16909077, res-name=action_bar, visibility=VISIBLE, width=768, height=96, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+---->LinearLayout{id=-1, desc=Hello, Navigate up, visibility=VISIBLE, width=124, height=96, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+----->HomeView{id=-1, visibility=VISIBLE, width=97, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+------>ImageView{id=16908890, res-name=up, visibility=VISIBLE, width=32, height=32, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=32.0}
|
+------>ImageView{id=16908332, res-name=home, visibility=VISIBLE, width=64, height=64, 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, root-is-layout-requested=false, has-input-connection=false, x=25.0, y=16.0}
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=27, height=49, 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, root-is-layout-requested=false, has-input-connection=false, x=97.0, y=23.0, child-count=2}
|
+------>TextView{id=16908901, res-name=action_bar_title, visibility=VISIBLE, width=11, height=49, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Hello, input-type=0, ime-target=false, has-links=false}
|
+------>TextView{id=16908902, 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, 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=644, height=96, 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, root-is-layout-requested=false, has-input-connection=false, x=124.0, y=0.0, child-count=3}
|
+----->EditText{id=2131165210, res-name=postTitle, visibility=VISIBLE, width=420, height=96, 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, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x1 imeOptions=0x8000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0 hintText=New Post label=null packageName=null fieldId=0 fieldName=null extras=null ], x=0.0, y=0.0, text=Hello, hint=New Post, input-type=1, ime-target=false, has-links=false}
|
+----->ActionMenuItemView{id=2131165222, res-name=action_publish, desc=Publish, visibility=VISIBLE, width=112, height=96, 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, root-is-layout-requested=false, has-input-connection=false, x=420.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false}
|
+----->OverflowMenuButton{id=-1, desc=More options, visibility=VISIBLE, width=112, height=96, 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, root-is-layout-requested=false, has-input-connection=false, x=532.0, y=0.0}
|
+--->ActionBarContextView{id=16909078, 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, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}
|
+-->ActionBarContainer{id=16909079, 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, 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)

那么,我应该如何访问ID为的按钮项R.id.action_insert

我注意到了OverflowMenuButton。我可以单击它,但是然后我得到另一个看起来不像动作项目列表的视图层次结构。我是否应该在视图层次结构中进行挖掘,直到找到我的操作项,或者是否有更好的指定方法来单击溢出中的操作项?我在Android文档中的操作栏上找不到任何文档。


问题答案:

Espresso具有openActionBarOverflowOrOptionsMenu(Context context)打开操作栏菜单的方法。您必须在进行测试之前将其放在菜单项上。

import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;

@Test
public void testClickInsertItem() {
    openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext());
    onView(withId(R.id.action_insert)).perform(click());
}


 类似资料:
  • 我需要用jasmine对一些DOM操作函数进行单元测试(目前我在浏览器和Karma中运行测试) 我想知道最好的方法是什么? 例如,我可以模拟和存根窗口,记录对象和spyOn的几个函数。但这看起来并不是一个简单的解决方案,所以我要问这个问题! 或者有没有更好的方法(也许不用茉莉花)来做到这一点? 非常感谢

  • 如何从操作栏禁用“选项”菜单项? 我做了我自己的自定义栏,用于显示*. png标志,但我不想显示选项菜单的三个点按钮。 我试图找到一些解决办法,但没有任何效果。

  • 我有一个图像按钮在我的应用程序中的每一个活动。当图像按钮被按下时,我需要转到主屏幕。我正在使用代码 但是,当我在单击图像按钮后从主屏幕导航时,它应该会从它调用的地方转到相同的页面。 例如,我有Activity1、Activity2、Activity3、Activity4等…Activity1是首页,我会选择国家,然后去Activity2,然后去Activity3,等等,但如果我从Activity4

  • 我的任务是单独使用TestNG测试应用程序,该应用程序在tomcat服务器上运行。该应用程序是用JAVA编写的。我有应用程序的APIendpoint,但是如何使用POST和GET方法测试应用程序? 我想测试,例如:<代码>http://localhost:8080/sportsbetting-web/登录,我应该发布用户凭据。如何测试登录是否有效?

  • 有没有一种方法来显示在操作栏中的HoneyComb菜单项之间的分隔符。 有些帖子说,只有当菜单项具有android时,分隔符才会显示: show AsAction="with Text"。 我想只显示图标,而不是文本。 我通过实现一个操作栏兼容性,成功地展示了Pre-HoneyComb的分隔器。 我不想使用ActionBarSherlock在这篇文章中给出的Android actionbar sh

  • 问题内容: 我正在尝试编写代码以检查单击SVG对象的功能-例如此URL上的美国状态 这可行,但是有更好的方法吗?有没有物理移动鼠标的东西? 问题答案: 在SVG中,显示的所有内容都是由坐标定义的块。在您的情况下,每个状态也是一个块,因此有一个XPath,您可以在Selenium代码中使用它。 使用浏览器开发工具分析页面代码,并找到与您要单击的状态相对应的块。 这是与加利福尼亚相对应的元素。 找到它