Android Espresso-单击带有图像和文本的导航抽屉项

问题描述:

当导航抽屉包含带有图像和文本视图的行列表时,如何单击导航抽屉项目?

How can I click on a navigation drawer item when the navigation drawer consists of a list of rows with an image and a textview?

我从以下位置使用了Espresso测试源示例:git/testapp_test/src/main/java/com/google/android/apps/common/testing/ui/testapp/DrawerActionsTest.java

I used the Espresso test source example from: git/ testapp_test/ src/ main/ java/ com/ google/ android/ apps/ common/ testing/ ui/ testapp/ DrawerActionsTest.java

我从贡献中检索了DrawerActions和DrawerMatchers并将它们放在本地的测试项目中.

I retrieved the DrawerActions and DrawerMatchers from the contribution and put them locally in my test project.

导航抽屉行是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp" 
    android:background="@drawable/list_selector">

    <ImageView
        android:id="@+id/icon"
        etc ... />

    <TextView
        android:id="@+id/title"
        etc.... />

</RelativeLayout>

导航项为:

public class NavDrawerItem {
    public String title;
    public int icon;
    ....

    // a matcher
    @Override
    public boolean equals( Object mob2) {
        String otherName = ((NavDrawerItem) mob2).title;
        return( title.equals( otherName));
    }
} 

NavigationDrawerAdapter填充导航抽屉视图.

A NavigationDrawerAdapter populates the navigation drawer view.

Espresso测试源打开抽屉,关闭抽屉,然后重新打开...但是我找不到第一个导入"项目的匹配项.因此,测试会在执行点击时停止.

The Espresso test source opens the drawer, closes it, re-opens it ... but I cannot find a match for the first "Import" item. So the test stops on the perform click.

代码是:

public LearnerAppAutoTest() {
    super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
    super.setUp();
    getActivity();
}

public void testOpenAndCloseDrawer() {
    openDrawer(R.id.drawer_layout);
    closeDrawer(R.id.drawer_layout);

    openDrawer(R.id.drawer_layout);
    onView(withId(R.id.drawer_layout)).check(matches(isOpen()));

    String rowContents = "Import";

    // Option 1: too many lists having an "Import" string
    onData( allOf( is( instanceOf( String.class)), is( rowContents))).perform(click());

    // Option 2: selecting on NavDrawerItem.class and a matcher
    //    ... still all matches, also in other lists match. Why? They don't have NavDrawerItems. 
    onData( allOf( is( instanceOf( NavDrawerItem.class)), is( rowContents))).perform(click());

    // Option 3: custom matcher
    //   ... still all matches, also in other lists match. Why?
    onData( allOf( instanceOf( NavDrawerItem.class), navDrawerItemHavingName( rowContents))).perform( click());

  }

因此,无论我编写什么程序,都存在多个匹配器...即使在没有NavDrawerItem类的列表中也是如此.

So, whatever I program, there are multiple matchers ... even from lists that don't have NavDrawerItem classes.

尝试一下:

Espresso.onView(Matchers.allOf(ViewMatchers.withId(R.id.drawerItemNameTextView), ViewMatchers.hasSibling(ViewMatchers.withText(((NavDrawerItem)item).getItemName())))).perform(ViewActions.click());