Camera Operation UI testing with Espresso

espresso test animation
espresso start activity with intent
espresso-intents
android espresso example
espresso test multiple activities
espresso ui test dependencies
espresso test reports
android ui testing

I need to automate my UI testing for following operation with espresso test project.

Operation:

Click a button that opens my phone camera. Capture Image, and save the image in sdcard storage. Also update a small image view on the screen when done.

App works fine but with all other operations and similar type of above operation it becomes a time consuming process to test it manually again and again.

I was working with similar problem and found best available solution at below link Camera UI test

// 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 ...
    }
}

UI Testing with Espresso, Espresso is a UI test framework (part of the Android Testing Support Library) that allows sure to load the AdapterView item before performing any operations on it. When we think about scenarios where an app will launch the camera (i.e.  Espresso is targeted at developers, who believe that automated testing is an integral part of the development lifecycle. While it can be used for black-box testing, Espresso’s full power is unlocked by those who are familiar with the codebase under test.

If you still have the need, you can use the new Espresso-Intent that mocks the activity result that you can use to test this flow. See the sample from Android Testing

Testing Camera and Gallery Intents with Espresso, Testing Camera and Gallery Intents with Espresso from showing up and blocking the App's Ui, and according to official documentation:. Espresso is a simple, efficient and flexible testing framework. This tutorial walks you through the basics of Espresso framework, how to setup Espresso framework in a project, work flow of the framework and finding, automating & asserting user interface components in the testing environment with simple android application.

Google has provided an example on this camera problem, which they show how to stub an camera intent, launch camera, and test if image is get from stub intent, and display on imageView.

@Rule
public IntentsTestRule<ImageViewerActivity> mIntentsRule = new IntentsTestRule<>(
     ImageViewerActivity.class);

@Before
public void stubCameraIntent() {
    ActivityResult result = createImageCaptureActivityResultStub();

    // Stub the Intent.
    intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
}

@Test
public void takePhoto_drawableIsApplied() {
    // 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 createImageCaptureActivityResultStub() {
    // 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);
}

Check this link for more detail IntentsAdvancedsample

Camera Operation UI testing with Espresso, I need to automate my UI testing for following operation with espresso test project​. Operation: Click a button that opens my phone camera. Capture Image, and  The Espresso testing framework, provided by AndroidX Test, provides APIs for writing UI tests to simulate user interactions within a single target app. Espresso tests can run on devices running Android 2.3.3 (API level 10) and higher. A key benefit of using Espresso is that it provides automatic synchronization of test actions with the UI of

Test UI for a single app, Before building your UI test with Espresso, make sure to set a this case, follow the instructions in Locating a view in an AdapterView instead. The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app. Espresso Test Recorder then takes the saved recording and automatically generates a corresponding UI test that you can run to test your app.

Create UI tests with Espresso Test Recorder, Follow the "Set Up Espresso" instructions on the Testing UI for a Single App page​, but note that you do not need to manually set a dependency  espresso testing framework development into the library. Latest development (Android 9.0, API level 28 or higher) of espresso testing framework will be done in AndroidX library. Including espresso testing framework in a project is as simple as setting the espresso testing framework as a dependency in the application gradle file, app/build

Android user interface testing with Espresso - Tutorial, Android user interface testing with Espresso - Tutorial. Lars Vogel Android Espresso tutorial including instructions how to test RecyclerView. Learn about UI testing your Android app with Espresso. This tutorial covers the basics, AdapterViews, RecyclerViews, and custom Matchers and Failure Handlers.

Comments