Android EspressoTest与位置为

时间:2015-07-21 21:40:18

标签: android android-espresso

我想打开导航抽屉,点击具体代码项目,代码如下。

private void clickMenu(int position) {
    onView(withId(R.id.drawer_layout))
            .perform(actionOpenDrawer());

    onData(is(instanceOf(NavDrawerItem.class)))
            .inAdapterView(withId(R.id.navigation_list))
            .atPosition(position).perform(ViewActions.click());
}

目前它没有显示匹配。

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.trumpia.android.loyaltea.debug:id/navigation_list
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.widget.ListView{3bb66aad VFED.VC. .F...... 0,0-720,1533 #7f0f008c app:id/navigation_drawer}
- android.widget.ListView{22585c25 GFED.VC. ......I. 0,0-0,0 #102000a android:id/list}

或其他错误如下

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
(is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
Target view: "ListView{id=2131689612, res-name=navigation_drawer, visibility=INVISIBLE, width=720, height=1533, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=-720.0, y=0.0, child-count=5}"

似乎每次跑步之间都存在一些不一致。 这是因为抽屉没有完全打开?如何保证抽屉打开?

如果我评论.inAdapterView行,那么它会匹配两个项目,如下所示。

+--------->ListView{id=16908298, res-name=list, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} ****MATCHES****
...
+------>ListView{id=2131689612, res-name=navigation_drawer, visibility=VISIBLE, width=720, height=1533, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=-1.0, y=0.0, child-count=5} ****MATCHES****

有没有什么好方法可以逐项匹配导航抽屉? 我可以匹配文字但项目顺序在我的情况下很重要。

1 个答案:

答案 0 :(得分:0)

好吧,我将AdapterView的资源名称从R.id.navigation_drawer更改为R.id.navigation_list。并将espresso核心从2.0更新为2.2,然后运行良好。

private void clickMenu(int position) {
    onView(withId(R.id.drawer_layout))
            .perform(actionOpenDrawer()); 

    onData(is(instanceOf(NavDrawerItem.class))) 
            .inAdapterView(withId(R.id.navigation_drawer))
            .atPosition(position).perform(ViewActions.click());
}

NavigationDrawerFragment已替换为R.id.navigation_drawer片段MainActivity。 NavigationDrawerFragment只有R.id.navigation_list ListView