Pre-Request Scripts for Suite Execution

Overview

In the Studio 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 previously executed requests and use that data to update the currently executing request's body. This is particularly useful in Suite Execution, where requests within a collection folder are executed sequentially.

For more details on suite execution, including how to run scripts and view suite analytics, refer to the Suites page.

Functionality

During Suite Execution, the pre-request script extracts data from previously executed requests, 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', 'paramsKeyFromPreviousRequestParams'));
  • Setting Request Body Using Data from a Previous Request Headers
req.setBody('PathToSetData', preReq.getHeader('previousRequestLabel', 'headerKeyFromPreviousRequestHeader'));
  • 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 a key/path within the getBody() and setBody() functions to update the requestBody.

// Get data from a previously executed ISO request
req.setBody('fieldId', preReq.getBody('previousRequestLabel', 'fieldIdFromPreviousISORequestBody'));

// Get data from a previously executed REST request's response body
req.setBody('fieldId', preRes.getBody('previousRequestLabel', 'keyFromPreviousResponseBody'));

// Get data from a previously executed REST request's params
req.setBody('fieldId', preReq.getParam('previousRequestLabel', 'paramsKeyFromPreviousRequestParams'));
  • Users should not create requests with the same label within a collection.
  • In ISO requests, Params & Headers are not present.
  • You can add multiple scripts 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

Suppose we have a request with JSON content type:

  • Request Label: JSONRequest
  • Request Body:
{
  "productId": "1000",
  "productName": "iphone 15",
  "company": "Apple",
  "price": "85000",
  "data": [
    { "age": 18 },
    { "name": "user" }
  ]
}
  • Response Body:
{
  "cartId": "CR230",
  "message": "Added Successfully"
}

Now suppose we have another request in the collection with XML content type:

  • Request Label: XMLRequest
  • Request Body:
<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>
  </products>
</orderRequest>
  • Response Body:
<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>

To update the XMLRequest body with data from the previously executed JSONRequest, we can use scripts like the following:

// Get nested JSON data from the previous request body and set it in the current request body
// `data` is an array, so index the first element with dot notation: data.0.age
req.setBody('orderRequest.shippingDetails.age', preReq.getBody('JSONRequest', 'data.0.age'));

// Get data from the previous response body
req.setBody('orderResponse.cartId', preRes.getBody('JSONRequest', 'cartId'));

// Get data from the previous response headers
req.setBody('orderResponse.length', preRes.getHeader('JSONRequest', 'Content-Length'));

// Get a query parameter from a previously executed request
req.setBody('orderRequest.param', preReq.getParam('JSONRequest', 'p1'));

Image 1 : Updating XML Body

Image 2 : Updating ISO Body

In Image 2 you can see scripts that update the ISO request's fieldIds 13, 999.5, and 60 using data from previously executed JSONRequest and XMLRequest requests.

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.