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()));
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.
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.
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With