Skip to content

Instantly share code, notes, and snippets.

@Cronch
Created January 29, 2016 03:06
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 Cronch/8d4bf0e1f3024a0c0fdb to your computer and use it in GitHub Desktop.
Save Cronch/8d4bf0e1f3024a0c0fdb to your computer and use it in GitHub Desktop.
Some practical examples of Espresso
@Test
public void ensureTextChangesWork() {
// Type text and then press the button.
onView(withId(R.id.inputField))
.perform(typeText("HELLO"), closeSoftKeyboard());
onView(withId(R.id.changeText)).perform(click());
// Check that the text was changed.
onView(withId(R.id.inputField)).check(matches(withText("Lalala")));
}
@Test
public void changeText_newActivity() {
// Type text and then press the button.
onView(withId(R.id.inputField)).perform(typeText("NewText"),
closeSoftKeyboard());
onView(withId(R.id.switchActivity)).perform(click());
// This view is in a different Activity, no need to tell Espresso.
onView(withId(R.id.resultView)).check(matches(withText("NewText")));
}
@Test
public void triggerIntentTest() {
// check that the button is there
onView(withId(R.id.button)).check(matches(notNullValue()));
onView(withId(R.id.button)).check(matches(withText("Start new activity")));
onView(withId(R.id.button)).perform(click());
intended(toPackage("testing.android.sample.com.simpleactivity"));
intended(hasExtra("URL", "http://www.sample.com"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment