Skip to content

Instantly share code, notes, and snippets.

@brunodles
Last active April 5, 2016 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunodles/603742918acbe985f9334d54b4eae0bf to your computer and use it in GitHub Desktop.
Save brunodles/603742918acbe985f9334d54b4eae0bf to your computer and use it in GitHub Desktop.
This is a test to show how to use expresso to check if a toast was displayed.
package com.github.brunodles.toastespresso;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
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.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ApplicationTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
@Test
public void shouldShowToast() {
onView(withText("Click on this button")).perform(click());
onView(withText(R.string.toast)).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment