【问题标题】:How do I test the home button on the Action Bar with Espresso?如何使用 Espresso 测试操作栏上的主页按钮?
【发布时间】:2014-12-17 14:36:20
【问题描述】:

我已启用主页按钮以返回上一个视图。简单地说,这样做:

getActionBar().setDisplayHomeAsUpEnabled(true);

我正在使用com.android.support:appcompat-v7:21.0.2 的最新版本。但是,当我使用下面的代码时,它不会抛出异常。

Espresso.onView(ViewMatchers.withId(android.R.id.home)).perform(ViewActions.click()); Espresso.onView(ViewMatchers.withId(R.id.home)).perform(ViewActions.click());

例外:

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: is <2131296261> ...

【问题讨论】:

  • 嗯,很有趣。我使用相同的代码对主页按钮执行操作:onView(withId(android.R.id.home)).perform(click());不幸的是,我们仍在使用 appcompat-v7:20.0.+。也许这与新的 android 工具栏有关?如果您将使用 Android Monitor 检查视图,您会检索什么 ID?
  • 显然,新的 Android 工具栏没有 id,而不是我们使用“向上导航”:(。

标签: android android-espresso


【解决方案1】:

当您使用资源中的描述时,可以单击按钮。

onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());

适用于 appcompat-v7:23.1.1

【讨论】:

  • 请记住,R.string.abc_action_bar_up_description 是私有的,可以随时更改。但就目前而言,这是我能找到的最佳选择。
  • 我找到了没有解决方法的解决方案。这里: val upButton = onView(allOf(instanceOf(ImageButton::class.java), withParent(withId(toolBar))))
【解决方案2】:

我做了以下解决方法:

private String getString(int resId){
    return getInstrumentation().getTargetContext().getString(resId);
}

public void testUI() {   
    onView(withContentDescription(getString(R.string.navigation_drawer_open))).perform(click());
    onView(withContentDescription(getString(R.string.navigation_drawer_close))).perform(click());
}

基本上我使用内容描述属性而不是视图ID。

【讨论】:

    【解决方案3】:

    我自己回答。根据click home icon with espresso 上的帖子,在ActionBar 中为Home 按钮指定Id 是不可能的,至少支持库的版本为7,所以我们应该使用“向上导航”。但为什么呢?

    这就是 Espresso 错误跟踪的原因:

    --------> ActionMenuView
              {
                id=-1, visibility=VISIBLE, width=144, height=168, 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=936.0, y=0.0, child-count=1
              }
    |
    --------> ActionMenuItemView
              {
                id=2131296554, res-name=general, desc=, visibility=VISIBLE, 
                width=144, height=144, 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=12.0, text=, input-type=0, ime-target=false
              }
    |
    --------> ImageButton
              {
                id=-1, desc=Navigate up, visibility=VISIBLE, width=168, height=168,
                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
              }
    |
    

    【讨论】:

      【解决方案4】:

      我不知道使用 appCompat 的操作栏会有这么大的不同。正如你们在 cmets 中所写的那样,“向上导航”确实有效。对于那些像我一样不知道如何使用 espresso 的人来说,方法如下:

      import static android.support.test.espresso.Espresso.onView;
      import static android.support.test.espresso.action.ViewActions.click;
      import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
      

      onView(withContentDescription("Navigate up")).perform(click());

      AppCompat v7 已弃用

      在我的项目中,我同时使用 Robotium 和 Espresso。对于主页按钮,我使用 Robotiums Solo#clickOnActionBarHomeButton。超过 1 行,例如 click home icon with espresso 但您不必指定标题。 (在某些特殊情况下可能很有用......)。无论如何,我根据 SDK 版本决定使用哪一个:

      public static void actionBarBack(Instrumentation instrumentation, final Solo solo, String actionBarText) {
          if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
              onView(allOf(withText(actionBarText), isDisplayed())).perform(click());
          }
          else {
              instrumentation.runOnMainSync(new Runnable() {
                  @Override
                  public void run() {
                      solo.clickOnActionBarHomeButton();
                  }
              });
          }
      }
      

      用法如下:

       Compatibility.actionBarBack(getInstrumentation(), solo, R.string.any);
      

      它必须包含在 runOnMainSync 中,因为 Robotium 测试框架有点懒惰,如果你有断言,例如对于操作栏标题,在声明之后它仍然不会回来。但只能尝试使用solo.clickOnActionBarHomeButton();。它可能对你有用。

      【讨论】:

        猜你喜欢
        • 2021-02-04
        • 2015-07-02
        • 1970-01-01
        • 1970-01-01
        • 2016-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多