Espresso测试框架可减少重复性的UI操作测试,有时间还是要试下。
以前调试过Espresso,但没有记录下来,现在又得重新百度。
本文参考 Android 自动化测试 Espresso篇:简介&基础使用,但参考过程中还是遇到了点坑,因此做下记录。
本人开发环境:
Android Studio 3.1.2
compileSdkVersion 27
界面代码见本人的另一篇文章
Hello ButterKnife —— 第一次使用安卓ButterKnife
新建项目后,会自动添加如下依赖
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
由于用到
android.support.test.rule
因此添加依赖
androidTestImplementation 'com.android.support.test:rules:1.0.2'
包括UI界面用到黄油刀,dependencies如下
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
在androidTest文件夹下添加测试类 MainActivityTest,测试代码如下
package com.jsf.helloespresso;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.clearText;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withHint;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.IsNot.not;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
//
@Test
public void clickTest() {
//tvContent是否默认不显示
onView(withId(R.id.tv_content))
.check(matches(not(isDisplayed()))); //是否不可见
//检查btn_alter的text,然后执行点击事件
onView(withId(R.id.btn_alter))
.check(matches(withText("修改内容")))
.perform(click());
//检查tv内容是否修改,并且是否可见
onView(withId(R.id.tv_content))
.check(matches(withText("hello espresso!")))
.check(matches(isDisplayed()));
}
@Test
public void loginTest() throws Exception {
//先清除editText的内容,然后输入,然后关闭软键盘,最后校验内容
//这里如果要输入中文,使用replaceText()方法代替typeText()
onView(withId(R.id.et_01))
.perform(clearText(), replaceText("你好 username"), closeSoftKeyboard())
.check(matches(withText("你好 username")));
//点击登录
onView(withId(R.id.btn_login))
.perform(click());
//校验内容
onView(withId(R.id.tv_content))
.check(matches(withText("success")))
.check(matches(isDisplayed()));
onView(withId(R.id.et_01))
.check(matches(withText(""))) //内容是否为""
.check(matches(withHint("请输入账户名"))) //hint内容是否为"请输入账户名"
.check(matches(withHint(containsString("账户名")))); //hint内容是否包含"账户名"
Thread.sleep(3000);
}
}
注意是在 androidTest 文件夹下,而不是在 test 文件夹下,否则引入 ActivityTestRule 会报错。
经过上面的准备已可以通过编译,但运行测试时却报了如下错误
Process finished with exit code 1
Class not found: "com.jsf.helloespresso.MainActivityTest"Empty test suite.
需要添加配置
1、点击“Edit Configurations...”
2、添加 Android Instrumented Tests
3、添加相关测试类
4、点击AndroidStudio如下位置,即可测试该类或者某个方法。
结束。