onView(withId(R.id.my_view)) // withId(R.id.my_view) is a ViewMatcher .perform(click()) // click() is a ViewAction .check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion
dependencies {
...
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
}
}
defaultConfig{
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityInstrumentationTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(
MainActivity.class);
@Test
public void sayHello(){
onView(withText("Say hello!")).perform(click());
onView(withId(R.id.textView)).check(matches(withText("Hello, World!")));
}
}
@Rule public ActivityTestRule mTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void testLogin() {
...
}
| 方法名 | 含义 |
|---|---|
| click() | 点击view |
| clearText() | 清除文本内容 |
| swipeLeft() | 从右往左滑 |
| swipeRight() | 从左往右滑 |
| swipeDown() | 从上往下滑 |
| swipeUp() | 从下往上滑 |
| click() | 点击view |
| closeSoftKeyboard() | 关闭软键盘 |
| pressBack() | 按下物理返回键 |
| doubleClick() | 双击 |
| longClick() | 长按 |
| scrollTo() | 滚动 |
| replaceText() | 替换文本 |
| openLinkWithText() | 打开指定超链 |
@Override
public void onClick(View view) {
if (view.getId() == R.id.bt_login) {
login();
}
}
/**
* 去登录
*/
private void login() {
String name = et_name.getText().toString().trim();
String pwd = et_pwd.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
tv_login_result.setText("用户名为空");
return;
}
if (name.length() < 6 ) {
tv_login_result.setText("用户名格式错误");
return;
}
if (TextUtils.isEmpty(pwd)) {
tv_login_result.setText("密码为空");
return;
}
if (pwd.length() < 6 ) {
tv_login_result.setText("密码格式错误");
return;
}
tv_login_result.setText("登录成功");
}
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
private String[] names = {"", "a", "123123"};
private String[] pwds = {"", "a", "123123"};
@Rule
public ActivityTestRule mTestRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void init() {
Log.e("TAG", "init: ");
}
@Test
public void testLogin() {
// 不做任何输入,直接点击登录
onView(allOf(withId(R.id.bt_login), isDisplayed())).perform(click());
onView(allOf(withId(R.id.tv_login_result), isDisplayed())).check(matches(withText("用户名为空")));
// 用户名是空,点击登录
onView(allOf(withId(R.id.et_name), isDisplayed())).perform(replaceText(names[0]), closeSoftKeyboard());
onView(allOf(withId(R.id.bt_login), isDisplayed())).perform(click());
onView(allOf(withId(R.id.tv_login_result), isDisplayed())).check(matches(withText("用户名为空")));
// 用户名格式错误,点击登录
onView(allOf(withId(R.id.et_name), isDisplayed())).perform(replaceText(names[1]), closeSoftKeyboard());
onView(allOf(withId(R.id.bt_login), isDisplayed())).perform(click());
onView(allOf(withId(R.id.tv_login_result), isDisplayed())).check(matches(withText("用户名格式错误")));
// 用户名和密码都正确,点击登录
onView(allOf(withId(R.id.et_name), isDisplayed())).perform(replaceText(names[2]), closeSoftKeyboard());
onView(allOf(withId(R.id.et_pwd), isDisplayed())).perform(replaceText(pwds[2]), closeSoftKeyboard());
onView(allOf(withId(R.id.bt_login), isDisplayed())).perform(click());
onView(allOf(withId(R.id.tv_login_result), isDisplayed())).check(matches(withText("登录成功")));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有