Skip to content

Commit 96d8978

Browse files
espresso env variable settings
1 parent 3c9f099 commit 96d8978

File tree

2 files changed

+70
-136
lines changed

2 files changed

+70
-136
lines changed
Lines changed: 60 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
id: espresso-env-variables-settings
3-
title: Espresso Environment Variable Settings
3+
title: Setting Up Espresso Environment Variables
44
sidebar_label: Environment Variable Settings
55
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.
66
keywords:
@@ -48,35 +48,35 @@ import TabItem from '@theme/TabItem';
4848
}}
4949
></script>
5050

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.
51+
This feature allows you to dynamically set and test environment variables during Espresso test execution on LambdaTest.
5252

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.
53+
## Step 1: Create Variables in Your Test Suite
54+
Define environment variables in your Espresso test suite to fetch the variable values during execution.
5455

55-
## Prerequisites
5656

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).
57+
```bash title="Sample Script"
58+
String envVar = InstrumentationRegistry.getArguments().getString(ENV_VAR);
59+
```
6360

64-
:::
61+
``` bash title="Examples"
62+
String stage = InstrumentationRegistry.getArguments().getString(“STAGE”);
63+
String prod = InstrumentationRegistry.getArguments().getString(“PROD”);
64+
```
6565

66-
## Step 1: Setup Environment Variable
66+
## Step 2: Upload Your Application
6767

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).
68+
To begin testing, upload your Android application (.apk file) to LambdaTest's servers. You'll use our **REST API** for this process.
6969

70-
Run the below mentioned commands in your terminal to setup the CLI and the environment variables.
70+
- **Authentication :** You'll need your LambdaTest Username and AccessKey. Combine them in the format `Username:AccessKey`.
71+
- **Uploading the App :** Use **cURL command** to send a request to our API. The request should include the path to your application file (**appFile**).
7172

7273
<Tabs className="docs__val">
7374

7475
<TabItem value="bash" label="Linux / MacOS" default>
7576

7677
<div className="lambdatest__codeblock">
7778
<CodeBlock className="language-bash">
78-
{`export LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}"
79-
export LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
79+
{`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" --location --request POST 'https://manual-api.lambdatest.com/app/uploadFramework' --form 'appFile=@"<PATH_OF_YOUR_ANDROID_APP>"' --form 'type="espresso-android"'`}
8080
</CodeBlock>
8181
</div>
8282

@@ -86,32 +86,31 @@ export LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
8686

8787
<div className="lambdatest__codeblock">
8888
<CodeBlock className="language-powershell">
89-
{`set LT_USERNAME="${ YOUR_LAMBDATEST_USERNAME()}"
90-
set LT_ACCESS_KEY="${ YOUR_LAMBDATEST_ACCESS_KEY()}"`}
89+
{`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" --location --request POST "https://manual-api.lambdatest.com/app/uploadFramework" --form "appFile=@"<PATH_OF_YOUR_ANDROID_APP>"" --form "type=\"espresso-android\""`}
9190
</CodeBlock>
9291
</div>
9392

9493
</TabItem>
9594
</Tabs>
9695

97-
## Step 2: Upload your Application and Test Suite
96+
:::info
97+
- Provide the path of your android application in the above URL in place of `<PATH_OF_YOUR_ANDROID_APP>`
98+
- Response of above cURL will be a **JSON** object containing the `App URL` of the format - `lt://APP123456789123456789` and will be used in the last step.
99+
:::
98100

99-
### Upload your App
101+
## Step 3: Upload Your Test Suite
100102

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.
103+
Upload your Espresso test suite (.apk) file to LambdaTest servers using our REST API.
102104

103-
Here is an example cURL request to upload your app using our REST API:
105+
The following sample cURL command shows how to upload a test suite:
104106

105107
<Tabs className="docs__val">
106108

107109
<TabItem value="bash" label="Linux / MacOS" default>
108110

109111
<div className="lambdatest__codeblock">
110112
<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"'`}
113+
{`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" --location --request POST 'https://manual-api.lambdatest.com/app/uploadFramework' --form 'appFile=@"<PATH_OF_YOUR_TEST_SUITE_APP>"' --form 'type="espresso-android"'`}
115114
</CodeBlock>
116115
</div>
117116

@@ -121,55 +120,58 @@ Here is an example cURL request to upload your app using our REST API:
121120

122121
<div className="lambdatest__codeblock">
123122
<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""`}
123+
{`curl -u "${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}" --location --request POST "https://manual-api.lambdatest.com/app/uploadFramework" --form "appFile=@"<PATH_OF_YOUR_TEST_SUITE_APP>"" --form "type=\"espresso-android\""`}
125124
</CodeBlock>
126125
</div>
127126

128127
</TabItem>
129128
</Tabs>
130129

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`.
130+
:::info
131+
- Provide the path of your android application in the above URL in place of `<PATH_OF_YOUR_TEST_SUITE_APP>`
132+
- Response of above cURL will be a **JSON** object containing the `App URL` of the format - `lt://APP123456789123456789` and will be used in the last step.
133133
:::
134134

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>
135+
## Step 4: Executing The Test
151136

152-
</TabItem>
137+
- You will need **base64 encoded authentication** in order to execute your Espresso automation test suite. Enter your `username:accesskey` in **[Basic Authentication Header Generator](https://mixedanalytics.com/knowledge-base/api-connector-encode-credentials-to-base-64/)** to generate your auth token.
153138

154-
<TabItem value="powershell" label="Windows" default>
139+
Take note of the base64 encoded authentication which needs to be added in the next step.
155140

156-
<div className="lambdatest__codeblock">
141+
<div className="lambdatest__codeblock">
157142
<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\""`}
143+
{`${ YOUR_LAMBDATEST_USERNAME()}:${ YOUR_LAMBDATEST_ACCESS_KEY()}`}
159144
</CodeBlock>
160145
</div>
161146

162-
</TabItem>
163-
</Tabs>
164-
165-
:::note
147+
- Once you have uploaded your app and test suite, you can execute your test by running the following command:
148+
149+
> Enter your **BASIC_AUTH_TOKEN**, **APP_ID** (generated in the first step) and **TEST_SUITE_ID** (generated in the second step) in the below command.
166150
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-
:::
151+
```bash
152+
curl --location --request POST 'https://mobile-api.lambdatest.com/framework/v1/espresso/build' \
153+
--header 'Authorization: Basic BASIC_AUTH_TOKEN' \
154+
--header 'Content-Type: application/json' \
155+
--data-raw '{
156+
"app" : "APP_ID",
157+
"testSuite": "TEST_SUITE_ID",
158+
"device" : ["Galaxy S21 5G-12"],
159+
"queueTimeout": 10800,
160+
"IdleTimeout": 150,
161+
"deviceLog": true,
162+
"network": false,
163+
"build" : "Proverbial-Espresso"
164+
# highlight-start
165+
"envVariables":{ //setting up environment variables
166+
"STAGE":"stg1",
167+
"PROD":"prod1"
168+
}
169+
# highlight-end
170+
}'
171+
```
170172

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+
## Run your test in HyperExecute
174+
To execute your test suite in HyperExecute, configure your YAML file by specifying the `<RELATIVE_APP_PATH>` and `<RELATIVE_TEST_SUITE_PATH>`.
173175

174176
```yaml
175177
---
@@ -203,70 +205,7 @@ framework:
203205
values: ["<className>/<className/testName>", "<className>/<className/testName>"]
204206
```
205207
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.
263208
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.
267209
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.
271210
272211

sidebars.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,22 +2595,17 @@ module.exports = {
25952595
id: "getting-started-with-espresso-testing",
25962596
},
25972597
{
2598-
type: "doc",
2598+
type: "category",
2599+
collapsed: true,
25992600
label: "Supported Capabilities",
2600-
id: "espresso-supported-capabilities",
2601-
},
2602-
// {
2603-
// type: "category",
2604-
// collapsed: true,
2605-
// label: "Supported Capabilities",
2606-
// link: {
2607-
// type: "doc",
2608-
// id: "espresso-supported-capabilities",
2609-
// },
2610-
// items: [
2611-
// "espresso-env-variables-settings",
2612-
// ],
2613-
// },
2601+
link: {
2602+
type: "doc",
2603+
id: "espresso-supported-capabilities",
2604+
},
2605+
items: [
2606+
"espresso-env-variables-settings",
2607+
],
2608+
},
26142609
{
26152610
type: "doc",
26162611
label: "RegEx in Espresso",

0 commit comments

Comments
 (0)