简介
Espresso是Google推荐的测试框架,可以测试UI界面。通过简单例子的介绍,清晰整个工作流程。
如图:
Espresso基本只做3件事
- onView—找到视图(控件,或者操作的对象)
- perform—执行动作
- check—检查结果
就像程序猿平时的工作:写一个界面和逻辑、运行、查看结果是否符合预期。不多废话,上例子。
例子
环境
app/build.gradle
dependencies {
//....
compile 'com.android.support:support-annotations:25.3.0'
androidTestCompile 'com.android.support:support-annotations:25.3.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
}
指定两个support-annotations的原因:会报正式环境和测试环境模块版本不一致的错。
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
指定测试运行环境
代码
public class MainActivity extends AppCompatActivity {
public static final String NAME = "android";
public static final String PWD = "123456";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvLoginResult = (TextView) findViewById(R.id.tvLoginResult);
String name = getIntent().getStringExtra("name");
String pwd = getIntent().getStringExtra("pwd");
if (NAME.equals(name) && PWD.equals(pwd)) {
tvLoginResult.setText("登录成功");
} else {
tvLoginResult.setText("登录失败");
}
}
public class LoginActivity extends AppCompatActivity {
@BindView(R.id.edtName)
EditText edtName;
@BindView(R.id.edtPwd)
EditText edtPwd;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
}
@OnClick({R.id.btnLogin})
public void onClick(View view) {
switch (view.getId()){
case R.id.btnLogin:
Intent intent = new Intent(this,MainActivity.class);
intent.putExtra("name",edtName.getText().toString().trim());
intent.putExtra("pwd",edtPwd.getText().toString().trim());
startActivity(intent);
break;
}
}
}
两个界面代码,第一个LoginActivity界面,两个编辑框,一个按钮,点击按钮跳到MainActivity,并根据传过来的参数显示登陆成功或者失败。
在目录下创建LoginActivityTest测试类。
@RunWith(AndroidJUnit4.class)
public class LoginActivityTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
LoginActivity.class);
@Test
public void loginWithWrongPassword(){
onView(withId(R.id.edtName)).perform(typeText("android"), closeSoftKeyboard());
onView(withId(R.id.edtPwd)).perform(typeText("wrong"),closeSoftKeyboard());
onView(withId(R.id.btnLogin)).perform(click());
onView(withId(R.id.tvLoginResult)).check(matches(withText("登录失败")));
}
@Test
public void loginWithRightPassword(){
// onView(withId(R.id.edtName)).perform(typeText("android"), closeSoftKeyboard());
// onView(withId(R.id.edtPwd)).perform(typeText("123456"),closeSoftKeyboard());
onView(withId(R.id.edtName)).perform(replaceText("android"));
onView(withId(R.id.edtPwd)).perform(replaceText("123456"));
onView(withId(R.id.btnLogin)).perform(click());
onView(withId(R.id.tvLoginResult)).check(matches(withText("登录成功")));
}
}
测试类名需要规范:测试目标+Test,据说不规范会出现各种错误,呵呵,没试过反正被吓着了。
测试代码很简单,根据Espresso的3件事,可得到测试逻辑:在编辑框中输入账号和密码然后点击按钮跳到Main界面,判断是否登录成功,区别在于输入不同导致的结果也不同。
上面的例子有个问题,typeText类是模拟打字输入文本,而我的输入法是搜狗,会导致无法输入。所以我改成replaceText替换文本.
运行
运行时请注意,界面必须亮屏,不然Activity无法启动导致执行失败。
1、LoginActivityTest上点击右键,Run
2、Menu->Run->Edit Configuration里设置后点击Toolbar中运行键。
结果:
如果出错,会显示哪个case出错。
尾巴
Espresso初学,描述也有不当,请指出。
Espresso用起来跟写脚本差不多,继续学习ing。