🌍 Using Environment Variables and Datasets in API Requests in E2E
Environment variables in E2E test automation (E2E) make it easy to manage dynamic values like base URLs, tokens, or session IDs across different environments. By using environment variables in your API requests, you can streamline your configurations and avoid hardcoding values into each request. E2E’s variable reference system simplifies switching between environments (Development, QA, Staging, Production) without manually updating each request.
Additionally, Datasets in E2E let you manage multiple sets of values for the same environment variables. Instead of creating new variables for each scenario, you can simply switch datasets to test different configurations, such as valid vs. invalid tokens, staging vs. production users, or multiple API versions.
This guide explains how to use environment variables and datasets together in your API requests and workflows.
🔑 How to Reference Environment Variables
In E2E test automation (E2E), you can reference environment variables using the {\{variableName\}} notation. E2E automatically substitutes the variable with its value from the active environment and selected dataset when you send a request.
Here’s how to use environment variables and datasets in different parts of your API requests:
1️⃣ In the URL
You can use environment variables and datasets directly in the URL of your API requests. This allows you to dynamically change parts of the URL based on the selected environment and dataset.
Example:
If your environment and dataset define:
base_url = https://dev.api.myshop.comYou can reference base_url in the URL as follows:
{\{base_url\}}/users?id=1E2E will resolve this to:
https://dev.api.myshop.com/users?id=1If you switch to the Production Dataset:
base_url = https://api.myshop.comThe same request becomes:
https://api.myshop.com/users?id=12️⃣ In Headers
Environment variables and datasets can be used in headers for authentication tokens or custom header values.
Example:
If your dataset defines:
access_token = Bearer dev_token_87963You can reference it in the Authorization header like this:
Authorization: {\{access_token\}}Switch to a LoadTest Dataset with:
access_token = Bearer loadtest_token_22314The request automatically updates to:
Authorization: Bearer loadtest_token_223143️⃣ In Request Body
Datasets are also useful for sending different request bodies without manually changing values.
Example:
If your dataset defines:
session_cookie = session_dev_125You can reference it in the request body as follows:
{
"session_id": "{\{session_cookie\}}"
}Switch to a Staging Dataset with:
session_cookie = session_staging_490The body automatically updates to:
{
"session_id": "session_staging_490"
}📝 How to Use Environment Variables and Datasets in API Requests
- In URL: Use
{\{variableName\}}in the URL of the API request. E2E substitutes the correct value from the selected environment and dataset. - In Headers: Use
{\{variableName\}}in headers like Authorization, Content-Type, etc. Dataset switching changes these values automatically for different test scenarios. - In Body: Use
{\{variableName\}}in the request body for dynamic data testing. Switching datasets lets you test positive, negative, and regression scenarios without editing requests manually.
🖱️ Auto-Suggest for Environment Variables and Datasets
E2E makes it easy to reference variables and datasets.
- When typing
{{, E2E shows all available environment variables for the selected dataset. - Simply choose the correct variable, and E2E inserts it into your request.
- This reduces errors and speeds up test creation.
📊 Example Workflow with Datasets
Let’s say you have the following environment variables:
base_url = https://dev.api.myshop.com
access_token = Bearer dev_token_87963
session_cookie = session_dev_125And two datasets:
Your API request:
- URL:
{\{base_url\}}/booking - Header:
Authorization: {\{access_token\}} - Body:
{ "session_id": "{\{session_cookie\}}" }
With Default Dataset selected, E2E resolves to:
- URL →
https://dev.api.myshop.com/booking - Header →
Authorization: Bearer dev_token_87963 - Body →
{ "session_id": "session_dev_125" }
Switch to Staging Dataset, and E2E resolves to:
- URL →
https://staging.api.myshop.com/booking - Header →
Authorization: Bearer staging_token_99123 - Body →
{ "session_id": "session_staging_490" }
No manual edits needed—just switch the dataset.
💡 Benefits of Using Datasets
- Multiple Data Scenarios: Run tests for Dev, Staging, and Production quickly.
- Regression Testing: Test older and newer API versions without rewriting requests.
- Negative Testing: Use datasets with invalid tokens or missing fields to test error handling.
- Load Testing: Switch to datasets with hundreds of test users to simulate heavy traffic.
- A/B Testing: Toggle feature flags across datasets to compare API behaviors.
- Regional Testing: Use different datasets for EU, US, and Asia endpoints easily.
Using environment variables with datasets in E2E test automation (E2E) provides a powerful, data-driven approach to API testing:
- Environment Variables manage dynamic values across multiple environments.
- Datasets let you run tests with different configurations without editing requests manually.
This approach makes testing faster, reduces errors, and allows seamless switching between environments and data scenarios for maximum flexibility. 🚀