20

C'è un modo per testare usando Espresso che lo snackbar si presenta con il testo giusto?Test di snack con Espresso

Ho una semplice chiamata per creare uno snack bar

Snackbar.make(mView, "My text", Snackbar.LENGTH_LONG).show(); 

Ho provato questo senza fortuna

onView(withText("My text")).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed())); 

risposta

36

Questo ha funzionato per me, si prega di provare.

onView(allOf(withId(android.support.design.R.id.snackbar_text), withText("My text"))) 
      .check(matches(isDisplayed())); 
+0

Questo non fornisce una risposta alla domanda. Per criticare o richiedere chiarimenti da un autore, lascia un commento sotto il loro post. – Sufian

+0

La nuova formulazione funziona? – ksarmalkar

+0

Sì, ha funzionato! Grazie @ksarmalkar – SleepingLlama

6

Un'alternativa

private void checkSnackBarDisplayedByMessage(
     @StringRes int message) { 
    onView(withText(message)) 
      .check(matches(withEffectiveVisibility(
        ViewMatchers.Visibility.VISIBLE 
      ))); 
} 
0

ho visto le risposte precedenti, ma ho pensato che questo sarebbe stato meglio.

@Test 
public void onFabClick_shouldDisplaySnackbar() throws Exception { 
    onView(withId(R.id.fab)).perform(click()); 

    // Compare with the text message of snackbar 
    onView(withText(R.string.snackbar_message)) 
     .check(matches(isDisplayed())); 
}