web-dev-qa-db-fra.com

Espresso - cliquez sur un seul élément de la liste

J'ai essayé de faire ce qui suit:

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

Mais tout ce que j'ai, c'est:

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}

J'ai aussi essayé beaucoup de trucs onData mais ils échouent tous:

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

Des suggestions à ce sujet? Merci!

SOLUTIONS (Mise à jour)

Cela s'avère être un problème de timing. J'ai exécuté Thread.sleep (5000) avant d'exécuter le test et cela a fonctionné.

21
Great Question

Essaye ça.

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

La beauté d'Espresso réside dans le fait de ne pas faire face à tout ce gâchis de "timeout".

Avez-vous désactivé les animations comme mentionné dans la documentation Espresso:

Sur votre appareil, sous Paramètres-> Options développeur, désactivez les 3 paramètres suivants:

Échelle d'animation de fenêtre

Échelle d'animation de transition

Échelle de durée de l'animateur

?

9
lenrok258

Si vous ne connaissez pas l'ID de mise en page du ListView, vous pouvez toujours exécuter un test Espresso dessus:

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

Cela fonctionnera sur les boîtes de dialogue et DialogFragments qui affichent une liste d'éléments. Et vous n'avez pas besoin de spécifier l'ID ListView.

Source: https://github.com/shohrabuddin/Espresso


Remarque: pour ajouter rapidement, ajoutez les importations de ces méthodes, placez le curseur clignotant sur la méthode non résolue, puis faites Android Studio ➔ Aide ➔ Rechercher une action ➔ recherchez "show intention action" ➔ cliquez sur l'option de résultat ➔ Une fenêtre apparaîtra ➔ cliquez sur "Import static method ...". Vous pouvez également attribuer un raccourci clavier à "Afficher les actions d'intention". Plus d'informations ici . Une autre façon consiste à activer "Add unambiguous imports on the fly" dans les paramètres.

2
Mr-IDE