The Sauce Labs Cookbook

Sauce Headless

Front End Performance Testing


External Resources

More Info

Page tree
Skip to end of metadata
Go to start of metadata

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. 

System Requirements

See the topics under Mobile Application Testing for RDC system requirements. 

Key Specs


Not Supported

  • 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

  1. Launch a test session by logging into Sauce Labs and going to Live > Mobile App

  2. Hover over the app you intend to test and click the Settings icon.
  3. 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.
  4. Hover over the app again and click the Choose Device icon to bring up the menu of available devices.

  5. Hover over the device of your choice and click Launch to begin a test session on that device with your app.

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

  7. Click Choose Image to stage your image for the camera. 

  8. Activate the camera inside of your app. 

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

  1. First, you'll need add the camera instrumentation desired capability command,  sauceLabsImageInjectionEnabled, to your test script. This capability enables image injection functionality. example
    exports.config = {
      capabilities: [
          deviceName: 'Samsung Galaxy S10',
          platformName: 'Android',
          platformVersion: '10',
          automationName: 'UiAutomator2',
          // Enable image-injection on RDC
          sauceLabsImageInjectionEnabled: true,
    Java example
    var desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability("deviceName", "Samsung Galaxy S10");
    desiredCapabilities.setCapability("platformVersionName", "10");
    // Enable image-injection on RDC
    desiredCapabilities.setCapability("sauceLabsImageInjectionEnabled", true);
  2. 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. example
    const {readFileSync} = require('fs');
    const {join} = require('path');
    // Read the file from your project and transform it to a base64 string
    const qrCodeImage = readFileSync(join(process.cwd(), 'assets/qr-code.png'), 'base64');
    // Provide the transformed image to the device
    Java example
    import java.util.Base64;
    import static;
    // Read the file from the classpath and transform it to a base64 string
    String qrCodeImage = Base64.getEncoder().encodeToString(
    // Provide the transformed image to the device
    ((JavascriptExecutor)driver).executeScript("sauce:inject-image=" + qrCodeImage);
  3. 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

Additional Resources

For support in beta, please reach out to your CSM/SE or Sauce Labs Support.

  • No labels