Java 浓缩咖啡点击菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33965723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Espresso click menu item
提问by user1583209
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_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>
When testing in Espresso I would like to click on the "add" icon (menuId 99). I tried
在 Espresso 中进行测试时,我想单击“添加”图标(menuId 99)。我试过
@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. )
但这会因 NoMatchingViewException 而失败。(设置项,直接在xml中定义我可以用同样的代码点击。)
That's for targetSdkVersion 23 and AppCompatActivity. The relevant lines for the tool bar are:
那是针对 targetSdkVersion 23 和 AppCompatActivity。工具栏的相关行是:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
if( getSupportActionBar() != null ) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and tool_bar.xml looks like:
和 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>
回答by albodelu
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 hereby 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.
单击溢出菜单中的项目对于普通操作栏来说有点棘手,因为有些设备有一个硬件溢出菜单按钮(它们会在选项菜单中打开溢出的项目),而有些设备有一个软件溢出菜单按钮(它们会打开一个普通的溢出菜单)。幸运的是,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());
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:
你的情况的按钮可见,因为有房,这是正确的使用withId
像在这里:
import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
@Test
public void testClickInsertItem() {
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
onView(withId(R.id.action_insert)).perform(click());
}
But thisit'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.
是的,这就是它在 Espresso 中的工作方式。这里的问题是,在 Android 中,表示菜单项的视图没有菜单项的 ID。所以 onView(withId(X)) 只是找不到视图。我没有比仅使用 withText() 更好的建议。如果您有多个具有相同文本的视图,则使用层次结构进行区分是有效的。
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.
现在我的问题是当您在不同的设备上进行测试并且您不知道什么时候可以容纳某个项目时该怎么办。我不知道回应,也许结合这两种解决方案。
回答by Mathias Seguy Android2ee
The toolbar is not the actionBar, no need of the call :
工具栏不是 actionBar,不需要调用:
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext());
And if you do it, that's sure your item won't be found there (it's not in the ActionBar) and the ToolBar belongs to the "normal view".
如果你这样做,那肯定不会在那里找到你的项目(它不在 ActionBar 中)并且 ToolBar 属于“普通视图”。
回答by Ishan Fernando
You can use this method to click menu item. First click menu button using this code
您可以使用此方法单击菜单项。首先使用此代码单击菜单按钮
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
Then perform click menu item based on text.Replace your menu item name with "MenuItemName"
然后根据文本执行单击菜单项。将菜单项名称替换为“MenuItemName”
onView(withText("MenuItemName")).perform(click());
回答by gorbysbm
Espresso.openContextualActionModeOverflowMenu()
was the only option that worked for my Samsung Note 3. This openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
would try and fail without any error
是唯一适用于我的三星 Note 3 的选项。这openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
将尝试失败,没有任何错误
回答by guni
This is my solution which covers all the three situations: hardware menu button, overflow menu, only icons:
这是我的解决方案,涵盖了所有三种情况:硬件菜单按钮、溢出菜单、只有图标:
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());
回答by Eirik W
Use this to match on the description text for the menu item:
使用它来匹配菜单项的描述文本:
onView(withContentDescription(R.string.add)).perform(click());
Then you do not have to match on the (non-existent) ID, nor on the drawable icon.
然后您不必匹配(不存在的)ID,也不必匹配可绘制图标。
Also, remember to use this code snippet from the Espresso documentationif you need to make sure the menu is expanded:
另外,如果您需要确保菜单已展开,请记住使用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());
回答by Manish
Don't suffer a lot. Let's Make it simple.
不要受很多苦。让我们简单点。
onView(withId(98)).perform(click());
This will help you to perform click on Add
这将帮助您执行单击添加