我们已经看到了如何使用意图进行应用间测试,到目前为止,我们已经看到了如何测试Dialer和Browser活动。在帖子中,我将为您提供测试Camera Activity的简单示例。有许多应用程序使用相机捕获图像并共享图像。现在,使用Espresso Intent可以非常轻松地测试这种情况。
正在测试的应用-
测试案例方案-
1.启动应用程序。
2.拿 a photo
3.检查照片是否已拍摄并显示在“图像视图”框中。
测试用例代码说明-
1.我们需要创建一个自定义匹配器,以检查图像视图中是否包含图像。因此,在名称为ImageViewMatcher的androidTest文件夹中创建一个新的Java类,然后复制下面给出的代码。
2.在“测试用例”类中,我们将使用应用启动器图标图像对摄像机活动进行存根,以便我们进行交叉检查。因此,我们使用createImageCaptureStub()方法将应用程序可绘制图像放入包中,并使用包创建Intent,然后传递与ActivityResult相同的intent。
3.在执行测试用例之前,我们先将意图存根,以便当应用启动相机时,可绘制图像将作为预期存根的一部分传递。
4.然后在我们的测试用例中,单击“ camera”按钮,该按钮将调用“意图”,并且我们获得了先前绘制的可绘制图像,并且可以在我们的测试用例中对其进行断言。
自定义匹配器-
package com.example.anuja.cameraintentsample; /** * Created by Anuja on 24-02-2016. */ import android.support.test.espresso.matcher.BoundedMatcher; import android.view.View; import android.widget.ImageView; import org.hamcrest.Description; public class ImageViewMatcher { public static BoundedMatcher<View, ImageView> hasDrawable() { return new BoundedMatcher<View, ImageView>(ImageView.class) { @Override public void describeTo(Description description) { description.appendText("has drawable"); } @Override public boolean matchesSafely(ImageView imageView) { return imageView.getDrawable() != null; } }; } }
测试用例 -
package com.example.anuja.cameraintentsample; import android.app.Activity; import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Bundle; import android.provider.MediaStore; import android.support.test.espresso.intent.rule.IntentsTestRule; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static android.app.Instrumentation.ActivityResult; import static android.support.test.espresso.浓咖啡.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.intent.Intents.intending; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static com.example.anuja.cameraintentsample.ImageViewMatcher.hasDrawable; import static org.hamcrest.Matchers.not; /** * Created by Anuja on 24-02-2016. */ @RunWith(AndroidJUnit4.class) @LargeTest public class CameraIntentTest { @Rule public IntentsTestRule<ImageViewerActivity> mIntentsRule = new IntentsTestRule<>( ImageViewerActivity.class); @Before public void stubCameraIntent() { ActivityResult result = createImageCaptureStub(); // Stub the Intent. intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result); } @Test public void testTakePhoto() { // Check that the ImageView doesn't have a drawable applied. onView(withId(R.id.imageView)).check(matches(not(hasDrawable()))); // Click on the button that will trigger the stubbed intent. onView(withId(R.id.button_take_photo)).perform(click()); // With no user interaction, the ImageView will have a drawable. onView(withId(R.id.imageView)).check(matches(hasDrawable())); } private ActivityResult createImageCaptureStub() { // Put the drawable in a bundle. Bundle bundle = new Bundle(); bundle.putParcelable(ImageViewerActivity.KEY_IMAGE_DATA, BitmapFactory.decodeResource( mIntentsRule.getActivity().getResources(), R.drawable.ic_launcher)); // Create the Intent that will include the bundle. Intent resultData = new Intent(); resultData.putExtras(bundle); // Create the ActivityResult with the Intent. return new ActivityResult(Activity.RESULT_OK, resultData); } }
我希望这篇文章可以帮助您找到测试服的代码范围:)
请在下面的评论部分中分享您的反馈,并按照质量检查自动化以获取最新的帖子更新。HappyTesting :-)
此评论已被作者删除。
回复删除什么是KEY_IMAGE_DATA?
回复删除你好,我收到此错误```层次结构中没有找到匹配的视图:ID:154541016(未找到资源名称)```
回复删除打开后。
我的意思是打开相机后
删除从文件管理器中选择图像如何做同样的事情?
回复删除出现以下错误:
回复删除W / ActivityTestRule:getActivityIntent()使用默认值返回null:Intent(Intent.ACTION_MAIN)