Automate UI testing with the espresso framework for automated actions
Create a mainactivityinstrumentedtest Java file within a folder with the suffix androidtest
Packagecom.example.test;Importandroid.support.test.filters.LargeTest;ImportAndroid.support.test.rule.ActivityTestRule;ImportANDROID.SUPPORT.TEST.RUNNER.ANDROIDJUNIT4;ImportOrg.junit.Rule;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Import StaticAndroid.support.test.espresso.Espresso.onView;Import StaticAndroid.support.test.espresso.action.ViewActions.click;Import StaticAndroid.support.test.espresso.action.ViewActions.closeSoftKeyboard;Import StaticAndroid.support.test.espresso.action.ViewActions.typeText;Import Staticandroid.support.test.espresso.assertion.ViewAssertions.matches;Import Staticandroid.support.test.espresso.matcher.ViewMatchers.withId;Import StaticAndroid.support.test.espresso.matcher.ViewMatchers.withText; the @RunWith (ANDROIDJUNIT4.class) @LargeTest Public classMainactivityinstrumentedtest {Private Static FinalString string_to_be_typed = "Him"; //What to write@Rule PublicActivitytestrule<mainactivity> Mainactivitytestrule =NewActivitytestrule<mainactivity> (mainactivity.class); @Test Public voidSayHello () {Onview (Withid (R.id.edittext)). Perform (TypeText (string_to_be_typed), Closesoftkeyboard ()); //gets the ID of the EditText and writes the value toOnview (Withtext ("Say hello") . Perform (click ()); //Use the Text position button of the control and implement the Click actionString Expectedtext = "Hello," + string_to_be_typed + "!"; //the correct output contentOnview (Withid (R.id.textview)). Check (Matches (Withtext (Expectedtext))); //gets the ID of the TextView, which is more consistent than the text on the control }}
Run the test class, which will be executed automatically within the virtual machine
If correct, the display passed indicates that the
If an error is displayed, failed indicates that the test failed.
Use the Espresso test framework for UI testing in an Android studio environment