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 :
Contents
Math operation
This supports all Arithmetic operations such as + , - , * , / , % . Code for all operations will remain the same except symbols. Examples listed below
Input :
Output :
Note 1: In order to get integer response of above operation use “toFixed(0)” in above code like below -
Output :
Note 2: Users are not allowed to change line 1, They can only add respective JavaScript code in between {} example given below.
Good :
Bad :
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.
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 :
Related topics
Last updated