Espresso点击菜单项

use*_*209 25 java testing android android-actionbar android-espresso

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

@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;
}
Run Code Online (Sandbox Code Playgroud)

和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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

和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>
Run Code Online (Sandbox Code Playgroud)

alb*_*elu 43

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

...setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
Run Code Online (Sandbox Code Playgroud)

Espresso团队在此解释您的问题:

匹配可见图标:

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

单击溢出菜单中的项目对于正常操作栏来说有点棘手,因为某些设备具有硬件溢出菜单按钮(它们将在选项菜单中打开溢出项目),并且某些设备具有软件溢出菜单按钮(它们将打开正常的溢出菜单).幸运的是,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());
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

但是,是正确的太溢出项目:

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

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


小智 6

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

    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());
Run Code Online (Sandbox Code Playgroud)


Eir*_*k W 5

使用它来匹配菜单项的描述文本:

onView(withContentDescription(R.string.add)).perform(click());
Run Code Online (Sandbox Code Playgroud)

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

此外,如果您需要确保菜单已展开,请记住使用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());
Run Code Online (Sandbox Code Playgroud)


小智 5

别受太多苦。让我们把事情变得简单一些

onView(withId(98)).perform(click());
Run Code Online (Sandbox Code Playgroud)

这将帮助您执行单击添加