Official website address: https://google.github.io/android-testing-support-library/docs/index.html
GitHub : https://github.com/googlesamples/android-testing/tree/master/ui/espresso
Add test case code to an existing project
1. Introduce dependency packages (23.1.1 will be required to use 2.2.2, and ERROR will be exposed here, so 2.2.1 will not be dropped)
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' androidTestCompile 'com.android.support.test:runner:0.4.1' androidTestCompile 'com.android.support.test:rules:0.4.1'
2. Set the build configuration
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3. For the test of the login page, there are three test cases, the test user name cannot be empty, the test password cannot be empty, and the user name and password are entered to test the correctness
/** * Login test */ @RunWith(AndroidJUnit4.class) @LargeTest public class LoginFragmentText { @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class); private MainActivity mainActivity; //There is a main Fragment after entering here, there are 4 buttons below, click one of them to enter the login page @Before public void switchToLogin () { LogUtils.debug("switchToLogin..."); onView(withId(R.id.btnProfileID)).perform(click()); mainActivity = mActivityRule.getActivity (); } //The prompts are all Toast pop-up prompts (the same below) @Test public void testUsernameNotEmpty() { onView(withId(R.id.login_login_btn)).perform(click()); onView(withText("Username cannot be empty")).inRoot(withDecorView(not(mainActivity.getWindow().getDecorView()))).check(matches(isDisplayed())); } @Test public void testPasswordNotEmpty() { //Inject username onView(withId(R.id.login_username)).perform(click(), clearText(), typeText("1234567"), closeSoftKeyboard()); //click the login button onView(withId(R.id.login_login_btn)).perform(click()); // popup prompt onView(withText("Password cannot be empty")).inRoot(withDecorView(not(mainActivity.getWindow().getDecorView()))).check(matches(isDisplayed())); } @Test public void testLogin () { //Inject username onView(withId(R.id.login_username)).perform(typeText("1234567"), closeSoftKeyboard()); //Inject password onView(withId(R.id.login_password)).perform(typeText("123456"), closeSoftKeyboard()); //click the login button onView(withId(R.id.login_login_btn)).perform(click()); //The pop-up window prompts that the login is successful onView(withText("登录成功")).inRoot(withDecorView(not(mainActivity.getWindow().getDecorView()))).check(matches(isDisplayed())); } }
When testing, especially the EditText input box is easily affected by the input method installed on the mobile phone, and the input may not be entered. At this time, you can use Thread.sleep (XXXX) to pause the capture screen, which is much easier to use than the previous Selenium. span