Does your mobile app have the ability to take images and then process or store them within the app (e.g., scanning a bank check, taking a social media selfie)? Camera Image Injection – also known as camera mocking – is a Sauce Labs Real Device Cloud (RDC) feature that simulates taking a picture through your mobile device app, allowing you to test the app’s camera-based functionality and deliver the best possible user experience.
To mimic camera behavior when testing your app, you'll provide the app with an image that mocks the use of the camera. During a Sauce Labs live test, you'll be prompted to upload a photo that will be fed to your app, rather than using your device camera to take the photo. For automated tests, you'll pass an image to the image injection endpoint. Image Injection intercepts the camera input and replaces the camera output with the image file via the camera APIs. When the app requests an image from the device's camera, we inject your uploaded image into the response (the app doesn't know the difference).
See the following sections for more information:
What You'll Need
You'll need to upload your app to Sauce Labs prior to testing. For instructions, see Uploading your Application to Real Devices using the User Interface.
Using Camera Image Injection
Camera Image Injection is a core feature built into our RDC functionality and available for use with public and private devices. Your mobile app accesses the camera and instead of getting back the picture of the device camera, it'll retrieve your uploaded image for your test. You employ the built-in device camera in your live and automated testing and perform test cases that require taking images with any of the device cameras.
See the topics under Mobile Application Testing for RDC system requirements.
All iOS and Android devices available in the RDC
Front-facing and rear-facing system device cameras
- Image file sizes up to 5MB
JPG, JPEG, PNG image file formats.
For Android devices, there are multiple ways to capture an image, as described in the Android Camera API developer documentation. We support the following:
- ACTION_IMAGE_CAPTURE Intent: opens the system camera and notifies the calling app gets when the image is taken
- camera2 API: everything is configured and handled from within the app
- Camera API (deprecated) (partially supported): As with camera2, everything is handled in the app itself. QR Code readers often use Camera#setPreviewCallback. We pass the injected image to this method, but the rest of this deprecated API is not supported. UI Elements will not likely display the injected image.
- For iOS devices, the camera can be configured with different outputs. We support the following:
- AVCapturePhotoOutput: for capturing still images. The results are received via the AVCapturePhotoCaptureDelegate and the method captureOutput:didFinishProcessingPhoto:error: The other methods in this delegate are either deprecated or handle live photos, which we don't support.
- AVCaptureMetadataOutput: for reading QR-Codes. The QR Codes are passed to the app via captureOutput:didOutputMetadataObjects:fromConnection:. We are detecting the AVMetadataMachineReadableCodeObject and QR Codes are part of that.
- Ephemeral apps (i.e., app with temporary messages that disappear after a certain timeframe)
- Testing with Emulators, Simulators
Common Use Cases & Examples
Here are some common use cases ideal for implementing Camera Image Injection in your tests:
Scanning a Check for a Mobile Banking App Deposit
Many mobile banking apps allow customers to deposit checks using their smartphone. The customer takes and uploads an image of their physical paper check, and the image is then submitted to the bank for processing.
Using a QR Code to Link to an Embedded URL
QR codes are often used as a way to bridge print media to digital. Users take a photo with a QR code reader app, the app scans the code and directs them to an embedded URL.
For use cases that involve scanning barcodes or QR codes, your own application in testing must do the actual image processing. Camera Image Injection passes your uploaded image directly to your app as if it came from the device camera; it does not do any processing.
Taking a Selfie for a User Profile Photo
This could be taking a selfie or uploading a picture for apps that require a user profile photo. You can use Camera Image Injection to test image formats and sizes.
Taking an Image to Store or Send via Mobile App
Whether it’s a social media app or photo sharing, this use case can encompass many different scenarios. In its simplest form, it could be taking pictures from the front or back camera to send and/or archive within the app.
Live Testing with Camera Image Injection
Launch a test session by logging into Sauce Labs and going to Live > Mobile App.
- Hover over the app you intend to test and click the Settings icon.
- In the editor that appears, make sure the Image Injection toggle is enabled and then return to the app selection screen.
NOTE: It is important to enable image injection before choosing a device.
Hover over the app again and click the Choose Device icon to bring up the menu of available devices.
Hover over the device of your choice and click Launch to begin a test session on that device with your app.
When you want to capture an image with the app camera, go to the right toolbar and click the Camera icon to initiate Camera Image Injection.
Click Choose Image to stage your image for the camera.
Activate the camera inside of your app.
The device will show your uploaded image in the app as if the image was taken by the device camera. The image will continue to be available, should you go back to the camera during your test session, or you can upload another image and capture it with the camera.
Video: Running iOS Mobile Device Tests
This video illustrates how to use both biometric authentication and image injection (starting at 0:41) in an iOS live test:
Video: Running Mobile Device QR Code Tests
When injecting an image with a QR Code or barcode, the image size in your preview may exceed the boundaries of the target scanner area, which would prevent your app from reading the code. In this scenario, you'd need to add padding to your uploaded image so that when it's scaled to full-screen, the QR Code will fit inside the scanning area limits and can be processed.
Automated Appium Testing with Camera Image Injection
In your automated test script, you'll need to input the desired capabilities specific to Camera Image Injection (see below code snippets). The code will execute your image upload and opening of the device camera.
First, you'll need add the camera instrumentation desired capability command,
sauceLabsImageInjectionEnabled, to your test script. This capability enables image injection functionality.
Next, add the image injection method to your test script by providing a file path to your image. When you execute your test, these code snippets will call the endpoint and pass the image to the app for further processing or for other use.
You can change the image by sending the custom command with a different image. Note that your image file path must be converted to base64 encoding.
But you only really start image injection when you put the second code snippet into your code in one or more places to call the end point and pass the image.
Here are some common errors you may see in the course of testing with Camera Image Injection and how to resolve them.
Image injection failed
This error is displayed when you attempt to inject your image before the app fully loads during your initial test session startup. You must wait until your app has fully loaded prior to injecting your image.
Image injection is not enabled for the application
This error is displayed due to one or more of these reasons:
- Enable Image Injection checkbox is not checked; this needs to be checked
- For Android tests, the debuggable flag (
android:debuggable="true") is missing from your application's manifest file
- Image Injection code examples | Sauce Labs on GitHub
- Sample Mobile App; try out image injection for yourself using our demo app
- Android Camera API | Google Developer Documentation
For support in beta, please reach out to your CSM/SE or Sauce Labs Support.