2015-04-10 12 views
5

Ich benutze Android Espresso, um meine Aktivität zu testen. Ich habe mehrere Aktionsleisten, von denen die meisten im Überlauf versteckt sind. Ich versuche, diesen Test zu laufen, aber der Fehler sagt, dass es keine Ansicht in der Hierarchie ist (anders nicht sichtbar):Wie klickt man beim Testen mit Android Espresso auf Aktionsleisten?

@MediumTest 
public void testClickInsertItem() { 
    Espresso.onView(ViewMatchers.withId(R.id.action_insert)).perform(ViewActions.click()); 
} 

Und der Fehler:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: blogr.vpm.fr.blogr:id/action_insert 
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{5283cfb0 VFED.VC. ........ 2,2-762,973 #7f07000b app:id/allitems} 

View Hierarchy: 
+>DecorView{id=-1, visibility=VISIBLE, width=768, height=1184, 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} 
| 
+->ActionBarOverlayLayout{id=16909075, res-name=action_bar_overlay_layout, visibility=VISIBLE, width=768, height=1184, 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} 
| 
+-->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=768, height=1038, 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=146.0, child-count=1} 
| 
+--->RelativeLayout{id=-1, visibility=VISIBLE, width=768, height=1038, 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} 
| 
+---->ViewPager{id=2131165186, res-name=pager, visibility=VISIBLE, width=764, height=1034, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, 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=2.0, y=2.0, child-count=3} 
| 
+----->PagerTitleStrip{id=2131165187, res-name=pager_title_strip, visibility=VISIBLE, width=764, height=59, 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=763.0, y=0.0, child-count=3} 
| 
+------>TextView{id=-1, visibility=VISIBLE, width=125, height=43, 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=8.0, text=Free text, input-type=0, ime-target=false, has-links=false} 
| 
(...) 
| 
+-->ActionBarContainer{id=16909076, res-name=action_bar_container, visibility=VISIBLE, width=768, height=96, has-focus=false, 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=50.0, child-count=2} 
| 
+--->ActionBarView{id=16909077, res-name=action_bar, visibility=VISIBLE, width=768, height=96, has-focus=false, 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=2} 
| 
+---->LinearLayout{id=-1, desc=Hello, Navigate up, visibility=VISIBLE, width=124, height=96, has-focus=false, has-focusable=true, 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=0.0, y=0.0, child-count=2} 
| 
+----->HomeView{id=-1, visibility=VISIBLE, width=97, height=96, 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} 
| 
+------>ImageView{id=16908890, res-name=up, visibility=VISIBLE, width=32, height=32, 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=32.0} 
| 
+------>ImageView{id=16908332, res-name=home, visibility=VISIBLE, width=64, height=64, 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=25.0, y=16.0} 
| 
+----->LinearLayout{id=-1, visibility=VISIBLE, width=27, height=49, 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=97.0, y=23.0, child-count=2} 
| 
+------>TextView{id=16908901, res-name=action_bar_title, visibility=VISIBLE, width=11, height=49, 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, text=Hello, input-type=0, ime-target=false, has-links=false} 
| 
+------>TextView{id=16908902, 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} 
| 
+---->ActionMenuView{id=-1, visibility=VISIBLE, width=644, height=96, has-focus=false, 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=124.0, y=0.0, child-count=3} 
| 
+----->EditText{id=2131165210, res-name=postTitle, visibility=VISIBLE, width=420, height=96, has-focus=false, has-focusable=true, 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=true, editor-info=[inputType=0x1 imeOptions=0x8000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0 hintText=New Post label=null packageName=null fieldId=0 fieldName=null extras=null ], x=0.0, y=0.0, text=Hello, hint=New Post, input-type=1, ime-target=false, has-links=false} 
| 
+----->ActionMenuItemView{id=2131165222, res-name=action_publish, desc=Publish, visibility=VISIBLE, width=112, height=96, has-focus=false, has-focusable=true, 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=420.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false} 
| 
+----->OverflowMenuButton{id=-1, desc=More options, visibility=VISIBLE, width=112, height=96, has-focus=false, has-focusable=true, 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=532.0, y=0.0} 
| 
+--->ActionBarContextView{id=16909078, 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} 
| 
+-->ActionBarContainer{id=16909079, 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} 
| 
at dalvik.system.VMStack.getThreadStackTrace(Native Method) 

Also, wie soll ich Greifen Sie auf die Schaltfläche mit der ID R.id.action_insert zu?

Ich bemerkte die OverflowMenuButton. Ich könnte darauf klicken, aber ich bekomme dann eine andere Ansichtshierarchie, die nicht wie eine Liste von Aktionselementen aussieht. Soll ich in den View-Hierarchien graben, bis ich mein Action Item finde oder gibt es eine bessere, designierte Möglichkeit, auf Action-Items im Overflow zu klicken? Ich konnte in der Android-Dokumentation keine Dokumentation zu Aktionsleisten finden.

Antwort

27

Espresso hat openActionBarOverflowOrOptionsMenu(Context context) Methode, um das Menü der Aktionsleiste zu öffnen. Sie müssen es vor Ihrer Testaktion auf die Menüpunkte setzen.

import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; 

@Test 
public void testClickInsertItem() { 
    openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getTargetContext()); 
    onView(withId(R.id.action_insert)).perform(click()); 
} 
+4

Danke, insbesondere ich schätzen Sie die statische Import erwähnen! – Vince

+0

Was ist, wenn das Gerät sowohl einen Hardware-Schlüssel als auch ein Optionsmenü in der App-Aktionsleiste hat wie bei einigen Geräten [Samsung GT-P5210]? http://StackOverflow.com/questions/30532123/espresso-not-able-to-click-on-actionbar-overflow-menu-samsung-gt-p5210 –

+0

"Wenn ein Hardware-Menüschlüssel vorhanden ist, ist das Überlaufsymbol nie in ActionBars angezeigt und kann nur über die Tastenbedienung der Menüs interagiert werden. " von hier http://developer.android.com/reference/android/support/test/espresso/Espresso.html – denys

0

Hoffe, dass es jemand mit collaspingtoolbar

Ich habe folgendes Menü-Layout helfen können.

collasping Toolbar

enter image description here

Nach Espresso-Recorder verwenden, entdeckte ich die Menütaste

enter image description here

habe ich auch ToolBar und CollapsingToolbarLayout im xml mit withContentDescription Funktion ist klicken, was ich tat in der Prüfung ist nur mit der withContentDescription, um das Menü zu öffnen.

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/action_more" 
    android:icon="@drawable/ic_more_vert_white_24dp" 
    android:title="@string/menu_action_more" 
    app:showAsAction="always"> 
    <menu> 
     <item 
     android:id="@+id/action_login" 
     android:icon="@drawable/ic_account_circle_black_24dp" 
     android:orderInCategory="100" 
     android:title="@string/menu_login" /> 
     <item 
     android:id="@+id/action_settings" 
     android:icon="@drawable/ic_settings_black_24dp" 
     android:orderInCategory="100" 
     android:title="@string/menu_settings" /> 
     <item 
     android:id="@+id/action_about" 
     android:icon="@drawable/ic_info_black_24dp" 
     android:orderInCategory="100" 
     android:title="@string/menu_about" /> 
    </menu> 
    </item> 
</menu> 

MainTest.java

 @Test 
     public void menu_Login() {   
     onView(withContentDescription(R.string.menu_action_more)) 
      .perform(click()); 
     onView(withText("Login")).perform(click()); 
     intended(hasComponent(LoginActivity.class.getName())); 
     } 
Verwandte Themen