Utility Methods

In this section, user can define various utility methods to be used in test cases.

Similar to variables, Utility Methods in vREST provides a way to dynamically change the test case properties at run time. Utility Methods

  • provide dynamic values to test cases.
  • are also available to Response Validators as a third argument.

Utility methods in vREST, can be accessed by using the format {{method_name(argument1, argument2, ...)}}.

By Default, 4 utility methods are available in vREST.

  1. compareJSON :

    This method compares two JSON objects and returns boolean according to whether the two JSON objects are identical to each other or not. This method is used in Response Validator "Default Validator" and non-editable.

  2. validateJSONSchema

    This method validates whether the JSON object matches with the provided JSON schema. This method is also used in Response Validator and non-editable.

  3. getRandom

    This method returns a random number of length 7 if no arguments are passed or returns a random integer between min (included) and max (excluded). This method is also non-editable.

  4. getDate

    This method returns a date timestamp string if no arguments are passed or returns the number of milliseconds between midnight of January 1, 1970 and the current date if true is passed as an argument. This method is also non-editable.

  5. getHeader
    This utility method can be used in Variable Extractor tab and is helpful in extracting value of a response header. This is useful in performing token based authentication in vREST.

 Apart from built-in utility methods, an user can create custom utility methods by clicking on "New Utility" button in "Utility Methods" section. Similar to variables, utility methods can be used in:

  1. Request URL
  2. Request Parameter value
  3. Request Header value
  4. Request Raw Body
  5. Response Expected Body

Lets take some examples, how utility methods are useful in providing dynamic values to your test cases while testing.

 

Note:

An utility method can be invoked inside another utility method by using this.<METHOD_NAME>(<METHOD_ARGS>);

 

Example 1:

Suppose we have an API which accepts a parameter having current timestamp. How you can provide values to such test cases? Utility Methods are a solution:

  1. First define a utility method in the "Utility Methods" section or use any built-in utility method like so



  2. Then use this utility method in your test case like this

Example 2:

Now, suppose we have a numeric variable say "counter" having an initial value say 1 (This initial value can be obtained from either global variable or extracted from previous test case response). Now suppose we have 5 test cases and we want to pass incremented value in each test case.

  1. First we need to write a custom utility method which will increment the input value.



  2. Then use this utility method in your utility method like below. In the below figure, we have passed the variable "counter" to the utility method "increment".