2017-10-11 21 views
2

j'ai un Fragmant qui contient une CoordinatorLayout avec deux enfants: une MapView (API Google Maps) & un RelativeLayout, cette dernière mise en page contient quelques boutons que je veux tester à l'aide Espresso.Vous ne trouvez pas vue en feuille de fond avec Espresso

Alors, voici mon test:

@Test 
fun randomButtonTest() { 
    try { 
     Thread.sleep(2000) 
    } catch (e: InterruptedException) { 
     e.printStackTrace() 
    } 

    // Find the button and perform a click 
    val appCompatImageButton = onView(
      allOf(withId(R.id.random_poi), withContentDescription("Random Point of Interest"), 
        withParent(withId(R.id.design_bottom_sheet)), 
        isDisplayed())) 
    appCompatImageButton.perform(click()) 

} 

Et voici ma mise en page (je l'ai supprimé toutes les mise en page des paramètres liés à la visibilité):

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <com.google.android.gms.maps.MapView 
     android:id="@+id/mapView"/> 

    <RelativeLayout 
     android:id="@+id/design_bottom_sheet"    
     app:behavior_hideable="true" 
     app:layout_behavior="@string/bottom_sheet_behavior"> 

     <ImageButton 
      android:id="@+id/up_arrow" /> 

     <ImageButton 
      android:id="@+id/random_poi" 
      android:contentDescription="@string/random_poi"/> 

    </RelativeLayout> 


</android.support.design.widget.CoordinatorLayout> 

Je reçois l'erreur suivante lors du lancement de mon test instrumenté :

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: kade.com.accio:id/random_poi and with content description: is "Random Point of Interest" and has parent matching: with id: kade.com.accio:id/design_bottom_sheet and is displayed on the screen to the user)

Est-ce parce que mon bouton est dans une feuille inférieure? Pourquoi ne puis-je pas trouver cette vue?

Répondre

1

D'abord, vous devez charger le fragment (sinon il n'y a pas un fragment à tester) donc il y a deux façons

1.) instancier votre objet fragment et charge à l'aide FragmentManager comme

@Before // you can do it later 
    void setup(){ 
     fragment = ...  
     getActivity().getFragmentManager().beginTransaction().add(id,farment).commit() 
// or activityTestRule.getActivity() 
//   .getSupportFragmentManager().beginTransaction() 

    } 

Note: Si vos vues sont en dehors de l'écran, vous pouvez utiliser withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE) au lieu de isDisplayed()

+0

Cela semble être un pas en avant, merci, mais j'ai l'erreur suivante: > Erreur lors de l'exécution du 'clic simple' sur la vue 'Animations ou les transitions sont activées sur le périphérique cible. après avoir remplacé 'isDsplayed()' par ce que vous avez partagé. – Christopher

+0

@Christopher assurez-vous de suivre [Configurer votre environnement de test] (https://developer.android.com/training/testing/espresso/setup.html#set-up-environment) sur 'Sur votre appareil, sous Paramètres> Options du développeur, désactiver les paramètres: 'et vous devez utiliser' withEffectiveVisibility' lorsque vos vues cibles ne sont pas visibles à l'écran –