|
| 1 | +package testing.android.vogella.com.asynctask; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.test.InstrumentationRegistry; |
| 5 | +import android.support.test.rule.ActivityTestRule; |
| 6 | +import android.support.test.runner.AndroidJUnit4; |
| 7 | + |
| 8 | +import org.junit.Rule; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.rules.TestName; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +import static android.support.test.espresso.Espresso.onView; |
| 14 | +import static android.support.test.espresso.action.ViewActions.click; |
| 15 | +import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 16 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 17 | +import static android.support.test.espresso.matcher.ViewMatchers.withText; |
| 18 | + |
| 19 | +@RunWith(AndroidJUnit4.class) |
| 20 | +public class EspressoTest { |
| 21 | + @Rule |
| 22 | + public MyCustomRule myRule = new MyCustomRule(); |
| 23 | + |
| 24 | + @Rule |
| 25 | + public ActivityTestRule<MainActivity> mActivityRule = |
| 26 | + new ActivityTestRule<>(MainActivity.class); |
| 27 | + |
| 28 | + @Test |
| 29 | + public void buttonShouldUpdateText(){ |
| 30 | + onView(withId(getResourceId("update"))).perform(click()); |
| 31 | + onView(withId(R.id.text)).check(matches(withText("Done"))); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void buttonShouldUpdateText2(){ |
| 36 | + onView(withId(getResourceId("update"))).perform(click()); |
| 37 | + onView(withId(R.id.text)).check(matches(withText("Done"))); |
| 38 | + } |
| 39 | + |
| 40 | + private static int getResourceId(String s) { |
| 41 | + Context targetContext = InstrumentationRegistry.getTargetContext(); |
| 42 | + String packageName = targetContext.getPackageName(); |
| 43 | + return targetContext.getResources().getIdentifier(s, "id", packageName); |
| 44 | + } |
| 45 | +} |
0 commit comments