繁体   English   中英

在浓缩咖啡中获取一个项目

[英]Get an item of an item in espresso

我是Espresso的初学者。 我有这个menu.xml文件:

<?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>

我想单击具有id configuration的项目,然后单击具有id add_sound 因此,我输入了以下代码:

public void menuConfigurationTest()
    {
        onView(withId(R.id.configuration)).perform(click());
        onView(withId(R.id.add_sound)).perform(click());
    }

但是我得到这个错误:

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}

我做的事怎么了?

问题在于子菜单显示在PopupWindow中,这不是活动的视图层次结构的一部分。 因此,您必须添加:

.inRoot(RootMatchers.isPlatformPopup())

接下来的事情是项目显示在名为MenuDropDownListView的特殊ListView中。 因此onView()在这里不起作用,您必须使用onData()。

因此,完整的表达式是:

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());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM