Espresso是谷歌力推的一个UI自动化测试框架,新建一个Andrdoid工程的时候默认就引入了Espresso的核心依赖。
Espresso和UI Automator一样,也是在项目的app/src/androidTest文件夹下编写测试代码
来看个初步案例。
测试testView是否显示
@RunWith(AndroidJUnit4.class)
public class HelloWorldTest {
@Rule
public ActivityTestRule<MainActivity> activityRule =
new ActivityTestRule<>(MainActivity.class);
@Test
public void listGoesOverTheFold() {
onView(withText("Hello World!")).check(matches(isDisplayed()));
}
}
- 通过
@Rule
注解来规定测试的Activity是MainActivity,并且测试完后关闭它 - 通过withText方法找到屏幕上的对应的控件
- onView方法可以等待寻找完成之后再工作。寻找完成之后,调用check方法来判断该控件是不是已经显示在屏幕上
- 点击类旁边的 run Test按钮 测试完成
测试点击页面上的按钮
@Test
public void clickTest(){
onView(withId(R.id.btn_click))
.perform(click())
.check(matches(isDisplayed()));
}
//id为R.id.my_view text为"Hello!"的控件
onView(allOf(withId(R.id.my_view), withText("Hello!")));
//既有text "7" 又有text "item: 0"的控件
onView(allOf(withText("7"), hasSibling(withText("item: 0"))))
.perform(click());
点击按钮,将textview的文字由helloworld改为hello espresso,查看结果。
app中代码:
final TextView textView = findViewById(R.id.tv_text);
findViewById(R.id.btn_click).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("Hello Espresso");
}
});
添加新测试:
@Test
public void matchText(){
//点击按钮,将Hello World!改为Hello Espresso!
onView(withId(R.id.btn_click)).perform(click());
//检查TextView上的文字是不是Hello Espresso!
onView(withId(R.id.tv_text)).check(matches(withText("Hello Espresso!")));
}
测试一个页面选号码然后打电话的操作
app中的步骤是这样的,点击“获取号码”按钮,通过startActivityForResult
去联系人页面选择一个号码返回保存到成员变量中,然后点击“打电话”按钮通过Intent打开系统打电话的界面。下面是测试代码:
//测试去联系人页面获取一个电话号码
@Test
public void testPickIntent(){
//创建一个Intent用来封装返回的数据
Intent intent = new Intent();
intent.putExtra(ContactsActivity.KEY_PHONE_NUMBER,"123456789");
//验证如果有相应的startActivityOnResult发生,就返回前面自己创建的结果。
intending(IntentMatchers.hasComponent(ComponentNameMatchers.hasShortClassName(".ContactsActivity")))
.respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK,intent));
//点击去联系人页面的按钮
onView(withId(R.id.btn_to_contacts)).perform(click());
//点击打电话的按钮
onView(withId(R.id.btn_call)).perform(click());
//验证打电话的Intent是否发送
intended(allOf(
hasAction(Intent.ACTION_CALL),
hasData("tel:123456789")
));
}
- 上面的测试中,intending() 方法用来测试startActivityOnResult,当startActivityOnResult动作发生的时候,就返回自己创建的一个Intent
- intended() 方法用来测试startActivity打开打电话页面的时候Intent的参数是否正确。
测试打开系统相机获取图片的动作
动作:一个Button,一个ImageView,点击按钮拍照,拍照确定后将图片显示到ImageView上面
@Test
public void testTackPhoto() throws InterruptedException {
//自定义一个拍照返回drawable图片的Intent
Intent picIntent = new Intent();
//把drawable放到bundle中传递
Bundle bundle = new Bundle();
Bitmap bitmap = BitmapFactory.decodeResource(intentsTestRule.getActivity().getResources(), R.mipmap.ic_launcher);
bundle.putParcelable("data", bitmap);
picIntent.putExtras(bundle);
Instrumentation.ActivityResult result =
new Instrumentation.ActivityResult(Activity.RESULT_OK,picIntent);
//判断是否有包含ACTION_IMAGE_CAPTURE的Intent出现,出现就给它返回result
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
//判断ImageView上没有显示drawable
onView(withId(R.id.iv_take_photo)).check(matches(not(hasDrawable())));
//点击拍照按钮去拍照界面
onView(withId(R.id.btn_take_photo)).perform(click());
//判断ImageView上显示了一个drawable
onView(withId(R.id.iv_take_photo)).check(matches(hasDrawable()));
}
private BoundedMatcher<View, ImageView> hasDrawable(){
return new BoundedMatcher<View, ImageView>(ImageView.class) {
@Override
protected boolean matchesSafely(ImageView item) {
return item.getDrawable()!=null;
}
@Override
public void describeTo(Description description) {
description.appendText("是否有drawable");
}
};
}
以上便是一些简单的测试操作,可能有些小bug,供参考理解使用。