Espresso - 单击列表视图项

时间:2015-01-21 06:57:51

标签: android testing android-espresso

我尝试了以下操作:

onView(allOf(withId(R.id.single_row_text),  withText("Item1"))).perform(click());

但我得到的只是:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: net.test.android:id/single_row_text and with text: is "Item1")
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@410d6ab0

View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=480, height=800, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=480, height=800, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+-->ActionBarContainer{id=16909032, res-name=action_bar_container, visibility=VISIBLE, width=480, height=48, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, child-count=2}
|
+--->ActionBarView{id=16909033, res-name=action_bar, visibility=VISIBLE, width=480, height=48, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3}
|
+---->LinearLayout{id=-1, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=false, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+----->ImageView{id=16908838, res-name=up, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+------>TextView{id=16908845, res-name=action_bar_title, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false}
|
+------>TextView{id=16908846, res-name=action_bar_subtitle, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false}
|
+---->HomeView{id=-1, visibility=VISIBLE, width=127, height=48, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=false, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=19.0, y=0.0, child-count=2}
|
+----->ImageView{id=16908838, res-name=up, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+----->ImageView{id=16908332, res-name=home, visibility=VISIBLE, width=119, height=25, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=4.0, y=12.0}
|
+---->ActionMenuView{id=-1, visibility=VISIBLE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=480.0, y=24.0, child-count=0}
|
+--->ActionBarContextView{id=16909034, res-name=action_context_bar, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, 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}
|
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=480, height=704, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=96.0, child-count=1}
|
+--->CustomDrawerLayout{id=2131362153, res-name=main_frame, visibility=VISIBLE, width=480, height=704, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, 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=0.0, y=0.0, child-count=2}
|
+---->FrameLayout{id=2131362154, res-name=main_content_frame, visibility=VISIBLE, width=480, height=704, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}
|
+---->NoSaveStateFrameLayout{id=2131362155, res-name=menu_fragment, visibility=VISIBLE, width=260, height=704, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+----->LinearLayout{id=-1, visibility=VISIBLE, width=260, height=704, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2}
|
+------>View{id=-1, visibility=VISIBLE, width=260, height=1, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0}
|
+------>ListView{id=2131362159, res-name=browse_types_list, visibility=VISIBLE, width=260, height=703, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=1.0, child-count=0}
|
+-->ActionBarContainer{id=16909035, res-name=split_action_bar, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, 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}

我也尝试了很多onData的东西,但它们都失败了:

onData(allOf(is(AnyTextView.class), withText("Item1"))).perform(click());
onData(Matchers.allOf(Matchers.is(Matchers.instanceOf(ListView.class)), Matchers.hasToString(Matchers.startsWith("Item1")))).perform(ViewActions.click());
onData(anything()).inAdapterView(withContentDescription("menu_item_icon")).atPosition(0).perform(click());
onData(hasToString(startsWith("Item1")))
                .inAdapterView(withId(R.id.browse_types_list))
                .perform(click());

对此有何建议?谢谢!

解决方案(已更新)

事实证明这是一个时间问题。我在执行测试之前运行Thread.sleep(5000)并且它工作正常。

4 个答案:

答案 0 :(得分:55)

试试这个。

onData(anything()).inAdapterView(withId(R.id.list_view)).atPosition(0).perform(click());

答案 1 :(得分:9)

Espresso的美妙之处在于没有处理所有这些“超时”的混乱局面。

您是否禁用了Espresso文档中提到的动画:

  

在您的设备上,在“设置” - >“开发者”选项下,禁用以下3个设置:

     

窗口动画比例

     

过渡动画比例

     

Animator持续时间刻度

答案 2 :(得分:1)

如果您不知道ListView的布局ID,您仍然可以在其上运行Espresso测试:

// Perform a click on the 5th item of a ListView.
// anything() is a hamcrest matcher that always matches.
// It's useful if you don't care what the object under test is.
onData(anything()).atPosition(5).perform(click());

这将适用于显示项目列表的Dialogs和DialogFragments。而且您不需要指定ListView ID。

来源:https://github.com/shohrabuddin/Espresso

答案 3 :(得分:0)

尝试这个:

onView(withText("ItemName")).perform(ViewActions.click());