Forum Discussion
Have a look at the script, hope it helps. follow the comments in the script.
https://github.com/nmrao/soapUIGroovyScripts/blob/master/groovy/AccessResponseData.groovy
- rajs20204 years agoFrequent Contributor
nmrao - Thanks, I saw your solution also now. It works, but I have some questions.
1 - Why do you use closures here instead of regular functions?
2 - How do we reuse the code in other suites also instead of repeating the code?
PS - I modified your solution as follows./* Sample response: Content-Type: application/json; charset=utf-8 {"healthy": true} */ //The test step which calls the API. def stepName = 'CallApi' assert getHeader(stepName, 'Content-Type') == 'application/json; charset=utf-8' assert getJson(stepName).healthy == true //Utility functions: def getStepResponse(stepName){ context.testCase.testSteps[stepName].httpRequest.response } def getJson(stepName){ new groovy.json.JsonSlurper().parseText(getStepResponse(stepName).responseContent) } def getHeader(stepName, header){ getStepResponse(stepName).responseHeaders[header].first() }
- nmrao4 years agoChampion Level 3Appreciate to mark as solved if the provided solution works.
1. Refer:
https://stackoverflow.com/questions/21905550/groovy-closures-vs-methods-the-difference
2. Use Script Library for reusing. Refer:
https://www.soapui.org/docs/scripting-and-properties/scripting-and-the-script-library/- rajs20204 years agoFrequent Contributor
nmrao - Thanks. I was not asking about closures vs functions in general. I was asking why you used closures in your code. If there are reasons for it, then I can do the same in my code.
PS -
As an aside, here is the ReadyAPI docs for Groovy Script Library https://support.smartbear.com/readyapi/docs/testing/scripts/libs/groovy-lib.html
Related Content
Recent Discussions
- 3 hours ago
- 5 days ago