packagecom.example.administrator.teagarden.activity.login;importandroid.support.test.espresso.ViewInteraction;importandroid.support.test.filters.LargeTest;importandroid.support.test.rule.ActivityTestRule;importandroid.support.test.runner.AndroidJUnit4;importandroid.view.View;importandroid.view.ViewGroup;importandroid.view.ViewParent;importcom.example.administrator.teagarden.R;importorg.hamcrest.Description;importorg.hamcrest.Matcher;importorg.hamcrest.TypeSafeMatcher;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.replaceText;import staticandroid.support.test.espresso.matcher.ViewMatchers.isDisplayed;import staticandroid.support.test.espresso.matcher.ViewMatchers.withClassName;import staticandroid.support.test.espresso.matcher.ViewMatchers.withId;import staticandroid.support.test.espresso.matcher.ViewMatchers.withText;import staticorg.hamcrest.Matchers.allOf;import staticorg.hamcrest.Matchers.is;
@LargeTest
@RunWith(AndroidJUnit4.class)public classSplashActivityTest {
@Rulepublic ActivityTestRule mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);
@Testpublic voidsplashActivityTest() {//Added a sleep statement to match the app's execution delay.//The recommended way to handle such scenarios is to use Espresso idling resources:// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try{
Thread.sleep(6000);
}catch(InterruptedException e) {
e.printStackTrace();
}
ViewInteraction appCompatEditText=onView(
allOf(withId(R.id.login_edit_user),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.LinearLayout")),2),0),
isDisplayed()));
appCompatEditText.perform(click());
ViewInteraction appCompatEditText2=onView(
allOf(withId(R.id.login_edit_user),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.LinearLayout")),2),0),
isDisplayed()));
appCompatEditText2.perform(replaceText("152****3478"), closeSoftKeyboard());
ViewInteraction appCompatEditText3=onView(
allOf(withId(R.id.login_edit_mima),
childAtPosition(
allOf(withId(R.id.logint_edit_layout),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),4)),0),
isDisplayed()));
appCompatEditText3.perform(replaceText("admin000"), closeSoftKeyboard());
ViewInteraction appCompatButton=onView(
allOf(withId(R.id.login_button), withText("登录"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.RelativeLayout")),1),2),
isDisplayed()));
appCompatButton.perform(click());//Added a sleep statement to match the app's execution delay.//The recommended way to handle such scenarios is to use Espresso idling resources:// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try{
Thread.sleep(3000);
}catch(InterruptedException e) {
e.printStackTrace();
}
ViewInteraction radioButton=onView(
allOf(withId(R.id.main_home_radio2), withText("监控"),
childAtPosition(
allOf(withId(R.id.main_tab_group),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),0)),1),
isDisplayed()));
radioButton.perform(click());//Added a sleep statement to match the app's execution delay.//The recommended way to handle such scenarios is to use Espresso idling resources:// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try{
Thread.sleep(3000);
}catch(InterruptedException e) {
e.printStackTrace();
}
ViewInteraction radioButton2=onView(
allOf(withId(R.id.main_home_radio3), withText("动态"),
childAtPosition(
allOf(withId(R.id.main_tab_group),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),0)),2),
isDisplayed()));
radioButton2.perform(click());//Added a sleep statement to match the app's execution delay.//The recommended way to handle such scenarios is to use Espresso idling resources:// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try{
Thread.sleep(3000);
}catch(InterruptedException e) {
e.printStackTrace();
}
ViewInteraction radioButton3=onView(
allOf(withId(R.id.main_home_radio4), withText("我的"),
childAtPosition(
allOf(withId(R.id.main_tab_group),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),0)),3),
isDisplayed()));
radioButton3.perform(click());
}private static MatcherchildAtPosition(final Matcher parentMatcher, final intposition) {return new TypeSafeMatcher() {
@Overridepublic voiddescribeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Overridepublic booleanmatchesSafely(View view) {
ViewParent parent=view.getParent();return parent instanceof ViewGroup &&parentMatcher.matches(parent)&&view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}