使用Espresso作为测试工具
- - 移动开发 - ITeye博客 Youtube上面关于Espresso单独出了三期的内容讲解Espresso作为整合Unit Test, Instrument Test,end-to-end test等. 官网地址: https://google.github.io/android-testing-support-library/docs/index.html.
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'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
/**
* 登录测试
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginFragmentText {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class);
private MainActivity mainActivity;
//这里进入以后 有一个主Fragment, 下面是4个按钮,点击其中一个进入登录页面
@Before
public void switchToLogin () {
LogUtils.debug("switchToLogin...");
onView(withId(R.id.btnProfileID)).perform(click());
mainActivity = mActivityRule.getActivity();
}
//提示都是Toast弹窗提示(下同)
@Test
public void testUsernameNotEmpty() {
onView(withId(R.id.login_login_btn)).perform(click());
onView(withText("用户名不能为空")).inRoot(withDecorView(not(mainActivity.getWindow().getDecorView()))).check(matches(isDisplayed()));
}
@Test
public void testPasswordNotEmpty() {
//注入用户名
onView(withId(R.id.login_username)).perform(click(), clearText(), typeText("1234567"), closeSoftKeyboard());
//点击登录按钮
onView(withId(R.id.login_login_btn)).perform(click());
//弹窗提示
onView(withText("密码不能为空")).inRoot(withDecorView(not(mainActivity.getWindow().getDecorView()))).check(matches(isDisplayed()));
}
@Test
public void testLogin () {
//注入用户名
onView(withId(R.id.login_username)).perform(typeText("1234567"), closeSoftKeyboard());
//注入密码
onView(withId(R.id.login_password)).perform(typeText("123456"), closeSoftKeyboard());
//点击登录按钮
onView(withId(R.id.login_login_btn)).perform(click());
//弹窗提示 登录成功
onView(withText("登录成功")).inRoot(withDecorView(not(mainActivity.getWindow().getDecorView()))).check(matches(isDisplayed()));
}
}