Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking toast message in android espresso

Would anyone know how to test for the appearance of a Toast message in android espresso? In robotium its easy & I used but started working in espresso but dont getting the exact command.

like image 594
Humayun Rana Avatar asked Feb 08 '15 04:02

Humayun Rana


People also ask

How do you show Toast messages on android?

Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.

What are Toast notifications Android?

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.


2 Answers

This slightly long statement works for me:

import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.RootMatchers.withDecorView; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; .... onView(withText(R.string.TOAST_STRING)).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).check(matches(isDisplayed())); 
like image 83
kowalcj0 Avatar answered Oct 09 '22 13:10

kowalcj0


The accepted answer is a good one but didn't work for me. So I searched a bit and found this blog article. This gave me an idea of how to do it and I updated the solution above.

First I implemented the ToastMatcher:

import android.os.IBinder; import android.support.test.espresso.Root; import android.view.WindowManager; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher;  public class ToastMatcher extends TypeSafeMatcher<Root> {    @Override   public void describeTo(Description description) {     description.appendText("is toast");   }    @Override   public boolean matchesSafely(Root root) {     int type = root.getWindowLayoutParams().get().type;     if (type == WindowManager.LayoutParams.TYPE_TOAST) {         IBinder windowToken = root.getDecorView().getWindowToken();         IBinder appToken = root.getDecorView().getApplicationWindowToken();         if (windowToken == appToken) {             // windowToken == appToken means this window isn't contained by any other windows.             // if it was a window for an activity, it would have TYPE_BASE_APPLICATION.             return true;         }     }     return false;   }  } 

Then I implemented my check methods like this:

public void isToastMessageDisplayed(int textId) {     onView(withText(textId)).inRoot(MobileViewMatchers.isToast()).check(matches(isDisplayed())); } 

MobileViewMatchers is a container for accessing the matchers. There I defined the static method isToast().

public static Matcher<Root> isToast() {     return new ToastMatcher(); } 

This works like a charm for me.

like image 41
Thomas R. Avatar answered Oct 09 '22 13:10

Thomas R.