Clicking on a menu item in action bar with Espresso when using onView(withId(...))
Asked Answered
L

1

9

I am making a test using Espresso, first I open the action bar:

openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

Everything ok up to here, but then I want to click on an option menu:

onView(withId(R.id.action_menu)).perform(click());

The menu layout xml has an option with id: "action_menu".

I get an error:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: ***.********.******.android.debug:id/action_menu
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.support.v7.widget.ListPopupWindow$DropDownListView{43fd3f58 VFED.VC. .F...... 24,24-612,168}

I got this step to work by using:

onView(withText(R.string.action_report)).perform(click());

I would like to know what is going wrong with the id though.

Any ideas?

Losel answered 26/2, 2016 at 11:11 Comment(2)
Does the item really have id="@+id/action_menu"? It sounds from the withText matcher that it could be more specific, like report_action.Convulsive
Hi siger, yes the item does have the id android:id="@+id/action_menu" but espresso is only finding by text. Still don't know why...Losel
E
2

It seems that the view representing the menu does not know the id, but only the text of an item. I am new to Espresso and therefore cannot give more details, but this post is related to your problem : Espresso NoMatchingViewException when using withId matcher

Edrisedrock answered 27/2, 2016 at 21:56 Comment(1)
Thanks @Coralie B I hadn't been able to find that post before.Losel

© 2022 - 2024 — McMap. All rights reserved.