Flo
6 years agoContributor
Groovy - How to automatically modify/set operation test step request
Hello,
I want to automatically change content of a WSDL request of several requests in test steps of a test case. The name of the service changed in the WS I used to test, but the contents still the same.
I add the idea to do this using a groovy test step script to launch manually. In order to reuse it, I created it in a separated TestSuite.
def testCase = testRunner.testCase.testSuite.project.getTestSuiteByName("testSuiteName").getTestCaseByName("testCaseName") def request, holder testCase.testStepList.each { step -> request = context.expand( '${#[testSuiteName#testCaseName#'+step.name+']#Request}') if(request.contains("<soap:serviceNameBefore>")){ request = request.replaceAll("soap:serviceNameBefore>","soap:serviceNameAfter>") // At this time, the request is not updated on expected test step // I guess I missing something here } }
I don't find the correct method to update request according to the variable I modified (a string with expected request)
Do you have an idea ? Is there another way to do that ?
To help understand, here is the example of what I want to do. Change this request :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://XX/"> <soapenv:Header/> <soapenv:Body> <soap:serviceNameBefore> <request> <name>?</name> <price>?</price> </request> </soap:serviceNameBefore> </soapenv:Body> </soapenv:Envelope>
in all test step to that one
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://XX/"> <soapenv:Header/> <soapenv:Body> <soap:serviceNameAfter> <request> <name>?</name> <price>?</price> </request> </soap:serviceNameAfter> </soapenv:Body> </soapenv:Envelope>
Below should help you:
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
//Define element names in below two statements
def elementNameBefore = 'DefineOldName'
def elementNameAfter = 'DefineNameName'
//No change required beyond this
def testCase = testRunner.testCase.testSuite.project.getTestSuiteByName("testSuiteName").getTestCaseByName("testCaseName") testCase.testStepList.findAll{it instanceof WsdlTestRequestStep}.each { step -> def content = step.httpRequest.requestContent def changedContent = content.replaceAll(elementNameBefore, elementNameAfter) step.httpRequest.requestContent = changedContent }