2015-01-21 74 views
16

我嘗試了以下操作: onView(allOf(withId(R.id.single_row_text),withText(「Item1」)))。perform(click());Espresso - 單擊列表查看項目

但所有我得到的是:

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:[email protected] 

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

這有什麼建議?謝謝!

SOLUTIONS(更新)

它原來是一個時間問題。我在執行測試之前運行Thread.sleep(5000)並且它工作。

回答

44

試試這個。

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

我也試過。但事實證明,這是一個時間問題。我在執行測試之前運行Thread.sleep(5000)並且它工作。 – 2015-01-21 08:48:39

+0

意式濃縮咖啡旨在處理所有的UI時間。顯示列表之前是否加載了一些數據? – 2015-01-21 08:51:15

+0

是的,這就是爲什麼我來這個走動,但仍然不確定是否有更好的方式來實現這一點,而不使用thread.sleep – 2015-01-21 09:12:36

8

Espresso的美妙之處在於沒有處理所有這種「超時」的混亂。

是否禁用了動畫的咖啡文檔中提到:

在設備上,在設置 - >開發人員選項禁用以下3個設置:

窗口動畫縮放

過渡動畫縮放

動畫時長刻度

+1

此解決方案解決了我的問題。謝謝!! :D – 2016-07-11 18:40:26

+0

我們可以說沒有超時? – Morozov 2017-01-24 15:49:27