So, I would try to explain this step by step:
1) You opened the menu in this way:
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
I think you can open the same menu by putting this code:
onView(withContentDescription("More options")).perform(click());
2) You want to click an item by ID:
First, why don't you use "strings.xml". The text extracted from this file is automatically changed using the language of the set of smartphones, but only if you prepared it before the exact translation file.
What the code will look like:
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext()); onView(withText(R.string.help)).perform(click());
or
onView(withContentDescription("More options")).perform(click()); onView(withText(R.string.help)).perform(click());
Of course, you will still catch the view by its identifier, as @Rodrigo said. What the code will look like:
onView(withContentDescription("More options")).perform(click()); onView(withId(R.id.help_item)).perform(click());
Remember that in your xml files you can declare android:id , android:text or android:contentDescription for each "view".
source share