Question: 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.

Answer: 
First create a cutom Toast Matcher which we can use in our test cases -

```
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) {
                  //means this window isn't contained by any other windows. 
                  return true;
                }
            }
            return false;
        }
}

```

**1. Test if the Toast Message is Displayed**

```
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(isDisplayed()));

```

**2. Test if the Toast Message is not Displayed**

```
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(not(isDisplayed())));

```

**3. Test id the Toast contains specific Text Message**

```
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(withText("Invalid Name"));

```

Thanks,
Anuja

Note - this answer is from [This POST.](http://www.qaautomated.com/2016/01/how-to-test-toast-message-using-espresso.html)
I would like to suggest an alternative method, especially if you need to check that particular toast is **NOT** displayed

The problem here that

```
onView(viewMatcher)
    .inRoot(RootMatchers.isPlatformPopup())
    .check(matches(not(isDisplayed())))

```

or 

```
onView(viewMatcher)
    .inRoot(RootMatchers.isPlatformPopup())
    .check(doesNotExist())

```

or any other custom `inRoot` checks
are throwing `NoMatchingRootException` even before the code passes to `check` method

You may just catch the exception and complete the test but that's not a good option since throwing and catching `NoMatchingRootException` consumes a lot of time in a comparison with the default test case. Seems that Espresso is waiting for the Root for a while

For this case is suggest just to give up with espresso here and use `UiAutomator` for this assertion. The `Espresso` and `UiAutomator` frameworks could easily work together in one environment.

```
val device: UiDevice
   get() = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

fun assertPopupIsNotDisplayed() {
    device.waitForIdle()
    assertFalse(device.hasObject(By.text(yourText))))
}

fun assertPopupIsDisplayed() {
    device.waitForIdle()
    assertTrue(device.hasObject(By.text(yourText))))
}

```