1. First, create a mainactivityinstrumentedtest Java file in a folder with the suffix androidtest,
Package com.example.pj.git;
Import Android.support.test.filters.LargeTest;
Import Android.support.test.rule.ActivityTestRule;
Import ANDROID.SUPPORT.TEST.RUNNER.ANDROIDJUNIT4;
Import Android.view.View;
Import Org.hamcrest.Matcher;
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.action.ViewActions.closeSoftKeyboard;
Import static Android.support.test.espresso.action.ViewActions.typeText;
Import static android.support.test.espresso.assertion.ViewAssertions.matches;
Import static android.support.test.espresso.matcher.ViewMatchers.withId;
Import static Android.support.test.espresso.matcher.ViewMatchers.withText;
/**
* Created by PJ on 2017/3/15.
*/
@RunWith (Androidjunit4.class)
@LargeTest
public class Mainactivityinstrumentationtest {
private static final String string_to_be_typed = "Peter";
@Rule
Public activitytestrule<mainactivity> mactivityrule = new Activitytestrule<mainactivity> ( Mainactivity.class);
@Test
public void SayHello () {
Onview (Withid (R.id.et)). Perform (TypeText (string_to_be_typed), Closesoftkeyboard ());
Onview (Withtext ("Say hello!")). Perform (click ());
String Expectedtext = "Hello," + string_to_be_typed + "!";
Onview (Withid (r.id.tv)). Check (Matches (Withtext (Expectedtext)));
}
}
2. Then run mainactivityinstrumentedtest on the emulator, if the run is successful it will prompt
If the run fails, you are prompted
Use the Espresso test framework for UI testing in an Android studio environment