使用Espresso进行相机操作UI测试

Jan*_*ngo 9 android integration-testing unit-testing android-testing android-espresso

我需要使用espresso测试项目自动化我的UI测试以进行以下操作.

操作:

单击打开手机摄像头的按钮.捕获图像,并将图像保存在SD卡存储中.完成后还要在屏幕上更新小图像视图.

应用程序工作正常但是对于所有其他操作和类似的上述操作类型,一次又一次地手动测试它变得非常耗时.

Tar*_*tal 9

我正在处理类似的问题,并在下面的链接相机UI测试中找到了最佳可用解决方案

// CameraActivityInstrumentationTest.java
public class CameraActivityInstrumentationTest {

    // IntentsTestRule is an extension of ActivityTestRule. IntentsTestRule sets up Espresso-Intents
    // before each Test is executed to allow stubbing and validation of intents.
    @Rule
    public IntentsTestRule<CameraActivity> intentsRule = new IntentsTestRule<>(CameraActivity.class);

    @Test
    public void validateCameraScenario() {
        // Create a bitmap we can use for our simulated camera image
        Bitmap icon = BitmapFactory.decodeResource(
                InstrumentationRegistry.getTargetContext().getResources(),
                R.mipmap.ic_launcher);

        // Build a result to return from the Camera app
        Intent resultData = new Intent();
        resultData.putExtra("data", icon);
        Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);

        // Stub out the Camera. When an intent is sent to the Camera, this tells Espresso to respond 
        // with the ActivityResult we just created
        intending(toPackage("com.android.camera2")).respondWith(result);

        // Now that we have the stub in place, click on the button in our app that launches into the Camera
        onView(withId(R.id.btnTakePicture)).perform(click());

        // We can also validate that an intent resolving to the "camera" activity has been sent out by our app
        intended(toPackage("com.android.camera2"));

        // ... additional test steps and validation ...
    }
}
Run Code Online (Sandbox Code Playgroud)


Yen*_*chi 2

如果您仍然有需要,您可以使用新的 Espresso-Intent 来模拟可用于测试此流程的活动结果。 请参阅 Android 测试中的示例

  • 只是更正,正确的链接现在是 https://github.com/googlesamples/android-testing/tree/master/ui/espresso/IntentsBasicSample (2认同)