Forum Discussion

willtestforfood's avatar
willtestforfood
Occasional Contributor
8 years ago

How do I record each request and response within the same test step?

Hello everyone. I have spent hours searching the internet for a solution to my problem but was not able to find anything... so hopefully you can help me.

 

I have a simple test case with 2 steps:

 

1. POST (JSON to a REST API

2. Groovy script that reads a text file:

nextLine = context.fileReader.readLine()

if(nextLine != null){
String[] propData = nextLine.split("\\,")
curTC = testRunner.testCase
curTC.setPropertyValue("systemId",propData[0])
curTC.setPropertyValue("partnerId",propData[1])
curTC.setPropertyValue("honorificPrefix",propData[2])
curTC.setPropertyValue("first",propData[3])
curTC.setPropertyValue("middle",propData[4])
curTC.setPropertyValue("last",propData[5])
testRunner.gotoStep(0)
}

I also have the properties setup in the setup script. Anyway, this part is working but how do I record each request and response to a file? I was able to do this BUT it only saves the last request and response, instead of every request and response as per the data in the text file that it reads.

 

This is the script I tried that creates a new file and saves the request and response:

//Define the object for the collection of requests in the soapUI test case
def soapuiRequests = testRunner.testCase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep)


//Groovy Script to loop through each requests in the test case.
soapuiRequests.each{
//Creating file name using current date and time
Date startTime = new Date();
def cur_Time = startTime.getMonth() + 1 + "_" + startTime.getDate() + "_" + startTime.getHours() + startTime.getMinutes() +startTime.getSeconds()
def fileName = it.name + "_" + cur_Time
def path = "/Users/name/Documents/folder/"
def apiFile = new File(path + fileName + ".json")

apiFile.write("\n\n");

apiFile.append(context.testCase.getTestStepByName(it.name).getProperty("rawRequest").value)

apiFile.append("\n\n");
apiFile.append(context.testCase.getTestStepByName(it.name).getProperty("Response").value)
apiFile.append("\n\n"); }



6 Replies

    • willtestforfood's avatar
      willtestforfood
      Occasional Contributor

      Hello Rao. Yes, the code that loops through the data rows in my text file works... however, I cannot figure out how to save each iteration to a file. Only the last request and response get saved. 

       

      Thanks.

      • nmrao's avatar
        nmrao
        Champion Level 3
        Are you using teardown script of test case to log the request and response?

        You may use the script in Script Assertion of your request step itself to do the same.