|
| 1 | +--- |
| 2 | +id: camera-image-injection-vd |
| 3 | +title: Camera Image Injection Testing on Virtual Devices |
| 4 | +sidebar_label: Camera Image Injection (Virtual Devices) |
| 5 | +description: Learn how to automate camera injection testing on a virtual device cloud using LambdaTest. |
| 6 | +keywords: |
| 7 | + - test image capture on virtual devices |
| 8 | + - qr code scanner testing |
| 9 | + - barcode scanning on virtual device cloud |
| 10 | + - lambdaTest virtual device testing |
| 11 | + - mobile app testing qr code |
| 12 | + - lambdatest capture code |
| 13 | + - simulate camera features online |
| 14 | +url: https://www.lambdatest.com/support/docs/camera-image-injection-on-virtual-devices/ |
| 15 | +site_name: LambdaTest |
| 16 | +slug: camera-image-injection-on-virtual-devices/ |
| 17 | +--- |
| 18 | + |
| 19 | +import CodeBlock from '@theme/CodeBlock'; |
| 20 | +import {YOUR_LAMBDATEST_USERNAME, YOUR_LAMBDATEST_ACCESS_KEY} from "@site/src/component/keys"; |
| 21 | + |
| 22 | +import Tabs from '@theme/Tabs'; |
| 23 | +import TabItem from '@theme/TabItem'; |
| 24 | + |
| 25 | +<script type="application/ld+json" |
| 26 | + dangerouslySetInnerHTML={{ __html: JSON.stringify({ |
| 27 | + "@context": "https://schema.org", |
| 28 | + "@type": "BreadcrumbList", |
| 29 | + "itemListElement": [{ |
| 30 | + "@type": "ListItem", |
| 31 | + "position": 1, |
| 32 | + "name": "Home", |
| 33 | + "item": "https://www.lambdatest.com" |
| 34 | + },{ |
| 35 | + "@type": "ListItem", |
| 36 | + "position": 2, |
| 37 | + "name": "Support", |
| 38 | + "item": "https://www.lambdatest.com/support/docs/" |
| 39 | + },{ |
| 40 | + "@type": "ListItem", |
| 41 | + "position": 3, |
| 42 | + "name": "Camera Image Injection Testing on Virtual Devices", |
| 43 | + "item": "https://www.lambdatest.com/support/docs/camera-image-injection-on-virtual-devices/" |
| 44 | + }] |
| 45 | + }) |
| 46 | + }} |
| 47 | +></script> |
| 48 | +--- |
| 49 | +The camera image injection feature on LambdaTest lets you test key camera-based functionalities such as image capture, QR code scanning, and barcode scanning across a wide range of Android emulators using Appium framework. |
| 50 | + |
| 51 | +## How It Works? |
| 52 | +--- |
| 53 | + |
| 54 | +LambdaTest uses sensor instrumentation to integrate with native mobile sensors. When the image injection feature is enabled during a test session, LambdaTest injects a custom camera module into the mobile app using this sensor instrumentation. This module simulates the behavior of the camera by overriding the Android SDK, allowing you to validate how your mobile app handles camera input. |
| 55 | + |
| 56 | +### Key Features |
| 57 | +--- |
| 58 | + |
| 59 | +- Works across various Android emulators. |
| 60 | +- Enables testing of image capture, barcode, and QR code scanning. |
| 61 | +- Integrates smoothly into standard app testing workflows. |
| 62 | + |
| 63 | +## Supported Camera APIs |
| 64 | +--- |
| 65 | + |
| 66 | +Below given is the list of camera APIs which are supported for image injection feature. Please ensure that your mobile app uses these APIs only to enable image injection. |
| 67 | + |
| 68 | +<Tabs className="docs__val"> |
| 69 | + |
| 70 | +<TabItem value="powershell" label="Android >= version 9" default> |
| 71 | + |
| 72 | + <div className="lambdatest__codeblock"> |
| 73 | + | Supported APIs | |
| 74 | + |----------------| |
| 75 | + | `CameraX` API is used. <br />📕 For details, please check [CameraX documentation](https://developer.android.com/training/camerax). | |
| 76 | + | `Camera` API is used. <br />📕 For details, please check [Camera documentation](https://developer.android.com/training/camera-deprecated).| |
| 77 | + | `Camera2` API is used. <br />📕 For details, please check [Camera2 documentation](https://developer.android.com/training/camera2).| |
| 78 | + | `ACTION_IMAGE_CAPTURE` is standard intent action that can be sent to have the camera application capture an image and return it. <br /><br />📕 For details, please check [MediaStore documentation](https://developer.android.com/reference/android/provider/MediaStore).| |
| 79 | + </div> |
| 80 | + |
| 81 | +</TabItem> |
| 82 | +</Tabs> |
| 83 | + |
| 84 | +## Using Camera Image Injection for Android |
| 85 | +--- |
| 86 | + |
| 87 | +Follow the below steps to automate camera injection testing on Android emulators with Appium: |
| 88 | + |
| 89 | +### Step 1: Upload an Image |
| 90 | +--- |
| 91 | +Use the following cURL command to upload an image of your choice to the LambdaTest cloud. Only JPG, JPEG, and PNG formats are supported, with a maximum file size of 10 MB. |
| 92 | + |
| 93 | +<div className="lambdatest__codeblock"> |
| 94 | +<CodeBlock className="language-bash"> |
| 95 | +{`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" -X POST "https://mobile-mgm.lambdatest.com/mfs/v1.0/media/upload" -F "media_file=@"/Users/macuser/Downloads/sampleimage.jpg"" -F "type="image"" -F "custom_id="SampleImage""`} |
| 96 | +</CodeBlock> |
| 97 | +</div> |
| 98 | + |
| 99 | +The response to the above cURL command will be a JSON object containing a `media_url` in the format `lt://MEDIA123456789123456789`, as shown below. |
| 100 | + |
| 101 | +```js |
| 102 | +{ |
| 103 | +"media_url":"lt://MEDIA123456789123456789", |
| 104 | +"name":"sampleimage.jpg", |
| 105 | +"status":"success", |
| 106 | +"custom_id":"SampleImage" |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Step 2: Enable Image injection |
| 111 | +--- |
| 112 | +You can enable image injection in your mobile app using `appium` capability. Simply add `"enableImageInjection": true` capability in your automation script. Enabling this capability will trigger the sensor instrumentation in your mobile app. Now, add the media URL `"media": "lt://MEDIA123456789123456789"` capability. However, this capability is now optional and can instead be provided via LambdaHook, as described in Step 3. |
| 113 | + |
| 114 | +<Tabs className="docs__val"> |
| 115 | + |
| 116 | +<TabItem value="python" label="Python" default> |
| 117 | + <div className="lambdatest__codeblock"> |
| 118 | + <CodeBlock className="language-python"> |
| 119 | + {`desired_capabilities = { |
| 120 | + "enableImageInjection": true, |
| 121 | + "media": "lt://MEDIAf446d4170cd946aa9ec307d10cb679b9", #Add media_url here (optional) |
| 122 | + "isRealMobile"=false, |
| 123 | + }`} |
| 124 | + </CodeBlock> |
| 125 | + </div> |
| 126 | +</TabItem> |
| 127 | + |
| 128 | + |
| 129 | +<TabItem value="JavaScript" label="JavaScript" default> |
| 130 | + <div className="lambdatest__codeblock"> |
| 131 | + <CodeBlock className="language-javascript"> |
| 132 | + {`DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); |
| 133 | +desiredCapabilities.setCapability("enableImageInjection", "true"); |
| 134 | +desiredCapabilities.setCapability("isRealMobile", false);`} |
| 135 | + </CodeBlock> |
| 136 | + </div> |
| 137 | +</TabItem> |
| 138 | +</Tabs> |
| 139 | + |
| 140 | +### Step 3: Inject an Image |
| 141 | +--- |
| 142 | +Refer to the code snippets below to inject an image. Ensure the image is injected before your mobile app activates the camera, as the camera will only capture the most recently injected image. |
| 143 | + |
| 144 | + |
| 145 | +<Tabs className="docs__val"> |
| 146 | + |
| 147 | +<TabItem value="python" label="Python" default> |
| 148 | + <div className="lambdatest__codeblock"> |
| 149 | + <CodeBlock className="language-python"> |
| 150 | + {`driver.execute_script("lambda-image-injection=media_url") #Add media_url here`} |
| 151 | + </CodeBlock> |
| 152 | + </div> |
| 153 | +</TabItem> |
| 154 | + |
| 155 | + |
| 156 | +<TabItem value="JavaScript" label="JavaScript" default> |
| 157 | + <div className="lambdatest__codeblock"> |
| 158 | + <CodeBlock className="language-javascript"> |
| 159 | + {`driver.execute_script("lambda-image-injection=media_url") //Add media_url here`} |
| 160 | + </CodeBlock> |
| 161 | + </div> |
| 162 | +</TabItem> |
| 163 | +</Tabs> |
| 164 | + |
| 165 | +:::info |
| 166 | + |
| 167 | +**Use Case**: This feature allows you to inject multiple images at different points within a single test. Each new image injected using this process will replace the image injected in the previous step. |
| 168 | + |
| 169 | +::: |
| 170 | + |
| 171 | +### Step 4: Capture the Desired Image |
| 172 | +--- |
| 173 | +First, add the logic to inject the uploaded image in your test script. Then, add the logic to tap the camera button in the mobile app. Make sure to inject the image before clicking the camera, as only the most recently injected image will appear on the app screen. |
| 174 | + |
| 175 | + |
| 176 | +> 📕 Check the documentation to [automate camera image injection testing on real devices](/support/docs/camera-image-injection/). |
| 177 | +
|
| 178 | + |
| 179 | +<nav aria-label="breadcrumbs"> |
| 180 | + <ul className="breadcrumbs"> |
| 181 | + <li className="breadcrumbs__item"> |
| 182 | + <a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com"> |
| 183 | + Home |
| 184 | + </a> |
| 185 | + </li> |
| 186 | + <li className="breadcrumbs__item"> |
| 187 | + <a className="breadcrumbs__link" target="_self" href="https://www.lambdatest.com/support/docs/"> |
| 188 | + Support |
| 189 | + </a> |
| 190 | + </li> |
| 191 | + <li className="breadcrumbs__item breadcrumbs__item--active"> |
| 192 | + <span className="breadcrumbs__link"> |
| 193 | + Camera Image Injection |
| 194 | + </span> |
| 195 | + </li> |
| 196 | + </ul> |
| 197 | +</nav> |
0 commit comments