Overview


In your Test Cases,  If some of the Test Data is fetched dynamically from an API Endpoint, we might need to save the Test Data temporarily and use it in subsequent Test Steps. Therefore, Testsigma allows you to fetch data or perform actions via REST API calls along with normal Test Steps in all the Application Types in addition to REST Services so as to provide maximum flexibility and control in your tests. 


Let us see how we can store information that is needed in later steps to Run-time Test Data from a REST Response.




Steps for storing Run-time Test Data from Response


Please check the below article for more details on creating a REST Step - How to add Test Steps for Web Service/API Testing?


Below given is a sample REST Step:




1. After entering the Name, URL, HTTP Method, Test Type and Expected Status Code as in a normal REST API Test Step, please check the second half of the REST Step Creation Wizard.


2. Check the boxes corresponding to whichever Response Content we need to save Test Data from. Check both Store Header Content from Response and Store Body Content from Response check boxes if you want to save values from both Response Header and Response Body

In the above image, we have checked only the Store Body Content from Response.


3. Enter a name in the key field for the Run-time parameter which is to be created . A run-time parameter with the specified name will be created.


4. Enter the JSON Path expression in the value field which evaluates to the Response Entity that we need to store into the run-time parameter created from Step 3. If the Response contains a flat key-value pair instead of a JSON Object, you can simply use the key name here.


Please check this article to know more about JSON Path and its usage - REST API Tutorial - JSONPath

JayWay JSONPath Github Repo

Comparison Modes in Web Services Testing




Example


For this example, let us consider a simple GET request to https://jsonplaceholder.typicode.com/posts/. It returns a JSON Array containing 100 JSON Object items i.e 100 posts. We have truncated the response to 10 posts for brevity:


[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  },
  {
    "userId": 1,
    "id": 3,
    "title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
    "body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
  },
  {
    "userId": 1,
    "id": 4,
    "title": "eum et est occaecati",
    "body": "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
  },
  {
    "userId": 1,
    "id": 5,
    "title": "nesciunt quas odio",
    "body": "repudiandae veniam quaerat sunt sed\nalias aut fugiat sit autem sed est\nvoluptatem omnis possimus esse voluptatibus quis\nest aut tenetur dolor neque"
  },
  {
    "userId": 1,
    "id": 6,
    "title": "dolorem eum magni eos aperiam quia",
    "body": "ut aspernatur corporis harum nihil quis provident sequi\nmollitia nobis aliquid molestiae\nperspiciatis et ea nemo ab reprehenderit accusantium quas\nvoluptate dolores velit et doloremque molestiae"
  },
  {
    "userId": 1,
    "id": 7,
    "title": "magnam facilis autem",
    "body": "dolore placeat quibusdam ea quo vitae\nmagni quis enim qui quis quo nemo aut saepe\nquidem repellat excepturi ut quia\nsunt ut sequi eos ea sed quas"
  },
  {
    "userId": 1,
    "id": 8,
    "title": "dolorem dolore est ipsam",
    "body": "dignissimos aperiam dolorem qui eum\nfacilis quibusdam animi sint suscipit qui sint possimus cum\nquaerat magni maiores excepturi\nipsam ut commodi dolor voluptatum modi aut vitae"
  },
  {
    "userId": 1,
    "id": 9,
    "title": "nesciunt iure omnis dolorem tempora et accusantium",
    "body": "consectetur animi nesciunt iure dolore\nenim quia ad\nveniam autem ut quam aut nobis\net est aut quod aut provident voluptas autem voluptas"
  },
  {
    "userId": 1,
    "id": 10,
    "title": "optio molestias id quia eum",
    "body": "quo et expedita modi cum officia vel magni\ndoloribus qui repudiandae\nvero nisi sit\nquos veniam quod sed accusamus veritatis error"
  }
]


We need to save the 'id' of the third post i.e third JSON Object in this Response and use it in a later Test Step. We can do that by saving the value in a Run-time variable using Store Body Content from Response option in RESTful API Steps and then using the stored Run-time parameter in a subsequent Test Step.




Let us see how to store the value into a run-time parameter.



In a normal REST API Step, we need to do the following to store the value of 'id' into a Run-time variable named 'third_post_id':



1. Enter the Name, URL, HTTP Method, Test Type and Expected Status Code as in a normal REST API Test Step.
2. Check the checkbox Store Body Content from Response since we are going to save Test Data only from Response Body in our case.

3. Enter the name 'third_post_id' in the Key field for the run-time variable we are about to create.

4. Enter the JSONPath Query in the Value field which evaluates to the JSON Element we want to fetch. Since in our case, our Response Body contains a JSON Array, we need to use special query '[2].id' for fetching the required JSON.



Once the Test is run, the Response given on the top of the page will be received and Testsigma will evaluate the expression given in value field to get the JSON Object/Element name whose value is to be stored. Once the JSON Object/Element name is evaluated, it will store the value of the JSON Object/Element inside the newly created run-time parameter with name 'third_post_id'.

That's all we need to do for storing run-time parameters from REST API Test Steps. 



For using previously store Run-time Test Data in REST API Test Steps, please check this article - Using Run-time Test Data in REST API Test Steps in Testsigma