Issue
I am a beginner in Espresso. I have this menu.xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/configuration"
android:icon="@drawable/ic_settings"
android:title="Configuration"
app:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/add_sound"
android:title="Add a sound"
app:showAsAction="ifRoom" />
<item
android:id="@+id/takeof_sound"
android:enabled="false"
android:title="Take of the sound"
app:showAsAction="ifRoom" />
<item
android:id="@+id/add_image"
android:title="Add an image"
app:showAsAction="ifRoom" />
<item
android:id="@+id/takeof_image"
android:enabled="false"
android:title="Take of the image"
app:showAsAction="ifRoom" />
</menu>
</item>
<item
android:id="@+id/add"
android:icon="@drawable/ic_add"
android:title="Add"
app:showAsAction="ifRoom" />
</menu>
I would like to perform a click on the item with id configuration
and then a click on the sub-item with id add_sound
. So, I have typed this code:
public void menuConfigurationTest()
{
onView(withId(R.id.configuration)).perform(click());
onView(withId(R.id.add_sound)).perform(click());
}
However I get this error:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.example.adrien.smartalarm:id/add_sound
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.MenuPopupWindow$MenuDropDownListView{5a0c4d8 VFED.VC.. .F...... 0,0-686,672}
What is wrong with what I have done?
Solution
The problem is the submenu gets shown in a PopupWindow, which is not part of activity's view hierarchy. Therefore you have to add:
.inRoot(RootMatchers.isPlatformPopup())
The next thing is the items are displayed in a special ListView named MenuDropDownListView. So onView() will not work here, you have to use onData().
Therefore the complete expression is:
onData(CoreMatchers.anything())
.inRoot(RootMatchers.isPlatformPopup()) // isPlatformPopup() == is in PopupWindow
.inAdapterView(CoreMatchers.<View>instanceOf(MenuPopupWindow.MenuDropDownListView.class))
.atPosition(0) // for the first submenu item, here: add_sound
.perform(click());
Answered By - dankito
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.