Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Snackbar show with Espresso

Is there a way to test using Espresso that the snackbar shows up with the right text?

I have a simple call to create a snackbar

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

I have tried this without luck

onView(withText("My text")).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed())); 
like image 905
SleepingLlama Avatar asked Oct 13 '15 20:10

SleepingLlama


People also ask

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.

What is espresso software testing?

Espresso created by Google is a native framework for Android automated testing. The tool is a part of the Android SDK and is easy to use for native mobile development. Thanks to Espresso, you can create tests that are close to the Android app's logic.


1 Answers

This worked for me, please try.

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

If you use AndroidX, please use the following:

onView(withId(com.google.android.material.R.id.snackbar_text))         .check(matches(withText(R.string.whatever_is_your_text))) 
like image 115
ksarmalkar Avatar answered Sep 20 '22 09:09

ksarmalkar