|
| 1 | +package com.vogella.android.espressofirst; |
| 2 | + |
| 3 | +import android.support.test.rule.ActivityTestRule; |
| 4 | +import android.support.test.runner.AndroidJUnit4; |
| 5 | + |
| 6 | +import org.junit.Rule; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | + |
| 10 | +import static android.support.test.espresso.Espresso.onView; |
| 11 | +import static android.support.test.espresso.action.ViewActions.click; |
| 12 | +import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard; |
| 13 | +import static android.support.test.espresso.action.ViewActions.typeText; |
| 14 | +import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 15 | + |
| 16 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 17 | +import static android.support.test.espresso.matcher.ViewMatchers.withText; |
| 18 | + |
| 19 | + |
| 20 | +@RunWith(AndroidJUnit4.class) |
| 21 | +public class MainActivityEspressoTest { |
| 22 | + |
| 23 | + |
| 24 | + @Rule |
| 25 | + public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void ensureTextChangesWork() { |
| 29 | + // Type text and then press the button. |
| 30 | + onView(withId(R.id.inputField)) |
| 31 | + .perform(typeText("HELLO"), closeSoftKeyboard()); |
| 32 | + onView(withId(R.id.changeText)).perform(click()); |
| 33 | + |
| 34 | + // Check that the text was changed. |
| 35 | + onView(withId(R.id.inputField)).check(matches(withText("Lalala"))); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void changeText_newActivity() { |
| 40 | + // Type text and then press the button. |
| 41 | + onView(withId(R.id.inputField)).perform(typeText("NewText"), |
| 42 | + closeSoftKeyboard()); |
| 43 | + onView(withId(R.id.switchActivity)).perform(click()); |
| 44 | + |
| 45 | + // This view is in a different Activity, no need to tell Espresso. |
| 46 | + onView(withId(R.id.resultView)).check(matches(withText("NewText"))); |
| 47 | + } |
| 48 | +} |
0 commit comments