I\'m trying to write a simple test where I simply click on a MenuItem that is in the main activity:
public class doTest extends ActivityInstrumentationTestC
haffax's answer is right. The menu item and the View generated for the menu have different IDs. Using withText
is the best known practice in this case.
To avoid hardcoding a text like 'Add new' in your test, I would recommmand using a String reference. E.g.,
<item
android:id="@+id/create_new"
android:title="@string/action_create_new"
tools:ignore="HardcodedText"
/>
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.