簡體   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