Forum Discussion

Flo's avatar
Flo
Contributor
6 years ago

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>

 

  • Flo 

    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 }
  • nmrao's avatar
    nmrao
    Champion Level 3
    I am afraid you could do that or it may work after the change.

    Each operation supports fixed element, and changing that may not be suggested.

    Or missing the details or context in the question.
    • Flo's avatar
      Flo
      Contributor

      nmrao  I edited my question with this :"The name of the operation changed in the WS I used to test, but the contents still the same."

      You are right regarding the risks but in this case I know it is possible.

      • nmrao's avatar
        nmrao
        Champion Level 3
        Not sure if I am confused or you.

        Operation name and Element name in the request are two different things. Is it only Element name changed in your case? And not the operation name?