Obtain and Store values using variables
This tutorial will show you how to create a variable using a value from an API response and utilise it in a request in Testfully using scripting. Testfully's scripting feature allows you to use JavaScript (ES2020) to customise requests, automate tasks, and create workflows.
Say we sent a request, authenticated ourselves using an auth method, and got a token in response. We want to extract the token from the response and store it in a variable, so we can use it in subsequent requests.
Let's go through this example and achieve this goal using the scripting feature in five easy to follow steps.
Step 1: Obtain the Token from the API Endpoint
First, send a request to your authentication endpoint to obtain the token.
Using Testfully API client that's as easy as ABC!
Imagine the API endpoint for authentication is: https://api.example.com/auth
You send a request and get a token in the response body.
Step 2: Extract the Token from the Response
In Testfully, you have two options to run your scripts:
- Before: This script runs before the request is sent. It's commonly used to prepare the request by setting headers, parameters, or body data dynamically based on certain conditions.
- After: This script runs after the request has been executed. It's used for processing the response, setting variables based on the response data, or even for test assertions and validations.
If you want to use a token received from an API response, you'd normally use an "After" script to extract the token from the response body and store it as a variable. Then, in subsequent requests, use Testfully's Authorization feature to pass the authorization token or use a "Before" script to inject this token into the request headers for authorization.
The "After" script before demonstrates how to get the response body as a JSON object using the $.response.json()
API and extract the token from the response body.
// Assuming the token is in the response body under the attribute 'token'
let responseBody = $.response.json();
let token = responseBody.token;
Step 3: Choosing the right Variable Scope
Now you have the token stored in a Javascript variable called token
. It's time to store it in Testfully's variable storage so you can use it in subsequent requests. In Testfully, you can store variables in different scopes, serving different purposes:
- Global: Variables stored in the global scope are accessible across all folders and requests in your workspace.
- Environment: Variables stored in the environment scope are accessible across all requests in the same environment.
- Folder: Variables stored in the folder scope are accessible across all requests in the same folder.
You can choose the scope based on your requirements. For example:
- if you want to use the token across all requests in the same folder, you can store it as a folder variable.
- If you want to use the token across all requests in the same environment, you can store it as an environment variable.
- If you want to use the token across all requests in your workspace, you can store it as a global variable.
Step 4: Store the Token Using Testfully's Built-in APIs
Here’s how you can store the token based on your chosen scope:
- As a Global Variable:
let responseBody = $.response.json(); let token = responseBody.token; $.globals.set("apiToken", token); // For global scope
- As an Environment Variable:
let responseBody = $.response.json(); let token = responseBody.token; $.environment.set("apiToken", token); // For environment scope
- As a Folder Variable:
let responseBody = $.response.json(); let token = responseBody.token; $.parentFolder.variables.set("apiToken", token); // For folder scope
Step 5: Use the Token in Subsequent Requests
Our token is now stored in a variable called "apiToken" in the chosen scope. It's time to use it in subsequent requests. There are two ways to use the token in subsequent requests:
- Using Testfully's Authorization Feature: Testfully provides a dedicated UI for setting authorization headers. You can use the token stored in the variable to set the authorization header for the request.
- Using a "Before" Script: Use a "Before" script to retrieve the token from the variable and set it as the authorization header for the request.
We recommend using the "Authorization" feature for setting the token as the authorization header. However, if you want to use a "Before" script, here's how you can do it:
- Load a global variable and use it
// Retrieve the token from storage
let token = $.globals.get("apiToken");
// Inject the token into the authorization header of the request
$.request.headers.add({ key: "Authorization", value: `Bearer ${token}` });
- Load an environment variable and use it
// Retrieve the token from storage
let token = $.environment.get("apiToken");
// Inject the token into the authorization header of the request
$.request.headers.add({ key: "Authorization", value: `Bearer ${token}` });
- Load a folder variable and use it
// Retrieve the token from storage
let token = $.parentFolder.variables.get("apiToken");
// Inject the token into the authorization header of the request
$.request.headers.add({ key: "Authorization", value: `Bearer ${token}` });