浓缩咖啡点击菜单项 [英] Espresso click menu item

查看:29
本文介绍了浓缩咖啡点击菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在操作栏中创建了一个菜单:

@Override公共布尔 onCreateOptionsMenu(菜单菜单){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);返回真;}

和 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"工具:上下文=.MainActivity"><项目android:id="@+id/action_settings"android:title="@string/action_settings"android:orderInCategory="100"应用程序:showAsAction =从不"android:icon="@drawable/ic_settings_white_48dp"/></菜单>

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

@Test公共无效 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(工具栏);if( getSupportActionBar() != null ) {getSupportActionBar().setDisplayHomeAsUpEnabled(true);}

和 tool_bar.xml 看起来像:

</android.support.v7.widget.Toolbar>

解决方案

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

...setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)

您的问题已得到解释 此处 由 Espresso 团队提供:

<块引用>

匹配可见图标:

//点击图标 - 我们可以通过 r.Id 找到它.onView(withId(R.id.action_save)).perform(click());

<块引用>

点击溢出菜单中的项目对于正常操作栏,因为某些设备具有硬件溢出菜单按钮(他们将在选项菜单中打开溢出的项目)和一些设备有一个软件溢出菜单按钮(它们会打开一个正常的溢出菜单).幸运的是,Espresso 为我们处理了这个问题.

//打开溢出菜单或打开选项菜单,//取决于设备是否有硬件或软件溢出菜单按钮.openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());//点击项目.onView(withText("World")).perform(click());

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

在您的情况下,按钮是可见的,因为有空间,使用 withId 是正确的,例如 here:

import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;@测试public void testClickInsertItem() {openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());onView(withId(R.id.action_insert)).perform(click());}

但是这个对于溢出项也是正确的:

<块引用>

是的,这就是它在 Espresso 中的工作方式.这里的问题是,在Android,代表菜单项的视图没有ID菜单项.所以 onView(withId(X)) 只是找不到视图.我不有比仅使用 withText() 更好的建议.如果你有具有相同文本的多个视图,使用层次结构进行区分有效.

现在我的问题是,当您在不同的设备上进行测试并且不知道什么时候可以容纳某个项目时该怎么办.我不知道回应,也许结合两种解决方案.

I have a menu in the actionbar which I create through:

@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;
}

and menu_main.xml looks like:

<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>

When testing in Espresso I would like to click on the "add" icon (menuId 99). I tried

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

but this fails with a NoMatchingViewException. ( The settings item, which is defined in the xml directly I can click with the same code. )

That's for targetSdkVersion 23 and AppCompatActivity. The relevant lines for the tool bar are:

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

and tool_bar.xml looks like:

<?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>

解决方案

Update: I didn't see the final of the line, ignore my previous responses, I tried to fix it fast and I did wrong. I really didn't know the answer.

...setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)

Your question is explained here by the Espresso team:

Matching visible icons:

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

Clicking on items in the overflow menu is a bit trickier for the normal action bar as some devices have a hardware overflow menu button (they will open the overflowing items in an options menu) and some devices have a software overflow menu button (they will open a normal overflow menu). Luckily, Espresso handles that for us.

  // 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());

So I understand that both alternatives are correct but depend on your specific case. If you test a button you normally know if it's visible or not at that moment.

In your case the button is visible because there is room, and it's correct to use withId like here:

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

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

But this it's correct too for overflow items:

Yes, this is how it works in Espresso. The problem here is, that in Android, the View representing the menu item doesn't have the ID of the menu item. So onView(withId(X)) just fails to find a View. I don't have a better recommendation than just using withText(). If you have multiple views with the same text, using hierarchy for distinction works.

Now my question is what to do when you test on different devices and you don't know when will be room for an item. I don't know the response, perhaps combine both solutions.

这篇关于浓缩咖啡点击菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆