Pre-Request Scripts for Suite Execution

Overview

In the API module's collection, a dropdown option is available under the three-dot menu to execute all the requests within a collection folder.

Purpose

Pre-request scripts are used to retrieve data from a previously executed requestS and use these data to update the currently executing request's request body. This is particularly useful in Suite Execution, where requests within a collection folder are executed sequentially.

Functionality

During Suite Execution, the pre-request script extracts data from previously executed requests in suite execution such as:

requestBody

requestParams (if the request is not ISO)

responseHeader (if the request is not ISO)

responseBody

The extracted data is then used to update the request body of the current request.

Methods Used

To achieve this functionality, the following methods are utilized:

For JSON/XML Requests :-

  • Setting Request Body Using Data from a Previous Request Body
req.setBody('PathToSetData', preReq.getBody('previousRequestLabel', 'pathFromPreviousRequestBody'));
  • Setting Request Body Using Data from a Previous Request Params
req.setBody('PathToSetData', preReq.getParam('previousRequestLabel', 'paramsKeyFromPreviousRequestBody'));
  • Setting Request Body Using Data from a Previous Request Headers
req.setBody('PathToSetData', preReq.getHeader('previousRequestLabel', 'paramsKeyFromPreviousRequestBody'));
  • Setting Request Body Using Data from a Previous Response Body
req.setBody('PathToSetData', preRes.getBody('previousRequestLabel', 'pathFromPreviousResponseBody'));
  • Setting Request Body Using Data from a Previous Response Header
req.setBody('PathToSetData', preRes.getHeader('previousRequestLabel', 'keyFromPreviousResponseHeader'));

For ISO Requests :-

For ISO requests, we use fieldId instead of Key within the getBody() and setBody() functions to update the requestBody.

req.setBody('fieldId', preReq.getBody('previousRequestLabel', 'fieldIdFromPreviousISORequestBody')); For getting data from ISO request
req.setBody('fieldId', preRes.getBody('previousRequestLabel', 'keyFromPreviousResponseBody')); For getting data from REST request
req.setBody('fieldId', preRes.getBody('previousRequestLabel', 'paramsKeyFromPreviousRequestBody'));For getting data from REST request
  • User should not be creating requests with same label within a collection.
  • In ISO requets Params & Header do not present.
  • You can add multiple script for a request.
Accessing Body
  • To get/set the data using preRequest scripts we use methods like getBody(), getHeader(),getParam(), setBody()
  • They are used like
req.setBody(`PathToSetData`,preReq.getBody(`PathToGetData`));
req.setBody(`PathToSetData`,preReq.getHeader(`PathToGetData`));
req.setBody(`PathToSetData`,preReq.getParam(`PathToGetData`));

Note :- To get/set nested data use dot(.) notation.

ContentType : JSON/XML :

or use this label to add a new key within the current requestBody.

  • To get/set nested data use dot(.) notation

let we have a request named `JSONRequest` in an API collection with JSON content type.
Request Label : `JSONRequest`
RequestBody : {
    "productId": "1000",
    "productName": "iphone 15",
    "company": "Apple",
    "price": "85000",
    "data": [
        {
            "age": 18
        },
        {
            "name": "user"
        }
    ]
}

responseBody : {
    "cartId": "CR230",
    "message": "Added Successfully"
}


Step 2 : We have another request in the with XML content with label `XMLRequest`

Request Label : `XMLRequest`
RequestBody : <orderRequest>
  <customer>
    <customerId>123456</customerId>
    <name>John Doe</name>
    <email>john.doe@email.com</email>
  </customer>
  <products>
    <product>
      <productId>789</productId>
      <productName>Laptop</productName>
      <quantity>2</quantity>
      <price>1000.00</price>
    </product>
<orderRequest>
  
ResponseBody : <orderResponse>
  <orderId>ODR123456789</orderId>
  <status>Success</status>
  <message>Your order has been placed successfully!</message>
  <totalAmount>2000.00</totalAmount>
  <shippingDetails>
    <estimatedDeliveryDate>2024-01-05</estimatedDeliveryDate>
    <shippingMethod>Standard</shippingMethod>
    <shippingCost>50.00</shippingCost>
  </shippingDetails>
</orderResponse>

Step 3 : Now if we want to update the XMLRequest's requestBody with the data from `JSONRequest` we can do something like this below.

{/* This script is used to get nested JSON data from requestBody of previously executed JSONRequest and update the requestBody of current request XMLRequest before execution in root place within requestBody .
 */}
req.setBody('orderRequest.shippingDetails.age', preReq.getBody('JSONRequest','data.age')); 

{/* This script is used to get JSON data from responseBody of previously executed JSONRequest and update the requestBody of current request XMLRequest before execution in nested place within requestBody .
 */}
req.setBody('orderResponse.cartId', preRes.getBody('JSONRequest','cartId')); 

Image 1 : Updating XML Body

Image 2 : Updating ISO Body

In Image 2 user can see the scripts to update/add the ISORequest's value of fieldIds 13,999.5, 60 by using the data from previously executed requests JSONRequest & XMLRequest.

Summary

  • Pre-request scripts are only applicable in Suite Execution.
  • A pre-request script is a piece of code that runs before the request is executed.
  • They help carry forward data between sequentially executed requests.
  • Methods allow retrieving data from previous request bodies, response bodies, and response headers.
  • This ensures smooth execution of dependent requests within a collection folder, reducing manual intervention and improving automation efficiency.

By utilizing pre-request scripts in PruTAN, you can customize and enhance your API requests with dynamic behavior, making your testing and development processes more efficient and effective.