Set Variable
Set Variable is a component that can be added to the test to capture a particular value for later use or to perform computations to arrive at a given result.
Parameters
| Name | Type | Required? | Description | Default value |
|---|---|---|---|---|
| Variable Name | String | Yes | Name of variable to be used in subsequent components | |
| Mode | Selection List | Yes | Select one of the following values:
|
String |
| Value | String | No | Provide the Value of the variable if String was selected for Mode |
|
| Object | String | No | Provide the logic for use when Mode is set to Object |
|
| Language | Selection List | No | The language you wish to script in, Groovy is the only option available | Groovy |
| Content | String | No | Your Groovy script logic |
String
To set a variable from a requests response if one is given.
for example to set a variable to the name property Add Set Variable Component from list Provide the name of the variable Set the value to ${<request varaible name>.name}
If the variable being referenced is wihtin a loop the variable name will be _1
Setting a Variable using Groovy script
To set variable to a value that requires a bit of additional logic, you can write a segment of groovy script to process inputs and provide an single output.
To write script, within Set Variable change the Variable Mode to Language
As an example if given a temperature I want to check if where it falls on a set of parameters using the tempature from a prior request
def temperature="Just Right"
if(payload.temperature>22){
temperature='Its Warm'
} else if(payload.temperature<16){
temperature='Its Cold'
}
return temperature
You much return a inner variable at the end to set the value of variable to be used later
NOTE: within Groovy script you do not need to wrap existing variables within ${}
Object Mode
Object allows you to inspect the a larger object to find particular parts of a varialbe
payload.findAll {it.name == ‘Douglas Adams’}