Dynamic payload during runtime

Hello there,
I have below scenario.

  1. Post a message to Kafka topic. I have created custom keyworks for initiating the connecting and producing to the topic.
  2. It works fine with static payload and it’s in JSON format.
  3. Requirement here is, we need to update the JSON payload every time before we produce on topic.

Sample Payload:
{
“event”: {
“header”: {
“headerRow”: {
“originatorCode”: “NILE-LAX”,
“shipmentReferenceNumber”: “SXA231116111”,
“shipmentINCOterm”: “FOB”,
}
}

In this example, we need to update the SXA231116111 to something unique every time.

Currently, I am having the json file stored in “Include” folder in katalon studio like below and calling in test cases using below code.


CustomKeywords.‘CustomFunctions.KafkaProducerKeyword.initializeKafkaProducer’(GlobalVariable.Kafka_BootStrap_Servers)

String jsonFilePath = System.getProperty(“user.dir”) + “Include/Payloads/Visibility/eventPayload.json”
def eventPayload = new File(jsonFilePath).text
println("Event Payload = " + eventPayload)

//CustomKeywords.‘CustomFunctions.KafkaProducerKeyword.produceMessage’(GlobalVariable.Kakfa_Visibility_Event_Topic, ‘’, eventPayload)
//CustomKeywords.‘CustomFunctions.KafkaProducerKeyword.closeKafkaProducer’()


Template Payload in nclude/Payloads/Visibility/eventPayload.json
Sample Payload:
{
“event”: {
“header”: {
“headerRow”: {
“originatorCode”: “NILE-LAX”,
“shipmentReferenceNumber”: “${ShipNum}”,
“shipmentINCOterm”: “FOB”,
}
}

while executing I have created a parameter “ShipNum” in test case and hoping it will map the variable to payload on execution. But unfortunately, its not happening. Payload is sent as in in template.

Any suggestions/guidance is well appreciated :slight_smile:

1 Like

You need groovy.text.SimpleTemplateEngine.

See Template engines

or

try creating custom code for json manipulation if templating doesn’t work for use case