|
| 1 | +--- |
| 2 | +id: espresso-env-variables-settings |
| 3 | +title: Espresso Environment Variable Settings |
| 4 | +sidebar_label: Environment Variable Settings |
| 5 | +description: Now you can run your Espresso framework on LambdaTest and this particular feature allows users to pass and retrieve environment variables (like STAGE, PROD, or DEV) during automated Android tests. |
| 6 | +keywords: |
| 7 | + - espresso |
| 8 | + - environment |
| 9 | + - lambdatest java |
| 10 | + - stage |
| 11 | + - production |
| 12 | + - testng |
| 13 | + - app testing |
| 14 | + - real devices |
| 15 | +image: /assets/images/og-images/appium-testing-og-image.jpg |
| 16 | +url: https://www.lambdatest.com/support/docs/espresso-env-variables-settings/ |
| 17 | +site_name: LambdaTest |
| 18 | +slug: espresso-env-variables-settings/ |
| 19 | +--- |
| 20 | + |
| 21 | +import CodeBlock from '@theme/CodeBlock'; |
| 22 | +import {YOUR_LAMBDATEST_USERNAME, YOUR_LAMBDATEST_ACCESS_KEY} from "@site/src/component/keys"; |
| 23 | + |
| 24 | +import Tabs from '@theme/Tabs'; |
| 25 | +import TabItem from '@theme/TabItem'; |
| 26 | + |
| 27 | +<script type="application/ld+json" |
| 28 | + dangerouslySetInnerHTML={{ __html: JSON.stringify({ |
| 29 | + "@context": "https://schema.org", |
| 30 | + "@type": "BreadcrumbList", |
| 31 | + "itemListElement": [{ |
| 32 | + "@type": "ListItem", |
| 33 | + "position": 1, |
| 34 | + "name": "Home", |
| 35 | + "item": "https://www.lambdatest.com" |
| 36 | + },{ |
| 37 | + "@type": "ListItem", |
| 38 | + "position": 2, |
| 39 | + "name": "Support", |
| 40 | + "item": "https://www.lambdatest.com/support/docs/" |
| 41 | + },{ |
| 42 | + "@type": "ListItem", |
| 43 | + "position": 3, |
| 44 | + "name": "Environment Variable Settings", |
| 45 | + "item": "https://www.lambdatest.com/support/docs/espresso-env-variables-settings/" |
| 46 | + }] |
| 47 | + }) |
| 48 | + }} |
| 49 | +></script> |
| 50 | + |
| 51 | +Espresso is a powerful UI testing framework for Android that allows you to write automated UI tests for Android apps. When testing your Android apps on LambdaTest, you may need to pass environment variables for various configurations (e.g., STAGE, PROD, DEV) to manage different environments seamlessly. |
| 52 | + |
| 53 | +This guide will walk you through setting up, accessing, and using environment variables for Espresso tests on LambdaTest. You'll learn how to define and retrieve these variables using different configuration formats (JSON, YAML), integrate them with LambdaTest, and run your tests smoothly. |
| 54 | + |
| 55 | +## Prerequisites |
| 56 | + |
| 57 | +- Your LambdaTest [Username and Access key](https://accounts.lambdatest.com/security). |
| 58 | +- Access to an **Android** app (.apk) and an **Espresso Test** app (.apk file). |
| 59 | + |
| 60 | +:::tip |
| 61 | + |
| 62 | +If you do not have any **Android** app (.apk) and an **Espresso Test** app (.apk) file, you can run your sample tests on LambdaTest by using our sample :link: [Android app](https://prod-mobile-artefacts.lambdatest.com/assets/docs/proverbial_android.apk) and a sample :link: [Espresso Test](https://prod-mobile-artefacts.lambdatest.com/assets/docs/proverbial_android_expressotest.apk). |
| 63 | + |
| 64 | +::: |
| 65 | + |
| 66 | +## Step 1: Setup Environment Variable |
| 67 | + |
| 68 | +Now, you need to export your environment variables *LT_USERNAME* and *LT_ACCESS_KEY* that are available in the [LambdaTest Profile page](https://accounts.lambdatest.com/detail/profile). |
| 69 | + |
| 70 | +Run the below mentioned commands in your terminal to setup the CLI and the environment variables. |
| 71 | + |
| 72 | +<Tabs className="docs__val"> |
| 73 | + |
| 74 | +<TabItem value="bash" label="Linux / MacOS" default> |
| 75 | + |
| 76 | + <div className="lambdatest__codeblock"> |
| 77 | + <CodeBlock className="language-bash"> |
| 78 | + {`export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" |
| 79 | +export LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`} |
| 80 | + </CodeBlock> |
| 81 | +</div> |
| 82 | + |
| 83 | +</TabItem> |
| 84 | + |
| 85 | +<TabItem value="powershell" label="Windows" default> |
| 86 | + |
| 87 | + <div className="lambdatest__codeblock"> |
| 88 | + <CodeBlock className="language-powershell"> |
| 89 | + {`set LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}" |
| 90 | +set LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`} |
| 91 | + </CodeBlock> |
| 92 | +</div> |
| 93 | + |
| 94 | +</TabItem> |
| 95 | +</Tabs> |
| 96 | + |
| 97 | +## Step 2: Upload your Application and Test Suite |
| 98 | + |
| 99 | +### Upload your App |
| 100 | + |
| 101 | +Upload your **android** application (.apk file) to the LambdaTest servers using our **REST API**. You need to provide your **Username** and **AccessKey** in the format ```Username:AccessKey``` in the **cURL** command for authentication. Make sure to add the path of the **appFile** in the cURL request. |
| 102 | + |
| 103 | +Here is an example cURL request to upload your app using our REST API: |
| 104 | + |
| 105 | +<Tabs className="docs__val"> |
| 106 | + |
| 107 | +<TabItem value="bash" label="Linux / MacOS" default> |
| 108 | + |
| 109 | + <div className="lambdatest__codeblock"> |
| 110 | + <CodeBlock className="language-bash"> |
| 111 | + {`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" \\ |
| 112 | +--location --request POST 'https://manual-api.lambdatest.com/app/uploadFramework' \\ |
| 113 | +--form 'appFile=@"/Users/macuser/Downloads/proverbial.ipa"' \\ |
| 114 | +--form 'type="espresso-android"'`} |
| 115 | + </CodeBlock> |
| 116 | +</div> |
| 117 | + |
| 118 | +</TabItem> |
| 119 | + |
| 120 | +<TabItem value="powershell" label="Windows" default> |
| 121 | + |
| 122 | + <div className="lambdatest__codeblock"> |
| 123 | + <CodeBlock className="language-powershell"> |
| 124 | +{`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" -X POST "https://manual-api.lambdatest.com/app/uploadFramework" -F "appFile=@"/Users/macuser/Downloads/proverbial.ipa"" -F "type="espresso-android""`} |
| 125 | + </CodeBlock> |
| 126 | +</div> |
| 127 | + |
| 128 | +</TabItem> |
| 129 | +</Tabs> |
| 130 | + |
| 131 | +:::note |
| 132 | +Response of above cURL will be a **JSON** object containing the `App URL` of the format - `APP123456789123456789` and will be used in the last step as `appId`. |
| 133 | +::: |
| 134 | + |
| 135 | +### Upload your Test Suite |
| 136 | + |
| 137 | +Upload your **test suite** (.apk file) to the LambdaTest servers using our **REST API**. You need to provide your **Username** and **AccessKey** in the format `Username:AccessKey` in the **cURL** command for authentication. Make sure to add the path of the **appFile** in the cURL request. Here is an example cURL request to upload your app using our REST API: |
| 138 | + |
| 139 | +<Tabs className="docs__val"> |
| 140 | + |
| 141 | +<TabItem value="bash" label="Linux / MacOS" default> |
| 142 | + |
| 143 | + <div className="lambdatest__codeblock"> |
| 144 | + <CodeBlock className="language-bash"> |
| 145 | + {`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" \\ |
| 146 | +--location --request POST 'https://manual-api.lambdatest.com/app/uploadFramework' \ |
| 147 | +--form 'appFile=@"/Users/macuser/Downloads/proverbial_ios_xcuitest.ipa"' \ |
| 148 | +--form 'type="espresso-android"'`} |
| 149 | + </CodeBlock> |
| 150 | +</div> |
| 151 | + |
| 152 | +</TabItem> |
| 153 | + |
| 154 | +<TabItem value="powershell" label="Windows" default> |
| 155 | + |
| 156 | + <div className="lambdatest__codeblock"> |
| 157 | + <CodeBlock className="language-powershell"> |
| 158 | +{`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" --location --request POST "https://manual-api.lambdatest.com/app/uploadFramework" --form "appFile=@"C:/Users/varunkumarb/Downloads/proverbial_ios_xcuitest.ipa"" --form "type=\"espresso-android\""`} |
| 159 | + </CodeBlock> |
| 160 | +</div> |
| 161 | + |
| 162 | +</TabItem> |
| 163 | +</Tabs> |
| 164 | + |
| 165 | +:::note |
| 166 | + |
| 167 | +Response of above cURL will be a **JSON** object containing the `App URL` of the format - `APP123456789123456789` and will be used in the next step as ``testSuiteAppId`` |
| 168 | + |
| 169 | +::: |
| 170 | + |
| 171 | +## Step 3: Configure YAML in your Test Suite |
| 172 | +Enter your `<RELATIVE_APP_PATH>` and `<RELATIVE_TEST_SUITE_PATH>` in the YAML file |
| 173 | + |
| 174 | +```yaml |
| 175 | +--- |
| 176 | +version: "0.2" |
| 177 | +concurrency: 4 |
| 178 | +runson: android |
| 179 | +autosplit: true |
| 180 | +maxRetries: 2 |
| 181 | +# highlight-start |
| 182 | +env: |
| 183 | + STAGE: stg1 |
| 184 | + PROD: prod1 |
| 185 | + DEV: dev |
| 186 | +# highlight-end |
| 187 | +framework: |
| 188 | + name: "android/espresso" |
| 189 | + args: |
| 190 | + reservation: false |
| 191 | + buildName: "Test Espresso Sharding" |
| 192 | + appId: <TARGET_SUITE> |
| 193 | + testSuiteAppId: <TEST_SUITE> |
| 194 | + deviceSelectionStrategy: any |
| 195 | + devices: [".*"] |
| 196 | + shards: |
| 197 | + mappings: |
| 198 | + - name: shard1 |
| 199 | + strategy: "only-testing/skip-testing" |
| 200 | + values: ["<className>/<className/testName>"] |
| 201 | + - name: shard2 |
| 202 | + strategy: "only-testing/skip-testing" |
| 203 | + values: ["<className>/<className/testName>", "<className>/<className/testName>"] |
| 204 | +``` |
| 205 | +
|
| 206 | +## Step 4: Generate Reports and Artifacts |
| 207 | +To generate artifacts for your XCUI tests, add the `artifacts: true` and `report: true` flag in your YAML file: |
| 208 | + |
| 209 | +```yaml |
| 210 | +report: true |
| 211 | +
|
| 212 | +framework: |
| 213 | + name: "android/espresso" |
| 214 | + args: |
| 215 | + artifacts: true |
| 216 | + ...// |
| 217 | +``` |
| 218 | + |
| 219 | +To download these artifacts in your local machine, you can pass the `--download-artifacts` and `--download-report` flag with the CLI command to execute the tests as shown in the next step. |
| 220 | + |
| 221 | +## Step 5: Execute your Test Suite |
| 222 | + |
| 223 | +> **NOTE :** In case of macOS, if you get a permission denied warning while executing CLI, simply run **`chmod u+x ./hyperexecute`** to allow permission. In case you get a security popup, allow it from your **System Preferences** → **Security & Privacy** → **General tab**. |
| 224 | + |
| 225 | +Run the below command in your terminal at the root folder of the project: |
| 226 | + |
| 227 | +```bash |
| 228 | +./hyperexecute --config RELATIVE_PATH_OF_YOUR_YAML_FILE --download-artifacts --download-report |
| 229 | +``` |
| 230 | + |
| 231 | +OR use this command if you have not exported your username and access key in the step 2. |
| 232 | + |
| 233 | +<div className="lambdatest__codeblock"> |
| 234 | + <CodeBlock className="language-bash"> |
| 235 | + {`./hyperexecute --user ${ YOUR_LAMBDATEST_USERNAME()} --key ${ YOUR_LAMBDATEST_ACCESS_KEY()} --config RELATIVE_PATH_OF_YOUR_YAML_FILE --download-artifacts --download-report`} |
| 236 | + </CodeBlock> |
| 237 | +</div> |
| 238 | + |
| 239 | +## Step 6: Monitor the Test Execution |
| 240 | + |
| 241 | +Visit the dashboard and check your job status. |
| 242 | + |
| 243 | +<img loading="lazy" src={require('../assets/images/espresso/environment-variable-success.png').default} alt="automation-dashboard" width="1920" height="868" className="doc_img"/> |
| 244 | + |
| 245 | +## Benefits of Using Environment Variables in Espresso |
| 246 | + |
| 247 | +### Environment Flexibility: |
| 248 | +Easily switch between different configurations (STAGE, PROD, DEV) without changing the test code. |
| 249 | + |
| 250 | +### Reusability: |
| 251 | +Use the same test script for different environments by only changing the environment variables. |
| 252 | + |
| 253 | +### Cleaner Configurations: |
| 254 | +Manage and switch environment settings via Postman or YAML without modifying your test code. |
| 255 | + |
| 256 | +### Cross-Platform Compatibility: |
| 257 | +LambdaTest allows you to run your Espresso tests on multiple Android devices in parallel, ensuring broader coverage. |
| 258 | + |
| 259 | +## Troubleshooting Tips |
| 260 | +### Problem: Environment Variables Not Loading |
| 261 | +Ensure that the environment variables are correctly defined in Postman or YAML and properly linked to LambdaTest. |
| 262 | +Double-check that the correct environment variable names are being used in your Espresso test code. |
| 263 | + |
| 264 | +### Problem: Environment Variable Not Found |
| 265 | +Verify that the environment variable name in the LambdaTest capability and the test code match exactly. |
| 266 | +Check the syntax in the Postman or YAML file for any typos or missing parameters. |
| 267 | + |
| 268 | +### Problem: Test Fails on LambdaTest |
| 269 | +Make sure the Appium server or Espresso test is correctly set up on LambdaTest. |
| 270 | +Ensure that the environment variable values are being passed and accessed properly in the code. |
| 271 | + |
| 272 | + |
0 commit comments