Espresso点击菜单项

问题描述 投票:18回答:7

我在操作栏中有一个菜单,我通过以下方式创建:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(Menu.NONE, 98,Menu.NONE,R.string.filter).setIcon(R.drawable.ic_filter_list_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    menu.add(Menu.NONE, 99,Menu.NONE,R.string.add).setIcon(R.drawable.ic_add_white_48dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);


    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

和menu_main.xml看起来像:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never"
        android:icon="@drawable/ic_settings_white_48dp"/>
</menu>

在Espresso中测试时,我想点击“添加”图标(menuId 99)。我试过了

@Test
public void testAdd() {
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
    onView(withText(R.string.add)).perform(click());
}

但是这会因NoMatchingViewException而失败。 (设置项,直接在xml中定义,我可以使用相同的代码单击。)

这是针对targetSdkVersion 23和AppCompatActivity的。工具栏的相关行是:

Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
if( getSupportActionBar() != null ) {
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

和tool_bar.xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"
    tools:ignore="UnusedAttribute">
</android.support.v7.widget.Toolbar>
java android testing android-actionbar android-espresso
7个回答
36
投票

更新:我没有看到最后一行,忽略我之前的回复,我试图快速解决它,我做错了。我真的不知道答案。

...setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)

您的问题由Espresso团队解释here

匹配可见图标:

  // Click on the icon - we can find it by the r.Id.
  onView(withId(R.id.action_save))
    .perform(click());

单击溢出菜单中的项目对于正常操作栏来说有点棘手,因为某些设备具有硬件溢出菜单按钮(它们将在选项菜单中打开溢出项目),并且某些设备具有软件溢出菜单按钮(它们将打开正常的溢出菜单)。幸运的是,Espresso为我们处理这个问题。

  // Open the overflow menu OR open the options menu,
  // depending on if the device has a hardware or software overflow menu button.
  openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

  // Click the item.
  onView(withText("World"))
    .perform(click());

所以我理解两种选择都是正确的,但取决于你的具体情况。如果你测试一个按钮,你通常知道那时它是否可见。

在你的情况下,按钮是可见的,因为有空间,使用像withId这样的here是正确的:

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

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

this对于溢出物品也是正确的:

是的,这就是Espresso的工作原理。这里的问题是,在Android中,表示菜单项的View没有菜单项的ID。因此onView(withId(X))无法找到View。我没有比使用withText()更好的建议。如果您有多个具有相同文本的视图,则使用层次结构进行区分工作。

现在我的问题是当你在不同的设备上进行测试时你该怎么做,而你不知道什么时候会有一个项目的空间。我不知道答案,也许结合两种解决方案。


3
投票

您可以使用此方法单击菜单项。首先使用此代码单击菜单按钮

openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

然后根据文本执行单击菜单项。使用“MenuItemName”放置菜单项名称

onView(withText("MenuItemName")).perform(click());

1
投票
Espresso.openContextualActionModeOverflowMenu() 

是唯一适用于我的三星Note 3的选项。这个openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());会尝试失败,没有任何错误


1
投票

使用此选项匹配菜单项的描述文本:

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

然后,您不必匹配(不存在的)ID,也不必匹配可绘制的图标。

此外,如果您需要确保扩展菜单,请记住使用the Espresso documentation中的此代码段:

// Open the overflow menu OR open the options menu,
// depending on if the device has a hardware or software overflow menu button.
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

1
投票

不要受苦很多。让我们简单吧。

onView(withId(98)).perform(click());

这将帮助您执行单击“添加”


0
投票

工具栏不是actionBar,不需要调用:

openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());

如果你这样做,那肯定你的项目不会在那里找到(它不在ActionBar中)并且ToolBar属于“普通视图”。


0
投票

这是我的解决方案,它涵盖了所有三种情况:硬件菜单按钮,溢出菜单,仅图标:

    try {
        openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
    } catch (Exception e) {
        //This is normal. Maybe we dont have overflow menu.
    }
    onView(anyOf(withText(R.string.<your label for the menu item>), withId(R.id.<id of the menu item>))).perform(click());
© www.soinside.com 2019 - 2024. All rights reserved.