Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 516
Jango Avatar asked Jan 19 '15 07:01

Jango


People also ask

What is Espresso UI Test?

Espresso is a UI test framework (part of the Android Testing Support Library) that allows you to create automated UI tests for your Android app.

What is espresso testing used for?

Espresso tests state expectations, interactions, and assertions clearly without the distraction of boilerplate content, custom infrastructure, or messy implementation details getting in the way. Espresso tests run optimally fast!

Is Espresso unit testing?

Espresso is an open source android user interface (UI) testing framework developed by Google.


2 Answers

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 ...
    }
}
like image 186
Tarkik Mittal Avatar answered Sep 21 '22 17:09

Tarkik Mittal


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

like image 45
Yenchi Avatar answered Sep 17 '22 17:09

Yenchi