Forum Discussion
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.
- nmrao8 years agoChampion Level 3Are 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.- willtestforfood8 years agoOccasional Contributor
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 agoChampion Level 3Can you please show the structure of test case?
Related Content
- 6 years agopankajmalinda
- 7 years agoKate
- 7 years agoluluberlu
- 7 years agomoolickj
- 9 years agomaximojo
Recent Discussions
- 3 days agoemoya
- 3 days agoMyBalanceNow