Now, with the help of utility methods, you an extract the variables of your test case response. This functionality is helpful in extracting variables
- from any type (JSON/XML or other than JSON/XML) of test case response.
- or from response headers
- An example use case is performing token based authentication in vREST.
- First define a utility method in Project Configuration >> Utility Methods section. vREST automatically provides the following first two parameters to this utility method automatically. Use supplied parameters will be automatically appended after these two parameters.
response: Test Case Response in string format. If the API is returning JSON response then the response parameter will be stringified JSON. You may overwrite the response parameter for JSON response using the below line:
response = JSON.parse(response);
- opts: Options object
- opts object is a simple JSON object which contains the following keys
- "headers" and value contains the JSON object of response headers.
- "statusCode" and value contains the HTTP response status code.
- to extract variables from response headers, use the opts.headers object.
- to use status code information, use the opts.statusCode.
Write your utility method like below:
- Use this utility method in variable extractor tab like this.
Any parameters passed to this utility method will get appended after two parameters (response and opts) in the utility method. In the above figure, value of var1 will be extracted via utility method varExtractUtility. Here value "hello" will be passed to "userDefinedParam1" parameter and "world" will be passed to "userDefinedParam2" parameter of the utility method defined in step 1.