Execute JavaScript

Users can now write JavaScript code to perform math operations, string operations using API’s response.

The idea here is to provide math operations and string operations functionalities under pre-requisite and assertions as a form of javascript code where users can use Json paths of current API response or normal hardcoded values to perform operations.

JavaScript code examples :

Note: Please follow ES5.1 syntax, Rules, and Functions for writing javascript code.

Contents

  • Execute JavaScript

    • Math Operations

    • String Operations

    • Steps to test Execute JavaScript:

    • Related topics


Math operation

  • This supports all Arithmetic operations such as + , - , * , / , % . Code for all operations will remain the same except symbols. Examples listed below

    Input :

      var executeJs = function(response,headers){
          var x = 10;
          var y = 20;
          var z = x * y;
          return z;
      }

    Output :

      200.0

    Note 1: In order to get integer response of above operation use “toFixed(0)” in above code like below -

      var executeJs = function(response,headers){
          var x = 10;
          var y = 20;
          var z = x * y;
          return z.toFixed(0);
      }

    Output :

      200

    Note 2: Users are not allowed to change line 1, They can only add respective JavaScript code in between {} example given below.

    Good :

      var executeJs = function(response,headers){
      //your JavaScript code here
      }

    Bad :

      var testing= function(test1, test2, test3){
      //your JavaScript code here
      }
      var myJsFunction= function(){
      //your JavaScript code here
      }

String operation

  • This operation support all String operations like concat, substring, equals etc. Code for all operation will remain same except mode of operations. Examples listed below

    Input :

    Output :

    Input :

    Output :

    Input :

    Output :

    RegEx example : my API response is having this string in it “code%test_user_1234%”

    Input :

    Output :


Using Json paths

  • This operation used to fetch Json path values and perform math or string opertaions.

  • For this example took reference of this post API from petstore

    • API : “https://petstore.swagger.io/v2/pet”

    • Verb Type : POST

Math operation input :

  • In my case id & category.id contains value “10” respectively.

    Output :

String operation input :

  • In my case photoUrls[0] & tags[0].name contains value “String” respectively.

    Output :

  • Say response is having multiple index of same value then follow below example :

    Input : In my case both values are different.

    Output :

    Note : Current JavaScript implementation does not support any import Node.js of liberates such as console, window etc.

    Example input :

    Output :


Steps to test Execute JavaScript:

Step-1: Navigate to Functional Testing.

Step-2: Step-2: Build a Test Script.

  • Click on to getting started with building a Test Script

Step-3: Click on Assertion tab then click on Json Path Assertion.

Step-4: Select javascript as type and enter your code.

Step-5: Save and Run the script.

  • Results for Execute Javascript would be like as :


  • API To DB Assertion

  • Emailing & Downloading Report