Forum Discussion
You may use the script in Script Assertion of your request step itself to do the same.
Hi. I am not using it in the tear down script. I have one POST request and another step for the groovy script... here is what I've tried:
/*Read in the next line of the file
We can use the same fileReader created in the Setup script because it
was assigned to the context variable.*/
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])
// Define collection of test steps
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/tester/Documents/test/"
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");
}
testRunner.gotoStep(0)
}That code loops through rows of data I use for my JSON request then it creates a new file which SHOULD save each unique request and response.
Can you please give me a suggestion on how to do this correctly? How do I make a groovy assertion that also saves the request/response?
EDIT:
Rao, I just tried this (added it as an assertion to my POST request) but the results are the same. The script creates one file with one response.
//Check if there is response
assert context.request, "Request is empty or null"
//Save the contents to a file
def saveToFile(file, content) {
if (!file.parentFile.exists()) {
file.parentFile.mkdirs()
log.info "Directory did not exist, created"
}
file.write(content)
assert file.exists(), "${file.name} not created"
}
def f = new File("/Users/tester/Documents/test/testfile.json")
saveToFile(f, context.response)
- nmrao8 years agoCommunity HeroCan you please show the structure of test case?
- willtestforfood8 years agoOccasional Contributor
In my test case I have this in the Setup Script:
//Create a new filereader object, using the context variable so it can be used between test components context.fileReader = new BufferedReader(new FileReader("/Users/test/Documents/test/testdata.csv")) //Read in the first line of the data file firstLine = context.fileReader.readLine() //Split the first line into a string array and assign the array elements to various test case properties String[] propData = firstLine.split("\\,") testCase.setPropertyValue("partnerId",propData[0]) testCase.setPropertyValue("honorificPrefix",propData[1]) testCase.setPropertyValue("first",propData[2]) testCase.setPropertyValue("middle",propData[3]) testCase.setPropertyValue("last",propData[4]) testCase.setPropertyValue("honorificSuffix",propData[5]) testCase.setPropertyValue("addresstype",propData[6]) testCase.setPropertyValue("street1",propData[7])My test case has two steps...
1. REST Request with my POST payload
2. groovy script which has the code I pasted above in my previous comment.
The REST Request from Step 1 goes through the data in my testdata.csv file until it reaches the end of the line. What I would like to achieve is record every request and response into a file or files (if not possible).
Thanks again.