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.stri
Espresso.openContextualActionModeOverflowMenu()
was the only option that worked for my Samsung Note 3. This openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
would try and fail without any error
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.
Also, remember to use this code snippet from the Espresso documentation if you need to make sure the menu is expanded:
// 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());
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"
onView(withText("MenuItemName")).perform(click());
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.
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());
Don't suffer a lot. Let's Make it simple.
onView(withId(98)).perform(click());
This will help you to perform click on Add